<?php

namespace Tests\Unit\Services\Attendance;

use App\Services\Attendance\AttendanceRecordSyncService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class AttendanceRecordSyncServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_update_attendance_record_adjusts_totals(): void
    {
        DB::table('attendance_record')->insert([
            'class_section_id' => 1,
            'student_id' => 10,
            'school_id' => 'S1',
            'total_presence' => 5,
            'total_absence' => 1,
            'total_late' => 2,
            'total_attendance' => 8,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $service = new AttendanceRecordSyncService(new \App\Models\AttendanceRecord());
        $service->updateAttendanceRecord(10, 'S1', 'absent', 'Fall', '2025-2026', 'present', 1);

        $row = DB::table('attendance_record')->where('student_id', 10)->first();
        $this->assertSame(4, (int) $row->total_presence);
        $this->assertSame(2, (int) $row->total_absence);
    }

    public function test_add_new_attendance_record_creates_row(): void
    {
        $service = new AttendanceRecordSyncService(new \App\Models\AttendanceRecord());
        $service->addNewAttendanceRecord(1, 10, 'S1', 'present', 'Fall', '2025-2026', 1);

        $this->assertDatabaseHas('attendance_record', [
            'student_id' => 10,
            'total_presence' => 1,
            'total_attendance' => 1,
        ]);
    }
}
