<?php

namespace Tests\Feature;

use App\Models\AdminPermission;
use App\Models\AdminUser;
use App\Models\Category;
use App\Models\Customer;
use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class AdminCustomerModuleTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_with_customer_permission_can_view_customers_and_details(): void
    {
        $admin = $this->customerAdmin();
        $customer = Customer::query()->create([
            'name' => 'Module Customer',
            'email' => 'module.customer@example.com',
            'password' => Hash::make('secret-password'),
            'is_active' => true,
        ]);
        $product = $this->product();
        $customer->cartItems()->create(['product_id' => $product->id, 'quantity' => 2]);
        $customer->favorites()->create(['product_id' => $product->id]);

        $this->actingAs($admin, 'admin')
            ->get('/admin/customers')
            ->assertOk()
            ->assertSee('Module Customer')
            ->assertSee('module.customer@example.com');

        $this->actingAs($admin, 'admin')
            ->get("/admin/customers/{$customer->id}")
            ->assertOk()
            ->assertSee('Cart Product')
            ->assertSee('Module Customer');
    }

    public function test_admin_can_toggle_and_delete_customer(): void
    {
        $admin = $this->customerAdmin();
        $customer = Customer::query()->create([
            'name' => 'Toggle Customer',
            'email' => 'toggle.customer@example.com',
            'password' => Hash::make('secret-password'),
            'is_active' => true,
        ]);

        $this->actingAs($admin, 'admin')
            ->patch("/admin/customers/{$customer->id}/toggle")
            ->assertSessionHas('status');

        $this->assertFalse($customer->fresh()->is_active);

        $this->actingAs($admin, 'admin')
            ->delete("/admin/customers/{$customer->id}")
            ->assertRedirect(route('admin.customers.index'));

        $this->assertDatabaseMissing('customers', ['id' => $customer->id]);
    }

    public function test_admin_without_customer_permission_cannot_view_customers(): void
    {
        $admin = AdminUser::query()->create([
            'name' => 'Product Admin',
            'email' => 'product.only.admin@example.com',
            'password' => Hash::make('secret-password'),
            'is_active' => true,
        ]);

        $permission = AdminPermission::query()->create(['name' => 'Manage Products', 'slug' => 'products.manage']);
        $admin->permissions()->attach($permission);

        $this->actingAs($admin, 'admin')
            ->get('/admin/customers')
            ->assertForbidden();
    }

    private function customerAdmin(): AdminUser
    {
        $admin = AdminUser::query()->create([
            'name' => 'Customer Admin',
            'email' => 'customer.admin@example.com',
            'password' => Hash::make('secret-password'),
            'is_active' => true,
        ]);

        $admin->permissions()->attach(
            AdminPermission::query()->create(['name' => 'Manage Customers', 'slug' => 'customers.manage'])
        );

        return $admin;
    }

    private function product(): Product
    {
        $category = Category::query()->create([
            'name' => 'Customer Module Category',
            'slug' => 'customer-module-category',
            'is_active' => true,
        ]);

        return Product::query()->create([
            'category_id' => $category->id,
            'name' => 'Cart Product',
            'slug' => 'customer-module-product',
            'price' => 59,
            'stock' => 10,
            'rating' => 4.5,
            'is_active' => true,
        ]);
    }
}
