<?php

namespace Tests\Feature;

use App\Models\Branch;
use App\Models\Business;
use App\Models\Customer;
use App\Models\LoanProduct;
use App\Models\User;
use App\Services\LoanApplicationService;
use App\Services\LoanContractPdfService;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LoanPenaltyContractSnapshotTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
    }

    public function test_contract_uses_penalty_terms_frozen_on_the_loan(): void
    {
        $business = Business::firstOrFail();
        $branch = Branch::create(['business_id' => $business->id, 'name' => 'Contract Snapshot Branch', 'status' => 'active']);
        $customer = Customer::create([
            'branch_id' => $branch->id,
            'customer_number' => 'CUS-CONTRACT-PEN',
            'identity_mode' => 'without_nin',
            'first_name' => 'Mariam',
            'last_name' => 'Joseph',
            'phone' => '0756112233',
            'status' => 'active',
        ]);
        $product = LoanProduct::create([
            'business_id' => $business->id,
            'name' => 'Contract Snapshot Product',
            'minimum_amount' => '1000.00',
            'maximum_amount' => '500000.00',
            'duration' => 1,
            'duration_unit' => 'months',
            'interest_rate' => '0.000000',
            'interest_calculation_method' => 'flat',
            'repayment_frequency' => 'monthly',
            'grace_period_days' => 0,
            'late_payment_penalty_type' => 'daily_percentage',
            'late_payment_penalty_value' => '1.500000',
            'processing_fee_type' => 'fixed',
            'processing_fee_value' => '0.000000',
            'insurance_fee_type' => 'fixed',
            'insurance_fee_value' => '0.000000',
            'other_charges' => '0.00',
            'collateral_required' => false,
            'guarantor_required' => false,
            'status' => 'active',
        ]);
        $admin = User::where('username', 'admin')->firstOrFail();
        $loan = app(LoanApplicationService::class)->create([
            'branch_id' => $branch->id,
            'customer_id' => $customer->id,
            'principal' => '10000.00',
            'purpose' => 'Contract penalty snapshot test',
            'schedule_start_date' => '2026-08-12',
            'collateral_ids' => [],
        ], $product, $admin);

        $product->update([
            'late_payment_penalty_type' => 'fixed',
            'late_payment_penalty_value' => '999.000000',
        ]);

        $pdf = $this->inBranch($branch->id, fn () => app(LoanContractPdfService::class)->generate($loan->fresh(), $business));

        $this->assertStringContainsString('Late Payment Rule: Daily Percentage 1.500000', $pdf);
        $this->assertStringNotContainsString('Late Payment Rule: Fixed 999.000000', $pdf);
    }
}
