<?php

namespace Tests\Feature\TravelRequest;

use App\Enums\JourneyMethod;
use App\Enums\TravelStatus;
use App\Models\ApprovalStep;
use App\Models\ApprovalWorkflow;
use App\Models\TravelRequest;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CreateTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(RoleSeeder::class);
    }

    public function test_create_page_requires_authentication(): void
    {
        $this->get('/travel-requests/create')->assertRedirect('/login');
    }

    public function test_authenticated_user_can_access_create_page(): void
    {
        $user = User::factory()->create();
        $user->assignRole('staff');

        $this->actingAs($user)->get('/travel-requests/create')->assertStatus(200);
    }

    public function test_travel_request_is_created_as_draft(): void
    {
        $user = User::factory()->create();
        $user->assignRole('staff');

        $workflow = ApprovalWorkflow::factory()->create(['is_active' => true]);
        ApprovalStep::factory()->create(['workflow_id' => $workflow->id, 'order' => 1, 'role' => 'travel_approver']);

        $this->actingAs($user);

        $requestData = [
            'emergencyFullName' => 'Jane Doe',
            'emergencyPhone' => '0400000000',
            'emergencyRelationship' => 'Spouse',
            'reasonSummary' => 'Attending the annual conference in Sydney.',
            'travelCategory' => 'event',
            'eventType' => 'Conference',
            'journeys' => [[
                'origin' => 'Perth',
                'destination' => 'Sydney',
                'date' => now()->addDays(30)->format('Y-m-d'),
                'time' => '08:00',
                'method' => JourneyMethod::Air->value,
            ]],
            'needsAccommodation' => true,
            'needsCarHire' => false,
            'vehiclePolicyAcknowledged' => false,
            'businessDays' => 3,
            'privateDays' => 0,
            'costCodes' => [[
                'business_unit' => 'MED',
                'project_grant' => 'PG-12345',
                'account_code' => 'AC001',
                'class_code' => '',
            ]],
        ];

        \Livewire\Livewire::test('travel-request.create')
            ->set('emergencyFullName', $requestData['emergencyFullName'])
            ->set('emergencyPhone', $requestData['emergencyPhone'])
            ->set('emergencyRelationship', $requestData['emergencyRelationship'])
            ->set('reasonSummary', $requestData['reasonSummary'])
            ->set('journeys', $requestData['journeys'])
            ->call('saveDraft');

        $this->assertDatabaseHas('travel_requests', [
            'user_id' => $user->id,
            'status' => TravelStatus::Draft->value,
        ]);
    }

    public function test_journey_validation_requires_origin_and_destination(): void
    {
        $user = User::factory()->create();
        $user->assignRole('staff');
        $this->actingAs($user);

        \Livewire\Livewire::test('travel-request.create')
            ->set('reasonSummary', 'Test reason for travel purpose')
            ->set('emergencyFullName', 'Jane Doe')
            ->set('emergencyPhone', '0400000000')
            ->set('emergencyRelationship', 'Spouse')
            ->set('journeys', [[
                'origin' => '',
                'destination' => '',
                'date' => now()->addDays(10)->format('Y-m-d'),
                'time' => '',
                'method' => JourneyMethod::Air->value,
            ]])
            ->call('saveDraft')
            ->assertHasErrors(['journeys.0.origin', 'journeys.0.destination']);
    }
}
