<?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 ReportsModuleTest extends TestCase
{
    use RefreshDatabase;

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

        // Account used for LKR payments
        $mainCash = Account::create([
            'name' => 'Main Cash',
            'currency_id' => $lkr->id,
            'type' => 'cash',
            'balance' => 50000,
        ]);

        $bank = Account::create([
            'name' => 'Bank LKR',
            'currency_id' => $lkr->id,
            'type' => 'bank',
            'balance' => 25000,
        ]);

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

        // Account used for EUR stock
        $eurAccount = Account::create([
            'name' => 'EUR Cash',
            'currency_id' => $eur->id,
            'type' => 'cash',
            'balance' => 10,
        ]);

        // Transaction 1: Buy USD (Quantity: 100, Rate: 305)
        Transaction::create([
            'type' => 'buy',
            'currency_id' => $usd->id,
            'quantity' => 100,
            'rate' => 305,
            'payment_amount' => 30500,
            'payment_currency_id' => $lkr->id,
            'payment_account_id' => $mainCash->id,
            'account_id' => $usdAccount->id,
            'handling_fee' => 100,
            'fee_currency' => 'LKR',
            'exchange_profit' => 0,
            'total_profit' => 0,
            'created_at' => now(),
        ]);

        // Transaction 2: Sell USD (Quantity: 30, Rate: 330)
        Transaction::create([
            'type' => 'sell',
            'currency_id' => $usd->id,
            'quantity' => 30,
            'rate' => 330,
            'payment_amount' => 9900,
            'payment_currency_id' => $lkr->id,
            'payment_account_id' => $mainCash->id,
            'account_id' => $usdAccount->id,
            'handling_fee' => 50,
            'fee_currency' => 'LKR',
            'exchange_profit' => 750,
            'total_profit' => 750,
            'created_at' => now(),
        ]);

        // Transaction 3: Buy EUR (Quantity: 10, Rate: 350)
        Transaction::create([
            'type' => 'buy',
            'currency_id' => $eur->id,
            'quantity' => 10,
            'rate' => 350,
            'payment_amount' => 3500,
            'payment_currency_id' => $lkr->id,
            'payment_account_id' => $bank->id,
            'account_id' => $eurAccount->id,
            'handling_fee' => 0,
            'fee_currency' => 'LKR',
            'exchange_profit' => 0,
            'total_profit' => 0,
            'created_at' => now()->subDay(),
        ]);

        $this->withSession(['admin_logged_in' => true])
            ->get(route('reports.currencies'))
            ->assertOk()
            ->assertSee('USD')
            ->assertSee('100.0000')
            ->assertSee('30.0000')
            ->assertSee('70.0000')
            ->assertSee('305.0000')
            ->assertSee('330.0000');

        $this->withSession(['admin_logged_in' => true])
            ->get(route('reports.profits'))
            ->assertOk()
            ->assertSee('Total Exchange Profit')
            ->assertSee('750.0000');

        $this->withSession(['admin_logged_in' => true])
            ->get(route('reports.handling-fees'))
            ->assertOk()
            ->assertSee('150.00')
            ->assertSee('LKR');

        $this->withSession(['admin_logged_in' => true])
            ->get(route('reports.accounts'))
            ->assertOk()
            ->assertSee('Main Cash')
            ->assertSee('50,000.0000')
            ->assertSee('Transaction History');

        $this->withSession(['admin_logged_in' => true])
            ->get(route('reports.accounts', [
                'date' => now()->toDateString(),
                'currency_id' => $usd->id,
                'account_id' => $mainCash->id,
            ]))
            ->assertOk()
            ->assertSee('USD')
            ->assertSee('Main Cash')
            ->assertSee('30,500.0000')
            ->assertSee('9,900.0000')
            ->assertDontSee('3,500.0000');
    }
}
