<?php

namespace Tests\Feature\Api\V1\Administrator;

use App\Models\EmergencyContact;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class EmergencyContactControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_grouped_contacts(): void
    {
        $admin = User::factory()->create();
        $parent = User::factory()->create([
            'firstname' => 'Omar',
            'lastname' => 'Parent',
            'cellphone' => '1111111111',
        ]);

        DB::table('parents')->insert([
            'secondparent_firstname' => 'Nora',
            'secondparent_lastname' => 'Parent',
            'secondparent_gender' => 'Female',
            'secondparent_email' => 'nora.parent@example.com',
            'secondparent_phone' => '2222222222',
            'firstparent_id' => $parent->id,
            'secondparent_id' => 1234,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        Student::factory()->create([
            'parent_id' => $parent->id,
            'firstname' => 'Rami',
            'lastname' => 'Student',
            'school_id' => 'S-202',
        ]);

        EmergencyContact::query()->create([
            'parent_id' => $parent->id,
            'emergency_contact_name' => 'Uncle Amin',
            'cellphone' => '3333333333',
            'email' => 'amin@example.com',
            'relation' => 'Uncle',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        Sanctum::actingAs($admin);
        $response = $this->getJson('/api/v1/administrator/emergency-contacts');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $response->assertJsonPath('groups.0.parent_id', $parent->id);
        $response->assertJsonPath('groups.0.parent_phones.0', '1111111111');
        $response->assertJsonPath('groups.0.parent_phones.1', '2222222222');
    }

    public function test_update_changes_emergency_contact(): void
    {
        $admin = User::factory()->create();
        $parent = User::factory()->create();

        $contact = EmergencyContact::query()->create([
            'parent_id' => $parent->id,
            'emergency_contact_name' => 'Old Name',
            'cellphone' => '4444444444',
            'email' => 'old@example.com',
            'relation' => 'Friend',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        Sanctum::actingAs($admin);
        $response = $this->patchJson('/api/v1/administrator/emergency-contacts/' . $contact->id, [
            'name' => 'New Name',
            'cellphone' => '5555555555',
            'email' => 'new@example.com',
            'relation' => 'Neighbor',
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('emergency_contacts', [
            'id' => $contact->id,
            'emergency_contact_name' => 'New Name',
            'email' => 'new@example.com',
            'relation' => 'Neighbor',
        ]);
    }

    public function test_destroy_deletes_emergency_contact(): void
    {
        $admin = User::factory()->create();
        $parent = User::factory()->create();

        $contact = EmergencyContact::query()->create([
            'parent_id' => $parent->id,
            'emergency_contact_name' => 'Delete Name',
            'cellphone' => '6666666666',
            'email' => 'delete@example.com',
            'relation' => 'Friend',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        Sanctum::actingAs($admin);
        $response = $this->deleteJson('/api/v1/administrator/emergency-contacts/' . $contact->id);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseMissing('emergency_contacts', [
            'id' => $contact->id,
        ]);
    }
}
