<?php

namespace Tests\Unit\Services\Parents;

use App\Services\Parents\ParentAttendanceReportCalendarService;
use App\Services\Parents\ParentAttendanceReportService;
use App\Services\Parents\ParentConfigService;
use App\Services\SemesterRangeService;
use App\Services\Semesters\SemesterConfigService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ParentAttendanceReportServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_form_data_returns_students(): void
    {
        $this->seedConfig();
        $parentId = $this->seedParent();
        $this->seedStudent($parentId);

        $service = new ParentAttendanceReportService(
            new ParentConfigService(),
            new ParentAttendanceReportCalendarService(new ParentConfigService()),
            new SemesterRangeService(new SemesterConfigService())
        );

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

        $this->assertSame($parentId, $data['students'][0]['parent_id']);
        $this->assertNotEmpty($data['sundays']);
    }

    private function seedParent(): int
    {
        return DB::table('users')->insertGetId([
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '1234567890',
            'email' => 'parent.report.service@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',
        ]);
    }

    private function seedStudent(int $parentId): void
    {
        DB::table('students')->insert([
            'school_id' => 'S-400',
            'firstname' => 'Casey',
            'lastname' => 'Parent',
            'dob' => '2014-09-01',
            'age' => 11,
            'gender' => 'Female',
            'photo_consent' => 1,
            'parent_id' => $parentId,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
            ['config_key' => 'date_age_reference', 'config_value' => '2025-12-31'],
            ['config_key' => 'parent_report_cutoff', 'config_value' => '23:59'],
            ['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'],
        ]);
    }
}
