<?php

namespace Tests\Feature;

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

class DisbursementErrorMessageTest extends TestCase
{
    use RefreshDatabase;

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

    public function test_opening_disbursement_for_non_ready_loan_redirects_with_readable_error_message(): void
    {
        [$loan, $admin] = $this->loan('rejected');
        $message = 'Only an undisbursed loan that is Ready for Disbursement can be posted.';

        $this->actingAs($admin)
            ->get(route('loan-disbursements.create', $loan))
            ->assertRedirect(route('loan-disbursements.index'))
            ->assertSessionHas('error', $message);
    }

    public function test_disbursement_page_displays_the_exact_error_message_instead_of_exception_output(): void
    {
        [$loan, $admin] = $this->loan('active');
        $message = 'Only an undisbursed loan that is Ready for Disbursement can be posted.';

        $this->followingRedirects()
            ->actingAs($admin)
            ->get(route('loan-disbursements.create', $loan))
            ->assertOk()
            ->assertSee($message)
            ->assertDontSee('Symfony\\Component\\HttpKernel\\Exception\\HttpException');
    }

    /** @return array{0:Loan,1:User} */
    private function loan(string $status): array
    {
        $business = Business::firstOrFail();
        $branch = Branch::create([
            'business_id' => $business->id,
            'name' => 'Disbursement Message Branch',
            'location' => 'Dodoma',
            'status' => 'active',
        ]);
        $customer = Customer::create([
            'branch_id' => $branch->id,
            'customer_number' => 'CUS-DSB-MSG-001',
            'identity_mode' => 'without_nin',
            'first_name' => 'Neema',
            'last_name' => 'Juma',
            'phone' => '0712000000',
            'status' => 'active',
        ]);
        $product = LoanProduct::create([
            'business_id' => $business->id,
            'name' => 'Disbursement Message Product',
            'minimum_amount' => '1000.00',
            'maximum_amount' => '1000000.00',
            'duration' => 1,
            'duration_unit' => 'months',
            'interest_rate' => '10.000000',
            'interest_calculation_method' => 'flat',
            'repayment_frequency' => 'monthly',
            'grace_period_days' => 0,
            'late_payment_penalty_type' => 'none',
            'late_payment_penalty_value' => '0.000000',
            'processing_fee_type' => 'fixed',
            'processing_fee_value' => '0.000000',
            'processing_fee_payment_mode' => 'financed',
            'insurance_fee_type' => 'fixed',
            'insurance_fee_value' => '0.000000',
            'other_charges' => '0.00',
            'collateral_required' => false,
            'guarantor_required' => false,
            'status' => 'active',
        ]);
        $admin = User::where('email', 'admin@microfinance.local')->firstOrFail();
        $loan = Loan::create([
            'branch_id' => $branch->id,
            'customer_id' => $customer->id,
            'loan_product_id' => $product->id,
            'sequence_number' => 1,
            'sequence_year' => 2026,
            'loan_number' => 'LN-DSB-MSG-2026-000001',
            'principal' => '100000.00',
            'interest' => '10000.00',
            'processing_fee' => '0.00',
            'processing_fee_payment_mode' => 'financed',
            'insurance_fee' => '0.00',
            'other_charges' => '0.00',
            'fees' => '0.00',
            'total_repayment' => '110000.00',
            'purpose' => 'Business stock',
            'schedule_start_date' => '2026-08-13',
            'status' => $status,
            'created_by' => $admin->id,
            'updated_by' => $admin->id,
        ]);

        return [$loan, $admin];
    }
}
