<?php

namespace Tests\Feature\Api\V1\Parents;

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

class ParentControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_attendance_returns_rows(): void
    {
        $this->seedConfig();
        $parent = $this->seedParent();

        $studentId = DB::table('students')->insertGetId([
            'school_id' => 'S-100',
            'firstname' => 'Adam',
            'lastname' => 'Parent',
            'dob' => '2015-09-01',
            'age' => 10,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => $parent->id,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        DB::table('attendance_data')->insert([
            'class_id' => 1,
            'class_section_id' => 1,
            'student_id' => $studentId,
            'school_id' => '1',
            'date' => '2025-10-01',
            'status' => 'absent',
            'reason' => 'Sick',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        Sanctum::actingAs($parent);
        $response = $this->getJson('/api/v1/parents/attendance?school_year=2025-2026');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $response->assertJsonPath('attendance.0.status', 'absent');
    }

    public function test_registration_creates_student_and_contact(): void
    {
        $this->seedConfig();
        $parent = $this->seedParent();

        Sanctum::actingAs($parent);
        $payload = [
            'students' => [
                [
                    'firstname' => 'Sara',
                    'lastname' => 'Parent',
                    'dob' => '2016-02-01',
                    'gender' => 'Female',
                    'registration_grade' => '2',
                    'photo_consent' => true,
                    'is_new' => true,
                    'allergies' => ['Peanuts'],
                    'medical_conditions' => ['Asthma'],
                ],
            ],
            'emergency_contacts' => [
                [
                    'name' => 'John Parent',
                    'cellphone' => '1234567890',
                    'email' => 'john.parent@example.com',
                    'relation' => 'Father',
                ],
            ],
        ];

        $response = $this->postJson('/api/v1/parents/registration', $payload);

        $response->assertStatus(201);
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('students', ['firstname' => 'Sara', 'parent_id' => $parent->id]);
        $this->assertDatabaseHas('emergency_contacts', ['parent_id' => $parent->id, 'emergency_contact_name' => 'John Parent']);
    }

    private function seedParent(): User
    {
        $id = DB::table('users')->insertGetId([
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '1234567890',
            'email' => 'parent@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        return User::query()->findOrFail($id);
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
            ['config_key' => 'date_age_reference', 'config_value' => '2025-12-31'],
            ['config_key' => 'max_kids', 'config_value' => '5'],
            ['config_key' => 'max_emergency', 'config_value' => '5'],
            ['config_key' => 'enrollment_deadline', 'config_value' => '2025-09-01'],
        ]);
    }
}
