<?php

namespace Tests\Feature;

use App\Models\Appointment;
use App\Models\Client;
use App\Models\Patient;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AppointmentsTest extends TestCase
{
    use RefreshDatabase;

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

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

        $this->actingAs($user)
            ->get(route('clinica.appointments.index'))
            ->assertOk();
    }

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

        $client = Client::query()->create([
            'document_type' => 'V',
            'document_number' => '123',
            'name' => 'Cliente Test',
            'phone' => null,
            'email' => null,
            'address' => null,
            'notes' => null,
        ]);
        $patient = Patient::query()->create([
            'client_id' => $client->id,
            'name' => 'Mascota',
            'is_active' => true,
        ]);

        $starts = now()->addDay()->setHour(10)->setMinute(0)->setSecond(0);
        $ends = $starts->copy()->addMinutes(30);

        $this->actingAs($user)
            ->post(route('clinica.appointments.store'), [
                'patient_id' => $patient->id,
                'assigned_to' => null,
                'title' => 'Consulta',
                'starts_at' => $starts->toDateTimeString(),
                'ends_at' => $ends->toDateTimeString(),
                'notes' => null,
            ])
            ->assertSessionHasNoErrors()
            ->assertRedirect();

        $this->assertDatabaseHas('appointments', [
            'patient_id' => $patient->id,
            'client_id' => $client->id,
            'title' => 'Consulta',
            'status' => 'scheduled',
        ]);
    }

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

        $client = Client::query()->create([
            'document_type' => 'V',
            'document_number' => '999',
            'name' => 'C2',
            'phone' => null,
            'email' => null,
            'address' => null,
            'notes' => null,
        ]);
        $patient = Patient::query()->create([
            'client_id' => $client->id,
            'name' => 'P2',
            'is_active' => true,
        ]);

        $appointment = Appointment::query()->create([
            'patient_id' => $patient->id,
            'client_id' => $client->id,
            'assigned_to' => null,
            'created_by' => $user->id,
            'title' => null,
            'starts_at' => now()->addDay(),
            'ends_at' => now()->addDay()->addHour(),
            'status' => Appointment::STATUS_SCHEDULED,
            'notes' => null,
        ]);

        $this->actingAs($user)
            ->delete(route('clinica.appointments.destroy', $appointment))
            ->assertRedirect();

        $appointment->refresh();
        $this->assertSame(Appointment::STATUS_CANCELLED, $appointment->status);
    }
}
