<?php

namespace Tests\Unit\Services\Invoices;

use App\Services\Invoices\InvoiceConfigService;
use App\Services\Invoices\InvoiceGenerationService;
use App\Services\Invoices\InvoiceGradeService;
use App\Services\Invoices\InvoiceTuitionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class InvoiceGenerationServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_generate_invoice_creates_new_invoice(): void
    {
        DB::table('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'parent@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',
        ]);

        DB::table('students')->insert([
            'id' => 100,
            'parent_id' => 10,
            'firstname' => 'Kid',
            'lastname' => 'User',
            'school_id' => 'S1',
            'age' => 8,
            'gender' => 'Male',
            'photo_consent' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'is_active' => 1,
        ]);

        DB::table('classSection')->insert([
            'class_section_id' => 1,
            'class_section_name' => '1A',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('student_class')->insert([
            'student_id' => 100,
            'class_section_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_event_only' => 0,
        ]);

        DB::table('enrollments')->insert([
            'student_id' => 100,
            'parent_id' => 10,
            'class_section_id' => 1,
            'enrollment_date' => '2025-01-01',
            'enrollment_status' => 'enrolled',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $config = new InvoiceConfigService();
        $grades = new InvoiceGradeService($config->getGradeFee());
        $tuition = new InvoiceTuitionService(
            $grades,
            $config->getGradeFee(),
            $config->getFirstStudentFee(),
            $config->getSecondStudentFee(),
            $config->getYouthFee(),
            $config->getTimezone()
        );
        $service = new InvoiceGenerationService($config, $tuition);

        $result = $service->generateInvoice(10, '2025-2026');

        $this->assertTrue($result['ok']);
        $this->assertNotNull($result['insert_id']);
        $this->assertDatabaseHas('invoices', [
            'id' => $result['insert_id'],
            'parent_id' => 10,
            'school_year' => '2025-2026',
        ]);
    }
}
