<?php

namespace Tests\Feature;

use App\Enums\UserRole;
use App\Models\ActivityLog;
use App\Models\Business;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SuperAdminBackendTest extends TestCase
{
    use RefreshDatabase;

    public function test_super_admin_dashboard_uses_database_metrics_and_businesses(): void
    {
        $this->seed();
        $admin = $this->superAdmin();

        $this->actingAs($admin)
            ->get(route('super-admin.dashboard'))
            ->assertOk()
            ->assertSee('Total Businesses')
            ->assertSee('Monthly Recurring Revenue')
            ->assertSee('Glow Beauty Salon');
    }

    public function test_super_admin_can_suspend_activate_business_and_log_sensitive_actions(): void
    {
        $this->seed();
        $admin = $this->superAdmin();
        $business = Business::query()->where('slug', 'glow-beauty-salon')->firstOrFail();

        $this->actingAs($admin)
            ->patch(route('super-admin.businesses.suspend', $business))
            ->assertRedirect(route('super-admin.businesses.show', $business));

        $this->assertSame('suspended', $business->fresh()->status);
        $this->assertDatabaseHas('activity_logs', [
            'business_id' => $business->id,
            'user_id' => $admin->id,
            'event' => 'business.suspended',
        ]);

        $this->actingAs($admin)
            ->patch(route('super-admin.businesses.activate', $business))
            ->assertRedirect(route('super-admin.businesses.show', $business));

        $this->assertSame('active', $business->fresh()->status);
        $this->assertDatabaseHas('activity_logs', [
            'business_id' => $business->id,
            'user_id' => $admin->id,
            'event' => 'business.activated',
        ]);
    }

    public function test_login_as_business_owner_is_placeholder_and_audited(): void
    {
        $this->seed();
        $admin = $this->superAdmin();
        $business = Business::query()->where('slug', 'glow-beauty-salon')->firstOrFail();

        $this->actingAs($admin)
            ->post(route('super-admin.businesses.login-as-owner', $business))
            ->assertRedirect(route('super-admin.businesses.show', $business))
            ->assertSessionHas('status');

        $this->assertDatabaseHas('activity_logs', [
            'business_id' => $business->id,
            'user_id' => $admin->id,
            'event' => 'business.login_as_owner.placeholder',
        ]);
    }

    public function test_super_admin_can_save_platform_settings_and_activity_log_is_created(): void
    {
        $this->seed();
        $admin = $this->superAdmin();

        $this->actingAs($admin)
            ->post(route('super-admin.settings.update', 'system'), [
                'site_name' => 'Booklio Platform',
                'site_logo' => 'booklio-new-logo.png',
                'currency' => 'USD',
                'timezone' => 'Asia/Karachi',
                'default_trial_days' => 21,
                'default_language' => 'en',
                'maintenance_mode' => '1',
            ])
            ->assertRedirect();

        $this->assertSame('Booklio Platform', Setting::query()
            ->whereNull('business_id')
            ->where('group', 'platform_system')
            ->where('name', 'site_name')
            ->firstOrFail()
            ->value);

        $this->assertDatabaseHas('activity_logs', [
            'user_id' => $admin->id,
            'event' => 'platform.settings.system.updated',
        ]);
    }

    public function test_super_admin_database_lists_render_with_filters(): void
    {
        $this->seed();
        $admin = $this->superAdmin();

        $this->actingAs($admin)
            ->get(route('super-admin.businesses.index', ['q' => 'Glow']))
            ->assertOk()
            ->assertSee('Glow Beauty Salon');

        $this->actingAs($admin)
            ->get(route('super-admin.payments.index', ['q' => 'prime']))
            ->assertOk()
            ->assertSee('sub_prime-dental-clinic');

        $this->actingAs($admin)
            ->get(route('super-admin.users.index', ['q' => 'Sarah']))
            ->assertOk()
            ->assertSee('Sarah Johnson');

        $this->actingAs($admin)
            ->get(route('super-admin.support.index', ['q' => 'TCK']))
            ->assertOk()
            ->assertSee('TCK-');

        $activity = ActivityLog::query()->latest()->firstOrFail();

        $this->actingAs($admin)
            ->get(route('super-admin.activity.index', ['q' => $activity->event]))
            ->assertOk()
            ->assertSee($activity->event);
    }

    public function test_business_owner_cannot_access_super_admin_backend(): void
    {
        $owner = User::factory()->create(['role' => UserRole::BusinessOwner->value]);

        $this->actingAs($owner)
            ->get(route('super-admin.businesses.index'))
            ->assertForbidden();
    }

    private function superAdmin(): User
    {
        return User::query()->where('email', 'admin@booklio.test')->firstOrFail();
    }
}
