<?php

namespace Tests\Unit\Services\Teachers;

use App\Libraries\StaffTimeOffLinkService;
use App\Services\SemesterRangeService;
use App\Services\Semesters\SemesterConfigService;
use App\Services\Teachers\TeacherAbsenceService;
use App\Services\Teachers\TeacherConfigService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class TeacherAbsenceServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_form_data_returns_available_dates(): void
    {
        Carbon::setTestNow(Carbon::parse('2025-10-01'));
        $this->seedConfig();
        $teacherId = $this->seedTeacherUser();

        $service = new TeacherAbsenceService(
            new TeacherConfigService(),
            new SemesterRangeService(new SemesterConfigService()),
            new StaffTimeOffLinkService()
        );

        $data = $service->formData($teacherId);

        $this->assertSame('Fall', $data['semester']);
        $this->assertNotEmpty($data['available_dates']);
        Carbon::setTestNow();
    }

    public function test_submit_creates_staff_attendance(): void
    {
        Carbon::setTestNow(Carbon::parse('2025-10-01'));
        $this->seedConfig();
        $teacherId = $this->seedTeacherUser();

        $service = new TeacherAbsenceService(
            new TeacherConfigService(),
            new SemesterRangeService(new SemesterConfigService()),
            new StaffTimeOffLinkService()
        );

        $date = $service->formData($teacherId)['available_dates'][0];

        $result = $service->submit($teacherId, [
            'dates' => [$date],
            'reason' => 'Family event',
            'reason_type' => 'personal',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('staff_attendance', [
            'user_id' => $teacherId,
            'date' => $date,
            'status' => 'absent',
        ]);
        Carbon::setTestNow();
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
            ['config_key' => 'fall_semester_start', 'config_value' => '2025-09-01'],
            ['config_key' => 'spring_semester_start', 'config_value' => '2026-01-15'],
            ['config_key' => 'last_school_day', 'config_value' => '2026-06-15'],
        ]);
    }

    private function seedTeacherUser(): int
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'teacher',
        ]);

        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Nina',
            'lastname' => 'Teacher',
            'cellphone' => '3333333333',
            'email' => 'teacher.absence@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $userId,
            'role_id' => $roleId,
        ]);

        return $userId;
    }
}
