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

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

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

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

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

    public function test_offers_api_ok_when_module_enabled(): void
    {
        $this->seed([UserSeeder::class, ModuleAndNavigationSeeder::class]);
        $this->enableMaestroModules(['customers', 'offers']);

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

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