<?php

namespace Tests\Feature\Admin;

use App\Enums\InvestmentStatus;
use App\Enums\InvestmentTransactionType;
use App\Models\Investment;
use App\Models\InvestmentSetting;
use App\Models\InvestmentTransaction;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PlatformAccrualDebugTest extends TestCase
{
    use RefreshDatabase;

    public function test_debug_accrual_endpoint_is_hidden_when_debug_disabled(): void
    {
        config(['app.allow_adjustments' => false]);

        $admin = User::factory()->admin()->create();

        $this->actingAs($admin)
            ->post(route('admin.platform-accrual.debug-run'))
            ->assertNotFound();
    }

    public function test_debug_accrual_runs_for_admin_when_debug_enabled(): void
    {
        config(['app.allow_adjustments' => true]);
        InvestmentSetting::current();

        $admin = User::factory()->admin()->create();
        $investor = User::factory()->create();

        $investment = $investor->investments()->create([
            'amount' => 1000,
            'duration_months' => 12,
            'monthly_return_snapshot' => 3,
            'txid' => 'tx-debug-accrual',
            'status' => InvestmentStatus::Active,
            'confirmed_at' => Carbon::parse('2026-05-01'),
            'maturity_at' => Carbon::parse('2027-05-01'),
            'confirmed_by' => $admin->id,
            'capital_balance' => 1000,
        ]);

        InvestmentTransaction::query()->create([
            'investment_id' => $investment->id,
            'type' => InvestmentTransactionType::Deposit,
            'amount' => 1000,
            'balance_after' => 1000,
            'platform_month_accrual_id' => null,
            'investment_month_index' => null,
            'applied_percent' => null,
        ]);

        $runDate = '2026-05-15';

        $this->actingAs($admin)
            ->post(route('admin.platform-accrual.debug-run'), ['date' => $runDate])
            ->assertRedirect(route('admin.platform-accrual.index'));

        $this->assertDatabaseHas('investment_transactions', [
            'investment_id' => $investment->id,
            'type' => InvestmentTransactionType::DailyGain->value,
        ]);
    }

    public function test_accrual_page_exposes_debug_flag_when_debug_enabled(): void
    {
        config(['app.allow_adjustments' => true]);

        $admin = User::factory()->admin()->create();

        $this->actingAs($admin)
            ->get(route('admin.platform-accrual.index'))
            ->assertOk()
            ->assertInertia(fn ($page) => $page
                ->component('admin/platform-monthly-accrual')
                ->where('adjustments_enabled', true));
    }

    public function test_accrual_run_detail_lists_delivered_lines(): void
    {
        InvestmentSetting::current();
        $admin = User::factory()->admin()->create();
        $investor = User::factory()->create(['email' => 'inversor@example.com']);

        $investment = $investor->investments()->create([
            'amount' => 500,
            'duration_months' => 6,
            'monthly_return_snapshot' => 3.5,
            'txid' => 'tx-detail-run',
            'status' => InvestmentStatus::Active,
            'confirmed_at' => Carbon::parse('2026-06-01'),
            'maturity_at' => Carbon::parse('2026-12-01'),
            'confirmed_by' => $admin->id,
            'capital_balance' => 502.5,
        ]);

        InvestmentTransaction::query()->create([
            'investment_id' => $investment->id,
            'type' => InvestmentTransactionType::DailyGain,
            'amount' => 2.5,
            'balance_after' => 502.5,
            'applied_percent' => round(3.5 / 30, 8),
            'accrual_on' => '2026-06-10',
        ]);

        $this->actingAs($admin)
            ->get(route('admin.platform-accrual.show', ['runDate' => '2026-06-10', 'mode' => 'daily']))
            ->assertOk()
            ->assertInertia(fn ($page) => $page
                ->component('admin/platform-monthly-accrual-detail')
                ->where('run.date', '2026-06-10')
                ->where('run.mode', 'daily')
                ->where('run.tx_count', 1)
                ->has('lines', 1)
                ->where('lines.0.txid', 'tx-detail-run')
                ->where('lines.0.investor_email', 'inversor@example.com')
                ->where('lines.0.amount_usd', '2.50'));
    }
}
