<?php

namespace Tests\Feature;

use App\Models\StudentProfile;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Tests\Traits\WithTestAuth;

class StudentProfileTest extends TestCase
{
    use RefreshDatabase;
    use WithTestAuth;

    protected function setUp(): void
    {
        parent::setUp();
    }

    public function test_user_can_login(): void
    {
        $user = $this->createStudentUser();

        StudentProfile::factory()->create([
            'user_id' => $user->id,
            'verification_status' => 'approved',
        ]);

        $this->actingAs($user)
            ->get('/student')
            ->assertStatus(200);
    }

    public function test_new_user_redirected_to_application_form(): void
    {
        $user = $this->createStudentUser();

        // Should be redirected to application form because no profile
        $this->actingAs($user)
            ->get('/student')
            ->assertRedirect('/student/application-form');
    }

    public function test_application_form_loads_successfully(): void
    {
        $user = $this->createStudentUser();

        // Seed states to verify options closure runs
        \App\Models\State::create(['name' => 'Delhi', 'code' => 'DL']);
        \App\Models\State::create(['name' => 'Mumbai', 'code' => 'MH']);

        $this->actingAs($user)
            ->get('/student/application-form')
            ->assertStatus(200)
            ->assertSee('First priority')
            ->assertSee('Delhi')
            ->assertSee('Mumbai');
    }

    public function test_user_can_submit_application_form(): void
    {
        $user = $this->createStudentUser();

        $profile = StudentProfile::factory()->create([
            'user_id' => $user->id,
            'verification_status' => 'pending',
        ]);

        // With pending status, should see status page
        $this->actingAs($user)
            ->get('/student')
            ->assertRedirect('/student/application-status');

        $this->actingAs($user)
            ->get('/student/application-status')
            ->assertStatus(200);
    }

    public function test_approved_user_can_access_dashboard(): void
    {
        $user = $this->createStudentUser();

        $profile = StudentProfile::factory()->create([
            'user_id' => $user->id,
            'verification_status' => 'approved',
        ]);

        $this->actingAs($user)
            ->get('/student')
            ->assertStatus(200);
    }
}
