<?php

namespace Tests\Unit\Services\AttendanceTracking;

use App\Services\AttendanceTracking\AttendanceParentLookupService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class AttendanceParentLookupServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_get_primary_parent_for_student(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'One',
            'cellphone' => '5555555555',
            'email' => 'parent@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('students')->insert([
            'id' => 1,
            'school_id' => 'S1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 8,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
        ]);

        $service = new AttendanceParentLookupService();
        $parent = $service->getPrimaryParentForStudent(1);

        $this->assertSame('parent@example.com', $parent['email']);
    }

    public function test_get_secondary_parent_falls_back_to_parent_row(): void
    {
        DB::table('students')->insert([
            'id' => 1,
            'school_id' => 'S1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 8,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => 11,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
        ]);

        DB::table('parents')->insert([
            'secondparent_firstname' => 'Second',
            'secondparent_lastname' => 'Parent',
            'secondparent_gender' => 'Female',
            'secondparent_email' => 'second@example.com',
            'secondparent_phone' => '5551112222',
            'firstparent_id' => 11,
            'secondparent_id' => 0,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $service = new AttendanceParentLookupService();
        $parent = $service->getSecondaryParentForStudent(1, '2025-2026');

        $this->assertSame('second@example.com', $parent['email']);
    }
}
