<?php

namespace Tests\Unit\Services\Parents;

use App\Models\User;
use App\Services\Parents\ParentAttendanceService;
use App\Services\Parents\ParentConfigService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ParentAttendanceServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_attendance_returns_rows(): 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'],
        ]);

        $parentId = DB::table('users')->insertGetId([
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '1234567890',
            'email' => 'parent2@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',
        ]);

        $studentId = DB::table('students')->insertGetId([
            'school_id' => 'S-101',
            'firstname' => 'Maya',
            'lastname' => 'Parent',
            'dob' => '2014-09-01',
            'age' => 11,
            'gender' => 'Female',
            'photo_consent' => 1,
            'parent_id' => $parentId,
            '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-02',
            'status' => 'late',
            'reason' => 'Traffic',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $service = new ParentAttendanceService(new ParentConfigService());
        $result = $service->listAttendance($parentId, '2025-2026');

        $this->assertSame('2025-2026', $result['selectedYear']);
        $this->assertCount(1, $result['attendance']);
        $this->assertSame('late', $result['attendance'][0]['status']);
    }
}
