<?php

namespace Tests\Unit\Services\Attendance;

use App\Models\AttendanceData;
use App\Models\AttendanceDay;
use App\Models\ClassSection;
use App\Models\Configuration;
use App\Models\User;
use App\Models\UserRole;
use App\Services\Attendance\AttendancePolicyService;
use App\Services\Attendance\AttendanceRecordSyncService;
use App\Services\Attendance\AttendanceService;
use App\Services\Attendance\StudentAttendanceWriterService;
use App\Services\Attendance\TeacherAttendanceSubmissionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class AttendanceServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_update_attendance_management_throws_when_locked(): void
    {
        $user = $this->seedUser();

        $policy = Mockery::mock(AttendancePolicyService::class);
        $policy->shouldReceive('canEditDay')->andReturn(false);

        $writer = Mockery::mock(StudentAttendanceWriterService::class);
        $teacherSubmission = Mockery::mock(TeacherAttendanceSubmissionService::class);
        $recordSync = Mockery::mock(AttendanceRecordSyncService::class);

        $service = new AttendanceService(
            new Configuration(),
            new AttendanceData(),
            new AttendanceDay(),
            new ClassSection(),
            new UserRole(),
            $policy,
            $writer,
            $teacherSubmission,
            $recordSync
        );

        $this->expectException(\RuntimeException::class);
        $service->updateAttendanceManagement($user, [
            'class_section_id' => 1,
            'student_id' => 1,
            'status' => 'present',
            'date' => '2025-01-01',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }

    public function test_update_attendance_management_saves_row(): void
    {
        $user = $this->seedUser();

        $policy = Mockery::mock(AttendancePolicyService::class);
        $policy->shouldReceive('canEditDay')->andReturn(true);

        $writer = Mockery::mock(StudentAttendanceWriterService::class);
        $writer->shouldReceive('saveOrUpdateStudentAttendance')->once();

        $teacherSubmission = Mockery::mock(TeacherAttendanceSubmissionService::class);
        $recordSync = Mockery::mock(AttendanceRecordSyncService::class);

        $service = new AttendanceService(
            new Configuration(),
            new AttendanceData(),
            new AttendanceDay(),
            new ClassSection(),
            new UserRole(),
            $policy,
            $writer,
            $teacherSubmission,
            $recordSync
        );

        $result = $service->updateAttendanceManagement($user, [
            'class_section_id' => 1,
            'student_id' => 1,
            'status' => 'present',
            'date' => '2025-01-01',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'class_id' => 1,
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('attendance_day', [
            'class_section_id' => 1,
            'date' => '2025-01-01 00:00:00',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }

    private function seedUser(): User
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Test',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'test@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',
        ]);

        return User::query()->findOrFail(1);
    }
}
