<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminDashboardControllerTest extends TestCase
{
    use RefreshDatabase;

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

        $response = $this->actingAs($admin)
            ->get(route('admin.dashboard'));

        $response->assertOk()
            ->assertSee('Filter Period:')
            ->assertSee('id="dateFilterSelect"', false);
    }

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

        $response = $this->actingAs($admin)
            ->withHeader('X-Requested-With', 'XMLHttpRequest')
            ->getJson(route('admin.dashboard.stats', ['filter' => 'this_month']));

        $response->assertOk()
            ->assertJsonStructure([
                'stats' => [
                    'total_orders',
                    'total_revenue',
                    'pending_orders',
                    'paid_orders',
                    'cancelled_orders',
                    'total_customers',
                    'total_products',
                    'low_stock_products',
                ],
                'chartsData' => [
                    'revenue' => ['labels', 'values'],
                    'order_statuses' => ['labels', 'values'],
                    'top_products' => ['labels', 'values'],
                    'payment_methods' => ['labels', 'values'],
                ],
                'recentOrders',
                'recentCustomers',
            ]);
    }

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

        // Create orders in different periods
        // Today
        $orderToday = Order::create([
            'order_number' => 'TEST-ORDER-001',
            'status' => 'completed',
            'order_status' => 'completed',
            'payment_status' => 'paid',
            'subtotal' => 100,
            'total' => 100,
            'customer_name' => 'Test Customer',
            'customer_phone' => '012345678',
        ]);
        $orderToday->created_at = now();
        $orderToday->save();

        // Yesterday
        $orderYesterday = Order::create([
            'order_number' => 'TEST-ORDER-002',
            'status' => 'completed',
            'order_status' => 'completed',
            'payment_status' => 'paid',
            'subtotal' => 50,
            'total' => 50,
            'customer_name' => 'Test Customer 2',
            'customer_phone' => '012345678',
        ]);
        $orderYesterday->created_at = now()->subDay();
        $orderYesterday->save();

        $responseToday = $this->actingAs($admin)
            ->withHeader('X-Requested-With', 'XMLHttpRequest')
            ->getJson(route('admin.dashboard.stats', ['filter' => 'today']));

        $responseToday->assertOk();
        $this->assertEquals(1, $responseToday->json('stats.total_orders'));
        $this->assertEquals(100.00, $responseToday->json('stats.total_revenue'));

        // Test Yesterday Filter
        $responseYesterday = $this->actingAs($admin)
            ->withHeader('X-Requested-With', 'XMLHttpRequest')
            ->getJson(route('admin.dashboard.stats', ['filter' => 'yesterday']));

        $responseYesterday->assertOk();
        $this->assertEquals(1, $responseYesterday->json('stats.total_orders'));
        $this->assertEquals(50.00, $responseYesterday->json('stats.total_revenue'));
    }

    public function test_guest_cannot_access_stats_ajax(): void
    {
        $response = $this->getJson(route('admin.dashboard.stats', ['filter' => 'this_month']));
        
        $response->assertStatus(401);
    }
}
