<?php

namespace Tests\Feature;

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

class AdminPaymentAndOrderActionsTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test admin can update order status and redirect back with referrer.
     */
    public function test_admin_can_update_order_status_and_redirects_back_with_referer(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $order = Order::create([
            'order_number' => 'TEST-ORD-101',
            'status' => 'pending',
            'order_status' => 'pending',
            'payment_status' => 'pending',
            'subtotal' => 50,
            'total' => 50,
            'customer_name' => 'John Doe',
            'customer_phone' => '012345678',
        ]);

        $response = $this->actingAs($admin)
            ->from(route('admin.orders.index'))
            ->put(route('admin.orders.update', $order), [
                'status' => 'processing'
            ]);

        $response->assertRedirect(route('admin.orders.index'));
        
        $this->assertDatabaseHas('orders', [
            'id' => $order->id,
            'status' => 'processing',
            'order_status' => 'processing',
        ]);
    }

    /**
     * Test admin can delete order.
     */
    public function test_admin_can_delete_order(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $order = Order::create([
            'order_number' => 'TEST-ORD-102',
            'status' => 'pending',
            'order_status' => 'pending',
            'payment_status' => 'pending',
            'subtotal' => 50,
            'total' => 50,
            'customer_name' => 'John Doe',
            'customer_phone' => '012345678',
        ]);

        $response = $this->actingAs($admin)
            ->delete(route('admin.orders.destroy', $order));

        $response->assertRedirect(route('admin.orders.index'));
        $this->assertDatabaseMissing('orders', ['id' => $order->id]);
    }

    /**
     * Test payments datatable returns actions column.
     */
    public function test_payments_datatable_returns_actions_column(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        Order::create([
            'order_number' => 'TEST-ORD-103',
            'status' => 'pending',
            'order_status' => 'pending',
            'payment_status' => 'pending',
            'payment_method' => 'aba',
            'transaction_id' => 'TXN-ABA-777',
            'subtotal' => 50,
            'total' => 50,
            'customer_name' => 'John Doe',
            'customer_phone' => '012345678',
        ]);

        $response = $this->actingAs($admin)
            ->withHeader('X-Requested-With', 'XMLHttpRequest')
            ->getJson(route('admin.payments.index', [
                'draw' => 1,
                'start' => 0,
                'length' => 10,
            ]));

        $response->assertOk()
            ->assertJsonPath('recordsTotal', 1)
            ->assertSee('dropdown-payment-')
            ->assertSee('Mark Paid')
            ->assertSee('Mark Failed');
    }

    /**
     * Test admin can update payment status.
     */
    public function test_admin_can_update_payment_status(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $order = Order::create([
            'order_number' => 'TEST-ORD-104',
            'status' => 'pending',
            'order_status' => 'pending',
            'payment_status' => 'pending',
            'payment_method' => 'aba',
            'transaction_id' => 'TXN-ABA-888',
            'subtotal' => 50,
            'total' => 50,
            'customer_name' => 'John Doe',
            'customer_phone' => '012345678',
        ]);

        $response = $this->actingAs($admin)
            ->put(route('admin.payments.update', $order->id), [
                'payment_status' => 'paid'
            ]);

        $response->assertRedirect(route('admin.payments.index'));
        
        $this->assertDatabaseHas('orders', [
            'id' => $order->id,
            'payment_status' => 'paid',
        ]);
        $this->assertNotNull($order->fresh()->paid_at);
    }

    /**
     * Test admin can destroy/reset payment status.
     */
    public function test_admin_can_delete_payment_record(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $order = Order::create([
            'order_number' => 'TEST-ORD-105',
            'status' => 'pending',
            'order_status' => 'pending',
            'payment_status' => 'paid',
            'payment_method' => 'aba',
            'transaction_id' => 'TXN-ABA-999',
            'subtotal' => 50,
            'total' => 50,
            'customer_name' => 'John Doe',
            'customer_phone' => '012345678',
        ]);

        $response = $this->actingAs($admin)
            ->delete(route('admin.payments.destroy', $order->id));

        $response->assertRedirect(route('admin.payments.index'));
        
        $freshOrder = $order->fresh();
        $this->assertEquals('aba', $freshOrder->payment_method);
        $this->assertEquals('pending', $freshOrder->payment_status);
        $this->assertNull($freshOrder->transaction_id);
    }
}
