<?php

namespace Tests\Unit;

use App\Models\AttendanceData;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class AttendanceDataTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        Schema::dropIfExists('attendance_data');

        Schema::create('attendance_data', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('class_id');
            $table->unsignedBigInteger('class_section_id');
            $table->unsignedBigInteger('student_id');
            $table->char('school_id', 36);
            $table->date('date');
            $table->string('status', 20)->default('present');
            $table->text('reason')->nullable();
            $table->string('is_reported', 10)->default('no');
            $table->string('is_notified', 10)->default('no');
            $table->string('semester', 25);
            $table->string('school_year', 9);
            $table->unsignedBigInteger('modified_by')->nullable();
            $table->timestamps();
        });
    }

    public function test_get_attendance_by_class_and_get_attendance(): void
    {
        AttendanceData::create([
            'class_id' => 1,
            'class_section_id' => 2,
            'school_id' => 1,
            'student_id' => 10,
            'date' => '2024-09-01',
            'status' => 'present',
            'reason' => null,
            'is_reported' => 'no',
            'is_notified' => 'no',
            'semester' => 'Fall',
            'school_year' => '2024-2025',
            'modified_by' => null,
        ]);

        AttendanceData::create([
            'class_id' => 2,
            'class_section_id' => 2,
            'school_id' => 1,
            'student_id' => 11,
            'date' => '2024-09-01',
            'status' => 'present',
            'reason' => null,
            'is_reported' => 'no',
            'is_notified' => 'no',
            'semester' => 'Fall',
            'school_year' => '2024-2025',
            'modified_by' => null,
        ]);

        $model = new AttendanceData();
        $rows = $model->getAttendanceByClass(1, 2, '2024-09-01');
        $this->assertCount(1, $rows);

        $row = $model->getAttendance(10, 2, '2024-09-01');
        $this->assertNotNull($row);
        $this->assertSame(10, $row['student_id']);
    }

    public function test_unreported_term_rows_filters_unreported(): void
    {
        DB::table('attendance_data')->insert([
            [
                'class_id' => 1,
                'class_section_id' => 1,
                'school_id' => 1,
                'student_id' => 1,
                'date' => '2024-09-01',
                'status' => 'absent',
                'reason' => null,
                'is_reported' => 'no',
                'is_notified' => 'no',
                'semester' => 'Fall',
                'school_year' => '2024-2025',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'class_id' => 1,
                'class_section_id' => 1,
                'school_id' => 1,
                'student_id' => 2,
                'date' => '2024-09-02',
                'status' => 'absent',
                'reason' => null,
                'is_reported' => 'yes',
                'is_notified' => 'no',
                'semester' => 'Fall',
                'school_year' => '2024-2025',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'class_id' => 1,
                'class_section_id' => 1,
                'school_id' => 1,
                'student_id' => 3,
                'date' => '2024-09-03',
                'status' => 'absent',
                'reason' => null,
                'is_reported' => 'no',
                'is_notified' => 'no',
                'semester' => 'Spring',
                'school_year' => '2024-2025',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        $model = new AttendanceData();
        $rows = $model->unreportedTermRows([1, 2, 3], '2024-2025', 'Fall');

        $this->assertCount(1, $rows);
        $this->assertSame(1, $rows[0]['student_id']);
    }

    public function test_get_unreported_absences_and_lates_groups_rows(): void
    {
        DB::table('attendance_data')->insert([
            [
                'class_id' => 1,
                'class_section_id' => 1,
                'school_id' => 1,
                'student_id' => 10,
                'date' => '2024-09-01',
                'status' => 'absent',
                'reason' => null,
                'is_reported' => 'no',
                'is_notified' => 'no',
                'semester' => 'Fall',
                'school_year' => '2024-2025',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'class_id' => 1,
                'class_section_id' => 1,
                'school_id' => 1,
                'student_id' => 10,
                'date' => '2024-09-02',
                'status' => 'late',
                'reason' => null,
                'is_reported' => 'no',
                'is_notified' => 'no',
                'semester' => 'Fall',
                'school_year' => '2024-2025',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'class_id' => 1,
                'class_section_id' => 1,
                'school_id' => 1,
                'student_id' => 10,
                'date' => '2024-09-03',
                'status' => 'absent',
                'reason' => null,
                'is_reported' => 'yes',
                'is_notified' => 'no',
                'semester' => 'Fall',
                'school_year' => '2024-2025',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        $model = new AttendanceData();
        $rows = $model->getUnreportedAbsencesAndLates('2024-2025', 'Fall');

        $this->assertArrayHasKey(10, $rows);
        $this->assertSame(1, $rows[10]['counts']['absences']);
        $this->assertSame(1, $rows[10]['counts']['lates']);
        $this->assertSame(2, $rows[10]['counts']['total']);
    }

    public function test_mark_reported_and_notified_updates_flags(): void
    {
        DB::table('attendance_data')->insert([
            'class_id' => 1,
            'class_section_id' => 1,
            'school_id' => 1,
            'student_id' => 20,
            'date' => '2024-09-10',
            'status' => 'absent',
            'reason' => null,
            'is_reported' => 'no',
            'is_notified' => 'no',
            'semester' => 'Fall',
            'school_year' => '2024-2025',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $model = new AttendanceData();
        $result = $model->markReportedAndNotified(20, ['2024-09-10'], 'Fall', '2024-2025');

        $this->assertTrue($result);

        $row = DB::table('attendance_data')->where('student_id', 20)->first();
        $this->assertSame('yes', $row->is_reported);
        $this->assertSame('yes', $row->is_notified);
    }
}
