<?php

namespace Tests\Unit;

use App\Models\SavingsAccount;
use PHPUnit\Framework\TestCase;

class SavingsAccountCompletionTest extends TestCase
{
    public function test_target_savings_is_completed_when_balance_reaches_target(): void
    {
        $account = new SavingsAccount([
            'status' => 'active',
            'target_amount' => 50000,
            'planned_total_amount' => null,
        ]);

        $this->assertFalse($account->isCompletedAtBalance(49999.99));
        $this->assertTrue($account->isCompletedAtBalance(50000));
    }

    public function test_planned_savings_is_completed_when_balance_reaches_planned_total(): void
    {
        $account = new SavingsAccount([
            'status' => 'active',
            'target_amount' => null,
            'planned_total_amount' => 30000,
        ]);

        $this->assertTrue($account->isCompletedAtBalance(30000));
    }

    public function test_open_ended_savings_does_not_auto_complete(): void
    {
        $account = new SavingsAccount([
            'status' => 'active',
            'target_amount' => null,
            'planned_total_amount' => null,
        ]);

        $this->assertFalse($account->isCompletedAtBalance(1000000));
    }

    public function test_thrift_withdrawal_fee_is_deducted_from_net_payout_once(): void
    {
        $account = new SavingsAccount([
            'product_type' => 'thrift_isusu',
            'processing_fee_amount' => 1000,
            'processing_fee_timing' => 'withdrawal',
        ]);

        $this->assertSame(1000.0, $account->withdrawalProcessingFeeDue());
        $this->assertSame(29000.0, $account->netWithdrawalAmount(30000));
        $this->assertSame(0.0, $account->withdrawalProcessingFeeDue(1000));
        $this->assertSame(30000.0, $account->netWithdrawalAmount(30000, 1000));
    }

    public function test_deposit_fee_is_available_only_when_timing_is_first_deposit(): void
    {
        $account = new SavingsAccount([
            'processing_fee_amount' => 750,
            'processing_fee_timing' => 'first_deposit',
        ]);

        $this->assertSame(750.0, $account->depositProcessingFeeDue());
        $this->assertSame(0.0, $account->depositProcessingFeeDue(750));

        $account->processing_fee_timing = 'manual';
        $this->assertSame(0.0, $account->depositProcessingFeeDue());
    }
}
