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

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

        $this->actingAs($admin)
            ->get(route('admin.dashboard'))
            ->assertOk();
    }

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

        $this->actingAs($admin)
            ->get(route('admin.products.index'))
            ->assertOk()
            ->assertSee('Actions')
            ->assertDontSee('id="selectAll"', false);
    }

    public function test_product_datatable_ignores_empty_filters(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        Product::create([
            'name' => 'Visible Product',
            'price' => 12.50,
            'category' => '',
            'in_stock' => true,
            'on_sale' => false,
            'images' => [],
        ]);

        $response = $this->actingAs($admin)
            ->withHeader('X-Requested-With', 'XMLHttpRequest')
            ->getJson(route('admin.products.index', [
                'draw' => 1,
                'start' => 0,
                'length' => 25,
                'in_stock' => '',
                'on_sale' => '',
                'price_min' => '',
                'price_max' => '',
                'search' => ['value' => ''],
                'order' => [['column' => 1, 'dir' => 'asc']],
            ]));

        $response->assertOk()
            ->assertJsonPath('recordsTotal', 1)
            ->assertJsonPath('recordsFiltered', 1)
            ->assertJsonCount(1, 'data')
            ->assertSee('Visible Product');
    }

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

        $this->actingAs($customer)
            ->get(route('admin.dashboard'))
            ->assertRedirect(route('admin.login'));

        $this->assertGuest();
    }

    public function test_product_api_falls_back_from_invalid_sorting_and_caps_page_size(): void
    {
        $this->getJson('/api/v1/products?sort_by=not_a_column&sort_order=sideways&per_page=9999')
            ->assertOk()
            ->assertJsonPath('success', true)
            ->assertJsonPath('data.per_page', 100);
    }

    public function test_admin_order_update_keeps_current_and_legacy_status_in_sync(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $order = Order::create([
            'order_number' => 'TEST-ORDER-001',
            'status' => 'pending',
            'order_status' => 'pending',
            'payment_status' => 'pending',
            'subtotal' => 25,
            'total' => 25,
            'customer_name' => 'Test Customer',
            'customer_phone' => '012345678',
        ]);

        $this->actingAs($admin)
            ->put(route('admin.orders.update', $order), ['status' => 'completed'])
            ->assertRedirect(route('admin.orders.show', $order));

        $this->assertDatabaseHas('orders', [
            'id' => $order->id,
            'status' => 'completed',
            'order_status' => 'completed',
        ]);

        $this->assertNotNull($order->fresh()->completed_at);
    }
}
