<?php

namespace Tests\Unit\Services\Refunds;

use App\Models\Invoice;
use App\Services\Discounts\DiscountInvoiceService;
use App\Services\Refunds\RefundInvoiceAdjustmentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class RefundInvoiceAdjustmentServiceTest extends TestCase
{
    use RefreshDatabase;

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

        $invoice = Invoice::query()->findOrFail(1);

        $invoiceService = Mockery::mock(DiscountInvoiceService::class);
        $invoiceService->shouldReceive('recalculateInvoice')->once()->with(1, '2025-2026');

        $service = new RefundInvoiceAdjustmentService($invoiceService);

        $result = $service->applyBookChargeAndUpdateInvoice($invoice, 10, [
            'eligible' => true,
            'book_price' => 15.00,
        ], 'Fall', 1);

        $this->assertSame('inserted', $result['book_charge_action']);
        $this->assertDatabaseHas('additional_charges', [
            'invoice_id' => 1,
            'title' => 'Book Fee (Non-Refundable)',
            'amount' => 15.00,
        ]);
    }
}
