<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use App\Filament\Widgets\FinancialMetricsWidget;
use App\Filament\Widgets\ProfitMarginWidget;
use App\Filament\Widgets\StatsOverviewWidget;
use Tests\TestCase;

class DashboardWidgetsTest extends TestCase
{
    use RefreshDatabase;

    public function test_dashboard_renders_successfully_with_correct_widgets(): void
    {
        $admin = User::factory()->create([
            'email' => 'admin@sawdabazar.com',
            'role' => 'admin',
        ]);

        $response = $this->actingAs($admin)->get('/admin');

        $response->assertStatus(200);

        // Core widgets (like ProfitMarginWidget) should be registered as children
        $response->assertSee('App\\\\Filament\\\\Widgets\\\\ProfitMarginWidget', false);
        $response->assertSee('App\\\\Filament\\\\Widgets\\\\StatsOverviewWidget', false);
        $response->assertSee('App\\\\Filament\\\\Widgets\\\\RevenueChartWidget', false);

        // Inventory widget (FinancialMetricsWidget) should NOT be registered on the Dashboard
        $response->assertDontSee('App\\\\Filament\\\\Widgets\\\\FinancialMetricsWidget', false);
    }

    public function test_reports_page_renders_successfully_with_correct_widgets(): void
    {
        $admin = User::factory()->create([
            'email' => 'admin@sawdabazar.com',
            'role' => 'admin',
        ]);

        $response = $this->actingAs($admin)->get('/admin/reports');

        $response->assertStatus(200);

        // FinancialMetricsWidget has moved to Stock Adjustments page — should NOT appear on Reports
        $response->assertDontSee('App\\\\Filament\\\\Widgets\\\\FinancialMetricsWidget', false);

        // ProfitMarginWidget should NOT be registered on the Reports page
        $response->assertDontSee('App\\\\Filament\\\\Widgets\\\\ProfitMarginWidget', false);
    }

    public function test_stock_adjustments_page_shows_financial_metrics_widget(): void
    {
        $admin = User::factory()->create([
            'email' => 'admin@sawdabazar.com',
            'role' => 'admin',
        ]);

        $response = $this->actingAs($admin)->get('/admin/stock-adjustments');

        $response->assertStatus(200);

        // FinancialMetricsWidget should appear on the Stock Adjustments page
        $response->assertSee('App\\\\Filament\\\\Widgets\\\\FinancialMetricsWidget', false);
    }

    public function test_financial_metrics_widget_renders(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);

        Livewire::test(FinancialMetricsWidget::class)
            ->assertSee('Inventory Value (Cost)')
            ->assertSee('Potential Revenue')
            ->assertSee('Potential Profit');
    }

    public function test_profit_margin_widget_renders(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);

        Livewire::test(ProfitMarginWidget::class)
            ->assertSee('Net Profit')
            ->assertSee('Net Profit Margin')
            ->assertSee('Average Order Value (AOV)');
    }

    public function test_stats_overview_widget_renders(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);

        Livewire::test(StatsOverviewWidget::class)
            ->assertSee('Gross Sales')
            ->assertSee('Net Sales')
            ->assertSee('Total Orders')
            ->assertSee('Pending Orders');
    }
}
