<?php

namespace Tests\Unit\Services\Fees;

use App\Services\Fees\FeeConfigService;
use App\Services\Fees\FeeGradeService;
use App\Services\Fees\FeeStudentFeeService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class FeeStudentFeeServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_total_tuition_fee_applies_tiers_and_youth_fee(): void
    {
        $this->seedFeeConfig();
        $this->seedClassSection(101, '3');
        $this->seedClassSection(102, '10');

        $service = $this->makeService();

        $total = $service->totalTuitionFee([
            ['class_section_id' => 101],
            ['class_section_id' => 102],
        ]);

        $this->assertSame(530.0, $total);
    }

    public function test_assign_fees_stores_tuition_fee_and_fee_category_on_each_student(): void
    {
        $this->seedFeeConfig();
        $this->seedClassSection(101, '2');
        $this->seedClassSection(102, '5');
        $this->seedClassSection(103, '10');

        $service = $this->makeService();

        $assigned = $service->assignFees([
            ['student_id' => 1, 'class_section_id' => 101],
            ['student_id' => 2, 'class_section_id' => 102],
            ['student_id' => 3, 'class_section_id' => 103],
        ]);

        $byId = [];
        foreach ($assigned as $row) {
            $byId[$row['student_id']] = $row;
        }

        $this->assertSame(350.0, $byId[1]['tuition_fee']);
        $this->assertSame('first_regular', $byId[1]['fee_category']);

        $this->assertSame(200.0, $byId[2]['tuition_fee']);
        $this->assertSame('additional_regular', $byId[2]['fee_category']);

        $this->assertSame(180.0, $byId[3]['tuition_fee']);
        $this->assertSame('youth', $byId[3]['fee_category']);
    }

    public function test_calculate_breakdown_includes_billable_and_non_billable_students(): void
    {
        $this->seedFeeConfig();
        $this->seedClassSection(201, '3');
        $this->seedClassSection(202, '4');

        $service = $this->makeService();

        $breakdown = $service->calculateBreakdown([
            [
                'student_id' => 1,
                'firstname' => 'Ada',
                'lastname' => 'L',
                'class_section_id' => 201,
                'enrollment_status' => 'enrolled',
                'admission_status' => 'accepted',
            ],
            [
                'student_id' => 2,
                'firstname' => 'Bo',
                'lastname' => 'L',
                'class_section_id' => 202,
                'enrollment_status' => 'withdrawn',
                'admission_status' => 'accepted',
            ],
            [
                'student_id' => 3,
                'firstname' => 'Cy',
                'lastname' => 'L',
                'class_section_id' => 201,
                'enrollment_status' => 'enrolled',
                'admission_status' => 'pending',
            ],
        ]);

        $this->assertSame(350.0, $breakdown['family_total']);
        $this->assertSame(1, $breakdown['billable_count']);
        $this->assertSame(2, $breakdown['non_billable_count']);
        $this->assertCount(3, $breakdown['students']);

        $byId = [];
        foreach ($breakdown['students'] as $row) {
            $byId[$row['student_id']] = $row;
        }

        $this->assertSame('first_regular', $byId[1]['fee_category']);
        $this->assertSame(350.0, $byId[1]['tuition_fee']);

        $this->assertSame('not_billable', $byId[2]['fee_category']);
        $this->assertSame(0.0, $byId[2]['tuition_fee']);

        $this->assertSame('not_billable', $byId[3]['fee_category']);
    }

    public function test_is_billable_and_is_withdrawn_classifiers(): void
    {
        $service = $this->makeService();

        $this->assertTrue($service->isBillable([
            'enrollment_status' => 'enrolled',
            'admission_status' => 'accepted',
        ]));
        $this->assertTrue($service->isBillable([
            'enrollment_status' => 'payment pending',
            'admission_status' => 'accepted',
        ]));
        $this->assertFalse($service->isBillable([
            'enrollment_status' => 'enrolled',
            'admission_status' => 'pending',
        ]));

        $this->assertTrue($service->isWithdrawn(['enrollment_status' => 'withdrawn']));
        $this->assertTrue($service->isWithdrawn(['enrollment_status' => 'refund pending']));
        $this->assertTrue($service->isWithdrawn(['enrollment_status' => 'withdraw under review']));
        $this->assertFalse($service->isWithdrawn(['enrollment_status' => 'enrolled']));
    }

    private function makeService(): FeeStudentFeeService
    {
        return new FeeStudentFeeService(new FeeGradeService(), new FeeConfigService());
    }

    private function seedFeeConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'first_student_fee', 'config_value' => '350'],
            ['config_key' => 'second_student_fee', 'config_value' => '200'],
            ['config_key' => 'youth_fee', 'config_value' => '180'],
        ]);
    }

    private function seedClassSection(int $sectionId, string $gradeName): void
    {
        DB::table('classSection')->insert([
            'class_id' => $sectionId,
            'class_section_id' => $sectionId,
            'class_section_name' => $gradeName,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }
}
