<?php

namespace Tests\Unit\Services\Refunds;

use App\Services\Refunds\RefundInvoiceAdjustmentService;
use App\Services\Refunds\RefundPolicyService;
use App\Services\Fees\FeeRefundDetailService;
use App\Services\Discounts\DiscountInvoiceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class RefundPolicyServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_process_withdrawal_refund_creates_refund(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
            ['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
            ['config_key' => 'book_price', 'config_value' => '10'],
            ['config_key' => 'grade_fee', 'config_value' => '9'],
            ['config_key' => 'first_student_fee', 'config_value' => '350'],
            ['config_key' => 'second_student_fee', 'config_value' => '200'],
            ['config_key' => 'youth_fee', 'config_value' => '180'],
        ]);

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

        DB::table('students')->insert([
            'id' => 1,
            'parent_id' => 10,
            'firstname' => 'Kid',
            'lastname' => 'One',
            'school_year' => '2025-2026',
        ]);

        DB::table('enrollments')->insert([
            'id' => 1,
            'student_id' => 1,
            'parent_id' => 10,
            'class_section_id' => 101,
            'enrollment_status' => 'withdrawn',
            'withdrawal_date' => '2025-01-15',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'admission_status' => 'accepted',
            'is_withdrawn' => 1,
        ]);

        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-1',
            'total_amount' => 350.00,
            'balance' => 0.00,
            'paid_amount' => 100.00,
            'status' => 'Paid',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'issue_date' => '2025-01-05',
        ]);

        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'paid_amount' => 100.00,
            'total_amount' => 100.00,
            'balance' => 0.00,
            'payment_date' => '2025-01-10',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'status' => 'Paid',
        ]);

        $service = new RefundPolicyService(
            new FeeRefundDetailService(),
            new RefundInvoiceAdjustmentService(new DiscountInvoiceService())
        );

        $result = $service->processWithdrawalRefund(10, 1, '2025-2026', 'Fall', 1);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('refunds', [
            'parent_id' => 10,
            'invoice_id' => 1,
            'request' => 'tuition',
        ]);
        $this->assertDatabaseHas('additional_charges', [
            'invoice_id' => 1,
            'title' => 'Book Fee (Non-Refundable)',
        ]);
    }
}
