<?php

declare(strict_types=1);

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Routing\Route;
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 HoursModuleMiddlewareTest extends TestCase
{
    use RefreshDatabase;

    public function test_hours_week_current_route_is_registered(): void
    {
        $uris = collect(\Illuminate\Support\Facades\Route::getRoutes()->getRoutes())
            ->map(fn (Route $route) => $route->uri());

        self::assertContains('api/hours/week/current', $uris->all(), 'Hours routes may be missing — check core.modules.hours and provider registration.');
    }

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

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

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

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

    public function test_hours_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/hours/week/current')
            ->assertSuccessful();
    }
}
