<?php

namespace Tests\Unit\Services\Attendance;

use App\Models\User;
use App\Services\Attendance\TeacherAttendanceSubmissionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class TeacherAttendanceSubmissionServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_submit_requires_attendance_data(): void
    {
        DB::table('classSection')->insert([
            'id' => 1,
            'class_id' => 1,
            'class_section_id' => 10,
            'class_section_name' => '1A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $user = $this->seedUser();

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

        $service = new TeacherAttendanceSubmissionService(
            new \App\Models\AttendanceDay(),
            new \App\Models\AttendanceData(),
            new \App\Models\TeacherClass(),
            new \App\Models\Student(),
            $policy,
            new \App\Services\Attendance\StudentAttendanceWriterService(
                new \App\Models\AttendanceData(),
                new \App\Models\Student(),
                new \App\Services\Attendance\AttendanceRecordSyncService(new \App\Models\AttendanceRecord())
            )
        );

        $this->expectException(\RuntimeException::class);

        $service->submit(
            $user,
            ['class_section_id' => 10],
            fn () => false,
            fn () => 'Fall',
            fn () => '2025-2026'
        );
    }

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