<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Feature\Concerns\BuildsFeatureData;
use Tests\TestCase;

class ApplicantFeatureFlowTest extends TestCase
{
    use BuildsFeatureData;
    use DatabaseTransactions;

    public function test_dashboard_requires_login_session(): void
    {
        $response = $this->get(route('dashboard'));

        $response->assertRedirect(route('login'));
    }

    public function test_logged_in_applicant_can_view_dashboard(): void
    {
        $applicant = $this->createApplicant();
        $job = $this->createJob();
        $this->createJobApplication($applicant, $job);

        $response = $this->withSession([
            'applicant_id' => $applicant->id,
        ])->get(route('dashboard'));

        $response->assertOk();
    }

    public function test_submit_application_creates_record(): void
    {
        $applicant = $this->createApplicant();
        $job = $this->createJob([
            'is_active' => true,
            'deadline' => now()->addDays(3),
        ]);

        $response = $this->withSession([
            'applicant_id' => $applicant->id,
        ])->post(route('apply.submit', $job));

        $response->assertRedirect(route('dashboard'));
        $this->assertDatabaseHas('applications', [
            'applicant_id' => $applicant->id,
            'job_listing_id' => $job->id,
            'status' => 'applied',
        ]);
    }

    public function test_submit_application_redirects_home_for_inactive_job(): void
    {
        $applicant = $this->createApplicant();
        $job = $this->createJob([
            'is_active' => false,
        ]);

        $response = $this->withSession([
            'applicant_id' => $applicant->id,
        ])->post(route('apply.submit', $job));

        $response->assertRedirect(route('home'));
        $response->assertSessionHas('error');
    }

    public function test_logged_in_applicant_can_edit_and_update_profile(): void
    {
        $applicant = $this->createApplicant([
            'name' => 'Nama Awal',
        ]);

        $this->withSession(['applicant_id' => $applicant->id])
            ->get(route('profile.edit'))
            ->assertOk();

        $response = $this->withSession(['applicant_id' => $applicant->id])
            ->post(route('profile.update'), [
                'name' => 'Nama Baru',
                'whatsapp' => '081234561111',
                'education' => 'S1',
                'birth_date' => '1999-02-02',
                'gender' => 'Perempuan',
                'province' => 'Jawa Barat',
                'city' => 'Kota Bandung',
            ]);

        $response->assertRedirect(route('profile.edit'));
        $this->assertDatabaseHas('applicants', [
            'id' => $applicant->id,
            'name' => 'Nama Baru',
            'gender' => 'Perempuan',
        ]);
    }

    public function test_cities_api_returns_city_list(): void
    {
        $applicant = $this->createApplicant();

        $response = $this->withSession([
            'applicant_id' => $applicant->id,
        ])->get(route('api.cities', ['province' => 'Jawa Barat']));

        $response->assertOk();
        $response->assertJsonFragment(['Kota Bandung']);
    }

    public function test_stage_form_can_be_opened_and_submitted(): void
    {
        $applicant = $this->createApplicant();
        $job = $this->createJob();
        $stage = $this->createStage($job, [
            'stage_number' => 1,
            'name' => 'Tahap 1',
            'form_fields' => [
                [
                    'name' => 'motivasi',
                    'label' => 'Motivasi',
                    'type' => 'text',
                    'required' => true,
                ],
            ],
        ]);

        $application = $this->createJobApplication($applicant, $job, [
            'status' => 'stage1_passed',
            'current_stage' => 1,
        ]);

        $showResponse = $this->withSession([
            'applicant_id' => $applicant->id,
        ])->get(route('form.stage', [$application, $stage]));

        $showResponse->assertOk();

        $submitResponse = $this->withSession([
            'applicant_id' => $applicant->id,
        ])->post(route('form.stage.submit', [$application, $stage]), [
            'field_motivasi' => 'Saya tertarik dengan posisi ini.',
        ]);

        $submitResponse->assertRedirect(route('dashboard'));
        $this->assertDatabaseHas('stage_responses', [
            'application_id' => $application->id,
            'job_stage_id' => $stage->id,
        ]);
        $this->assertDatabaseHas('applications', [
            'id' => $application->id,
            'status' => 'stage1_form_filled',
            'current_stage' => 1,
        ]);
    }
}
