<?php

namespace Tests\Unit\Services\Fees;

use App\Services\Fees\FeeRefundDetailService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class FeeRefundDetailServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_calculate_refund_details_returns_amount(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
            ['config_key' => 'book_price', 'config_value' => '20'],
            ['config_key' => 'first_student_fee', 'config_value' => '350'],
            ['config_key' => 'second_student_fee', 'config_value' => '200'],
        ]);

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

        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 FeeRefundDetailService();
        $result = $service->calculateRefundDetails([
            [
                'student_id' => 1,
                'enrollment_status' => 'withdrawn',
                'withdrawal_date' => '2025-01-15',
            ],
        ], 10);

        $this->assertTrue($result['ok']);
        $this->assertSame(80.0, $result['refund_amount']);
        $this->assertTrue($result['eligible']);
    }
}
