<?php

namespace Tests\Unit\Services\Parents;

use App\Services\Parents\ParentConfigService;
use App\Services\Parents\ParentEnrollmentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ParentEnrollmentServiceTest extends TestCase
{
    use RefreshDatabase;

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

        $studentId = DB::table('students')->insertGetId([
            'school_id' => 'S-200',
            'firstname' => 'Lana',
            'lastname' => 'Parent',
            'dob' => '2013-09-01',
            'age' => 12,
            'gender' => 'Female',
            'photo_consent' => 1,
            'parent_id' => $parentId,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        DB::table('enrollments')->insert([
            'student_id' => $studentId,
            'parent_id' => $parentId,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'enrollment_date' => '2025-09-01',
            'enrollment_status' => 'enrolled',
            'admission_status' => 'accepted',
            'is_withdrawn' => 0,
        ]);

        $service = new ParentEnrollmentService(new ParentConfigService());
        $result = $service->overview($parentId, '2025-2026');

        $this->assertSame('2025-2026', $result['selectedYear']);
        $this->assertCount(1, $result['students']);
        $this->assertSame('enrolled', $result['students'][0]['enrollment_status']);
    }

    private function seedParent(): int
    {
        return DB::table('users')->insertGetId([
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '1234567890',
            'email' => 'parent3@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 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' => 'enrollment_deadline', 'config_value' => '2025-09-01'],
            ['config_key' => 'refund_deadline', 'config_value' => '2025-09-15'],
            ['config_key' => 'fall_semester_start', 'config_value' => '2025-09-05'],
        ]);
    }
}
