<?php

namespace Tests\Unit\Services\Refunds;

use App\Services\Refunds\RefundNotificationService;
use App\Services\Refunds\RefundOverpaymentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class RefundOverpaymentServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_recalculate_creates_overpayment_refund(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-1',
            'total_amount' => 100.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' => 150.00,
            'total_amount' => 150.00,
            'balance' => 0.00,
            'payment_date' => '2025-01-10',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'status' => 'Paid',
        ]);

        $service = new RefundOverpaymentService(new RefundNotificationService());
        $result = $service->recalculate(false, null, 1);

        $this->assertNotEmpty($result['created_ids']);
        $this->assertDatabaseHas('refunds', [
            'parent_id' => 10,
            'request' => 'overpayment',
        ]);
    }
}
