<?php

namespace Tests\Feature\Maintenance;

use App\Models\User;
use App\Services\Settings\SettingsService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PlatformMaintenanceModeTest extends TestCase
{
    use RefreshDatabase;

    public function test_frontend_is_visible_when_platform_maintenance_is_disabled(): void
    {
        $this
            ->get(route('landing'))
            ->assertOk()
            ->assertSee('Find Remote Jobs That Actually Match Your Profile');
    }

    public function test_frontend_is_blocked_when_platform_maintenance_is_enabled(): void
    {
        app(SettingsService::class)->set(
            group: 'general',
            key: 'maintenance_mode_enabled',
            value: true,
            type: 'boolean',
        );

        app(SettingsService::class)->set(
            group: 'general',
            key: 'maintenance_mode_message',
            value: 'Short maintenance window.',
            type: 'textarea',
        );

        $this
            ->get(route('landing'))
            ->assertServiceUnavailable()
            ->assertSee('Maintenance mode')
            ->assertSee('Short maintenance window.');
    }

    public function test_legacy_duplicate_maintenance_keys_do_not_block_frontend(): void
    {
        app(SettingsService::class)->set(
            group: 'security',
            key: 'enable_maintenance_mode',
            value: true,
            type: 'boolean',
        );

        app(SettingsService::class)->set(
            group: 'maintenance',
            key: 'enable_maintenance_mode',
            value: true,
            type: 'boolean',
        );

        $this
            ->get(route('landing'))
            ->assertOk()
            ->assertSee('Find Remote Jobs That Actually Match Your Profile');
    }

    public function test_json_requests_receive_maintenance_json_response(): void
    {
        app(SettingsService::class)->set(
            group: 'general',
            key: 'maintenance_mode_enabled',
            value: true,
            type: 'boolean',
        );

        app(SettingsService::class)->set(
            group: 'general',
            key: 'maintenance_mode_message',
            value: 'API maintenance window.',
            type: 'textarea',
        );

        $this
            ->getJson(route('landing'))
            ->assertServiceUnavailable()
            ->assertJson([
                'message' => 'API maintenance window.',
            ]);
    }

    public function test_admin_routes_bypass_platform_maintenance(): void
    {
        app(SettingsService::class)->set(
            group: 'general',
            key: 'maintenance_mode_enabled',
            value: true,
            type: 'boolean',
        );

        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this
            ->actingAs($admin, 'admin')
            ->get('/admin/settings')
            ->assertOk()
            ->assertSee('Application name');
    }

    public function test_admin_users_still_see_maintenance_page_on_frontend(): void
    {
        app(SettingsService::class)->set(
            group: 'general',
            key: 'maintenance_mode_enabled',
            value: true,
            type: 'boolean',
        );

        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this
            ->actingAs($admin)
            ->get(route('landing'))
            ->assertServiceUnavailable()
            ->assertSee('Maintenance mode');
    }
}
