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

    public function test_profit_summary_displays_daily_transaction_wise_and_total_profit(): 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' => 10000,
        ]);

        Transaction::create([
            'type' => 'sell',
            'currency_id' => $usd->id,
            'quantity' => 10,
            'rate' => 325,
            'amount_lkr' => 3300,
            'handling_fee' => 50,
            'fee_currency' => 'LKR',
            'account_id' => $account->id,
            'exchange_profit' => 250,
            'handling_fee_profit' => 0,
            'total_profit' => 250,
            'created_at' => now(),
        ]);

        Transaction::create([
            'type' => 'buy',
            'currency_id' => $usd->id,
            'quantity' => 5,
            'rate' => 300,
            'amount_lkr' => 1450,
            'handling_fee' => 50,
            'fee_currency' => 'LKR',
            'account_id' => $account->id,
            'exchange_profit' => 0,
            'handling_fee_profit' => 0,
            'total_profit' => 0,
            'created_at' => now()->subDay(),
        ]);

        $response = $this
            ->withSession(['admin_logged_in' => true])
            ->get(route('profits.index', ['date' => now()->toDateString()]));

        $response
            ->assertOk()
            ->assertSee('Daily Exchange Profit')
            ->assertSee('250.0000')
            ->assertSee('Daily Total Profit')
            ->assertSee('250.0000')
            ->assertSee('Total Profit')
            ->assertSee('250.0000')
            ->assertSee('Transaction-wise Profit')
            ->assertSee('USD');
    }
}
