<?php

namespace Tests\Feature\BranchIsolation;

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

class Phase8LoanApplicationScopeTest extends TestCase
{
    use RefreshDatabase;

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

    public function test_primary_only_user_keeps_loan_list_and_create_scope_without_pivot_assignment(): void
    {
        $business = Business::firstOrFail();
        $branchA = Branch::create(['business_id'=>$business->id,'name'=>'Phase 8 Loan App A','status'=>'active']);
        $branchB = Branch::create(['business_id'=>$business->id,'name'=>'Phase 8 Loan App B','status'=>'active']);

        $role = Role::create(['name'=>'phase8-loan-app-scope','display_name'=>'Phase 8 Loan App Scope']);
        $role->permissions()->attach(Permission::whereIn('name',['loans.view','loans.create'])->pluck('id'));

        $user = User::factory()->create([
            'username'=>'phase8_loan_app_primary_only',
            'status'=>'active',
            'primary_branch_id'=>$branchA->id,
        ]);
        $user->roles()->attach($role->id);
        $this->assertSame([], $user->branches()->pluck('branches.id')->all());

        $product = LoanProduct::create([
            'business_id'=>$business->id,'name'=>'Phase 8 Loan App Product','minimum_amount'=>'1000.00','maximum_amount'=>'1000000.00',
            'duration'=>2,'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','insurance_fee_type'=>'fixed','insurance_fee_value'=>'0.000000','other_charges'=>'0.00',
            'collateral_required'=>false,'guarantor_required'=>false,'status'=>'active',
        ]);

        $customerA = Customer::factory()->create(['branch_id'=>$branchA->id,'customer_number'=>'CUS-P8-LOAN-A']);
        $customerB = Customer::factory()->create(['branch_id'=>$branchB->id,'customer_number'=>'CUS-P8-LOAN-B']);
        $this->loan($branchA,$customerA,$product,'LN-P8-LOAN-A',1);
        $this->loan($branchB,$customerB,$product,'LN-P8-LOAN-B',2);

        $this->actingAs($user)->get(route('loans.index'))
            ->assertOk()
            ->assertSee('LN-P8-LOAN-A')
            ->assertDontSee('LN-P8-LOAN-B')
            ->assertSee($branchA->name)
            ->assertDontSee($branchB->name);

        $this->actingAs($user)->get(route('loans.create'))
            ->assertOk()
            ->assertSee($branchA->name)
            ->assertDontSee($branchB->name)
            ->assertSee('CUS-P8-LOAN-A')
            ->assertDontSee('CUS-P8-LOAN-B');
    }

    private function loan(Branch $branch, Customer $customer, LoanProduct $product, string $number, int $sequence): Loan
    {
        return Loan::create([
            'branch_id'=>$branch->id,'customer_id'=>$customer->id,'loan_product_id'=>$product->id,'sequence_number'=>$sequence,
            'sequence_year'=>2026,'loan_number'=>$number,'principal'=>'10000.00','interest'=>'1000.00','processing_fee'=>'0.00',
            'insurance_fee'=>'0.00','other_charges'=>'0.00','fees'=>'0.00','total_repayment'=>'11000.00','principal_outstanding'=>'10000.00',
            'interest_outstanding'=>'1000.00','fees_outstanding'=>'0.00','penalties_outstanding'=>'0.00','total_outstanding'=>'11000.00','total_paid'=>'0.00',
            'purpose'=>'Phase 8 loan application scope','schedule_start_date'=>'2026-08-01','status'=>'draft',
        ]);
    }
}
