<?php

namespace Tests\Feature;

use App\Models\Program;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ProgramTest extends TestCase
{
    use RefreshDatabase;

    private User $admin;

    protected function setUp(): void
    {
        parent::setUp();
        $this->admin = User::factory()->create();
        $this->actingAs($this->admin);
    }

    public function test_admin_can_view_programs_list(): void
    {
        $this->get('/admin/programs')->assertOk();
    }

    public function test_admin_can_create_a_program(): void
    {
        $this->post('/admin/programs', [
            'title'                    => 'Program Ujian',
            'organizer'                => 'Jabatan Ujian',
            'location'                 => 'Putrajaya',
            'start_date'               => '2025-06-01',
            'end_date'                 => '2025-06-01',
            'allow_walk_in'            => true,
            'default_staff_session'    => 'pagi',
            'default_external_session' => 'pagi',
        ])->assertRedirect();

        $this->assertDatabaseHas('programs', ['title' => 'Program Ujian']);
    }

    public function test_admin_can_publish_a_draft_program(): void
    {
        $program = Program::factory()->create(['status' => 'draft', 'created_by' => $this->admin->id]);

        $this->post("/admin/programs/{$program->uuid}/publish")->assertRedirect();

        $this->assertEquals('published', $program->fresh()->status);
    }

    public function test_admin_can_delete_program_with_no_attendances(): void
    {
        $program = Program::factory()->create(['status' => 'draft', 'created_by' => $this->admin->id]);

        $this->delete("/admin/programs/{$program->uuid}")->assertRedirect();

        $this->assertDatabaseMissing('programs', ['id' => $program->id]);
    }
}
