<?php

namespace Tests\Unit\Services\Families;

use App\Services\Families\FamilyFinanceService;
use App\Services\Families\FamilyQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class FamilyQueryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_index_returns_families(): void
    {
        $this->seedConfig();
        $studentId = $this->seedStudent(10, 1);
        $familyId = $this->seedFamily('FAM-1');
        $this->seedFamilyStudent($familyId, $studentId);
        $this->seedFamilyGuardian($familyId, 1);

        $service = new FamilyQueryService(new FamilyFinanceService());
        $result = $service->adminIndexData($studentId, null, '2025-2026');

        $this->assertNotEmpty($result['families']);
        $this->assertSame($studentId, $result['student']['id']);
    }

    public function test_search_suggestions_returns_items(): void
    {
        $this->seedStudent(10, 1);

        $service = new FamilyQueryService(new FamilyFinanceService());
        $items = $service->searchSuggestions('Student', 5);

        $this->assertNotEmpty($items);
        $this->assertSame('student', $items[0]['type']);
    }

    public function test_family_card_returns_payload(): void
    {
        $this->seedConfig();
        $studentId = $this->seedStudent(10, 1);
        $familyId = $this->seedFamily('FAM-1');
        $this->seedFamilyStudent($familyId, $studentId);
        $this->seedFamilyGuardian($familyId, 1);

        $service = new FamilyQueryService(new FamilyFinanceService());
        $family = $service->familyCard(null, null, $familyId, '2025-2026');

        $this->assertNotNull($family);
        $this->assertSame($familyId, $family['id']);
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);
    }

    private function seedStudent(int $id, int $parentId): int
    {
        DB::table('users')->insert([
            'id' => $parentId,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '9999999999',
            'email' => 'parent' . $parentId . '@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',
        ]);

        DB::table('students')->insert([
            'id' => $id,
            'school_id' => 'SCH1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 10,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => $parentId,
            'year_of_registration' => '2025-2026',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_active' => 1,
        ]);

        return $id;
    }

    private function seedFamily(string $code): int
    {
        return DB::table('families')->insertGetId([
            'family_code' => $code,
            'household_name' => 'Family ' . $code,
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedFamilyStudent(int $familyId, int $studentId): void
    {
        DB::table('family_students')->insert([
            'family_id' => $familyId,
            'student_id' => $studentId,
            'is_primary_home' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedFamilyGuardian(int $familyId, int $userId): void
    {
        DB::table('family_guardians')->insert([
            'family_id' => $familyId,
            'user_id' => $userId,
            'relation' => 'primary',
            'is_primary' => 1,
            'receive_emails' => 1,
            'receive_sms' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
