<?php

namespace Tests\Unit;

use App\Support\AdminProjectPermissionRegistry;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;

class AdminProjectPermissionRegistryTest extends TestCase
{
    public function test_activation_manage_permission_allows_manage_routes_only(): void
    {
        $assigned = ['activation_requests_manage'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.index', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.manual_status', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.history', $assigned)
        );
    }

    public function test_activation_history_permission_allows_history_route_only(): void
    {
        $assigned = ['activation_requests_history'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.history', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.index', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.approve', $assigned)
        );
    }

    public function test_activation_parent_permission_still_allows_both_routes(): void
    {
        $assigned = ['activation_requests'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.index', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.activation_requests.history', $assigned)
        );
    }

    public function test_users_all_permission_allows_only_all_status_on_main_users_route(): void
    {
        $assigned = ['users_all'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users', $assigned, Request::create('/admin/users', 'GET'))
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users', $assigned, Request::create('/admin/users?status=active', 'GET'))
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users', $assigned, Request::create('/admin/users?status=banned', 'GET'))
        );
    }

    public function test_users_status_permissions_follow_requested_status(): void
    {
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects(
                'admin.users',
                ['users_active'],
                Request::create('/admin/users?status=active', 'GET')
            )
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects(
                'admin.users',
                ['users_active'],
                Request::create('/admin/users?status=inactive', 'GET')
            )
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users.activate', ['users_activate'])
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users.activate', ['users_active'])
        );
    }

    public function test_welcome_offer_permission_allows_welcome_offer_routes(): void
    {
        $assigned = ['welcome_offer'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.welcome-offer.edit', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.welcome-offer.update', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.welcome-offer.grant', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.recharge.settings.edit', $assigned)
        );
    }

    public function test_typing_job_history_permission_allows_typing_job_routes(): void
    {
        $assigned = ['typing_job_history'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.typing-job.index', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.typing-job.settings.edit', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.welcome-offer.edit', $assigned)
        );
    }

    public function test_typing_job_settings_permission_allows_settings_routes_only(): void
    {
        $assigned = ['typing_job_settings'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.typing-job.settings.edit', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.typing-job.settings.update', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.typing-job.index', $assigned)
        );
    }

    public function test_gmail_completed_permission_allows_completed_list_and_status_update(): void
    {
        $assigned = ['gmail_sales_completed'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.gmail.sales.completed', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.gmail.sales.status', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.gmail.sales.rejected', $assigned)
        );
    }

    public function test_users_withdraw_hold_permission_allows_only_dedicated_routes(): void
    {
        $assigned = ['users_withdraw_hold'];

        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users.withdraw-hold.index', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users.withdraw-hold.edit', $assigned)
        );
        $this->assertTrue(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users.withdraw-hold.update', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users.edit', $assigned)
        );
        $this->assertFalse(
            AdminProjectPermissionRegistry::isRouteAllowedForProjects('admin.users.update', $assigned)
        );
    }
}
