<?php

namespace Tests\Feature;

use App\Models\Account;
use App\Models\Currency;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AccountBalanceTest extends TestCase
{
    use RefreshDatabase;

    public function test_account_summary_displays_current_balances(): void
    {
        $lkr = Currency::create(['code' => 'LKR', 'name' => 'Sri Lankan Rupee']);
        $usd = Currency::create(['code' => 'USD', 'name' => 'US Dollar']);

        Account::create([
            'name' => 'Main Cash',
            'currency_id' => $lkr->id,
            'type' => 'cash',
            'balance' => 25000,
        ]);

        Account::create([
            'name' => 'USD Bank',
            'currency_id' => $usd->id,
            'type' => 'bank',
            'balance' => 1000.5,
        ]);

        $response = $this
            ->withSession(['admin_logged_in' => true])
            ->get(route('accounts.summary'));

        $response
            ->assertOk()
            ->assertSee('Main Cash')
            ->assertSee('LKR')
            ->assertSee('25,000.0000')
            ->assertSee('USD Bank')
            ->assertSee('USD')
            ->assertSee('1,000.5000');
    }

    public function test_buy_reduces_and_sell_increases_lkr_account_balance(): void
    {
        $lkr = Currency::create(['code' => 'LKR', 'name' => 'Sri Lankan Rupee']);
        $usd = Currency::create(['code' => 'USD', 'name' => 'US Dollar']);

        $account = Account::create([
            'name' => 'Main Cash',
            'currency_id' => $lkr->id,
            'type' => 'cash',
            'balance' => 100000,
        ]);

        $usdAccount = Account::create([
            'name' => 'USD Cash',
            'currency_id' => $usd->id,
            'type' => 'cash',
            'balance' => 100,
        ]);

        // Establish average buy rate of 300
        Transaction::create([
            'type' => 'buy',
            'currency_id' => $usd->id,
            'quantity' => 100,
            'rate' => 300,
            'payment_amount' => 30000,
            'payment_currency_id' => $lkr->id,
            'payment_account_id' => $account->id,
            'account_id' => $usdAccount->id,
        ]);

        // Reset balance for test assertions
        $account->update(['balance' => 100000]);

        $this
            ->withSession(['admin_logged_in' => true])
            ->post(route('transactions.buy.store'), [
                'currency_id' => $usd->id,
                'payment_account_id' => $account->id,
                'target_account_id' => $usdAccount->id,
                'quantity' => 10,
                'payment_amount' => 3200, // 10 USD at 320 rate
                'handling_fee' => 0,
            ]);

        $account->refresh();
        $this->assertEquals(96800.0000, (float) $account->balance);

        $this
            ->withSession(['admin_logged_in' => true])
            ->post(route('transactions.sell.store'), [
                'currency_id' => $usd->id,
                'source_account_id' => $usdAccount->id,
                'target_account_id' => $account->id,
                'quantity' => 5,
                'payment_amount' => 1650, // 5 USD at 330 rate
                'handling_fee' => 0,
            ]);

        $account->refresh();
        $this->assertEquals(98450.0000, (float) $account->balance);
    }
}
