<?php

declare(strict_types=1);

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Maestro\Core\Database\Seeders\ModuleAndNavigationSeeder;
use Maestro\Core\Database\Seeders\UserSeeder;
use Maestro\Core\Models\MaestroModule;
use Tests\Fixtures\AuthTestUser;
use Tests\TestCase;

class CustomersModuleMiddlewareTest extends TestCase
{
    use RefreshDatabase;

    public function test_customers_api_returns_403_when_module_disabled_in_database(): void
    {
        $this->seed([UserSeeder::class, ModuleAndNavigationSeeder::class]);

        MaestroModule::query()->where('slug', 'customers')->update(['is_enabled' => false]);

        $user = AuthTestUser::query()->where('email', 'demo@example.com')->first();
        self::assertNotNull($user);

        $this->actingAs($user, 'web')
            ->getJson('/api/customers')
            ->assertForbidden()
            ->assertJsonFragment(['message' => 'This module is disabled.']);
    }

    public function test_customers_api_ok_when_module_enabled(): void
    {
        $this->seed([UserSeeder::class, ModuleAndNavigationSeeder::class]);

        $user = AuthTestUser::query()->where('email', 'demo@example.com')->first();
        self::assertNotNull($user);

        $this->actingAs($user, 'web')
            ->getJson('/api/customers')
            ->assertOk();
    }
}
