<?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 Tests\Fixtures\AuthTestUser;
use Tests\TestCase;

class PermissionsAndApiUserTest extends TestCase
{
    use RefreshDatabase;

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

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

        $this->assertDatabaseHas('model_has_roles', [
            'model_type' => AuthTestUser::class,
            'model_id' => $user->id,
        ]);
    }

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

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

        $response = $this->actingAs($user, 'web')->getJson('/api/user');

        $response->assertOk();
        $permissions = $response->json('permissions');
        self::assertIsArray($permissions);
        self::assertContains('settings.organization.view', $permissions);
        self::assertContains('roles.view', $permissions);
        self::assertContains('platform.superadmin', $permissions);
        self::assertContains('customers.view', $permissions);
        self::assertContains('hours.week.manage', $permissions);
        self::assertContains('offers.view', $permissions);
        $roles = $response->json('roles');
        self::assertSame(['admin'], $roles);
        $enabled = $response->json('enabled_module_slugs');
        self::assertIsArray($enabled);
        self::assertContains('customers', $enabled);
        self::assertContains('hours', $enabled);
        self::assertContains('offers', $enabled);
    }
}
