<?php

namespace Tests\Unit\Services\Families;

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

class FamilyMutationServiceTest extends TestCase
{
    use RefreshDatabase;

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

        $service = new FamilyMutationService();
        $result = $service->bootstrapFamilies();

        $this->assertSame(1, $result['families_created']);
        $this->assertDatabaseHas('families', ['family_code' => 'FAM-1']);
        $this->assertDatabaseHas('family_students', ['student_id' => 10]);
    }

    public function test_attach_second_by_email_creates_guardian(): void
    {
        $this->seedUser(1);
        $this->seedStudent(10, 1);
        DB::table('families')->insert([
            'family_code' => 'FAM-1',
            'household_name' => 'Family 1',
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new FamilyMutationService();
        $result = $service->attachSecondByEmail(10, 'secondary@example.com', 'Second', 'Parent');

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('users', ['email' => 'secondary@example.com']);
        $this->assertDatabaseHas('family_guardians', ['user_id' => $result['user_id']]);
    }

    public function test_set_primary_home_updates_flag(): void
    {
        $this->seedUser(1);
        $this->seedStudent(10, 1);
        $familyId = DB::table('families')->insertGetId([
            'family_code' => 'FAM-1',
            'household_name' => 'Family 1',
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        DB::table('family_students')->insert([
            'family_id' => $familyId,
            'student_id' => 10,
            'is_primary_home' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new FamilyMutationService();
        $service->setPrimaryHome($familyId, 10, true);

        $this->assertDatabaseHas('family_students', [
            'family_id' => $familyId,
            'student_id' => 10,
            'is_primary_home' => 1,
        ]);
    }

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

    private function seedStudent(int $id, int $parentId): void
    {
        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,
        ]);
    }
}
