<?php

namespace Tests\Unit\Services\AttendanceManagement;

use App\Services\AttendanceManagement\AttendanceManagementService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class AttendanceManagementServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_badge_scan_persists_school_year_and_semester_for_the_event_date(): void
    {
        $this->seedConfiguration();
        $this->seedUserWithBadge('RFID-100');

        $service = new AttendanceManagementService();

        $row = $service->badgeScan([
            'badge_scan' => 'RFID-100',
            'scan_time' => '2026-02-10 08:30:00',
        ]);

        $this->assertSame('2025-2026', $row['school_year']);
        $this->assertSame('Spring', $row['semester']);
        $this->assertDatabaseHas('attendance_management_events', [
            'id' => $row['id'],
            'badge_id' => 'RFID-100',
            'school_year' => '2025-2026',
            'semester' => 'Spring',
        ]);
    }

    public function test_dashboard_derives_academic_context_for_legacy_rows_and_applies_filters(): void
    {
        DB::table('attendance_management_events')->insert([
            'person_type' => 'student',
            'person_id' => 10,
            'person_name' => 'Legacy Student',
            'event_date' => '2026-02-10',
            'attendance_status' => AttendanceManagementService::STATUS_PRESENT,
            'report_status' => 'reported',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new AttendanceManagementService();

        $data = $service->dashboard([
            'date' => '2026-02-10',
            'school_year' => '2025-2026',
            'semester' => 'Spring',
        ]);

        $this->assertCount(1, $data['rows']);
        $this->assertSame('2025-2026', $data['rows'][0]['school_year']);
        $this->assertSame('Spring', $data['rows'][0]['semester']);
        $this->assertSame(1, $data['summary']['present']);

        $empty = $service->dashboard([
            'date' => '2026-02-10',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        $this->assertCount(0, $empty['rows']);
    }

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

    private function seedUserWithBadge(string $rfidTag): void
    {
        DB::table('users')->insert([
            'id' => 100,
            'school_id' => 1,
            'firstname' => 'Badge',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'badge-user@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',
            'rfid_tag' => $rfidTag,
        ]);
    }
}
