<?php

namespace Tests\Feature\Controllers;

use App\Master\Models\ApiUser;
use Tests\DatabaseTestCase;
use Tests\UserForTenant;

class ApiUserControllerTest extends DatabaseTestCase
{
    use UserForTenant;

    public function testShowIncludesAvailableRoutes()
    {
        $apiUser = ApiUser::factory()->create();

        $response = $this->actingAs($this->getUser())
            ->getJson(route('api-users.show', $apiUser->id, false));

        $response->assertStatus(200);

        $routeNames = collect($response->json('available_routes'))->pluck('name')->all();

        // Routes WITH ApiAuthenticatedRequest middleware should be present
        $this->assertContains('tenant.notifications.autograb.store', $routeNames);
        $this->assertContains('storm-enquiries', $routeNames);
        $this->assertContains('api.vehicles.vrm-lookup', $routeNames);

        // Routes WITHOUT ApiAuthenticatedRequest middleware should not be present
        $this->assertNotContains('tenant.dashboard', $routeNames);
        $this->assertNotContains('api-users.index', $routeNames);
        $this->assertNotContains('users.show', $routeNames);
    }

    public function testShowIncludesAllowedRoutes()
    {
        $apiUser = ApiUser::factory()->create();
        $apiUser->allowedRoutes()->createMany([
            ['route_name' => 'storm-enquiries'],
            ['route_name' => 'api.vehicles.vrm-lookup'],
        ]);

        $response = $this->actingAs($this->getUser())
            ->getJson(route('api-users.show', $apiUser->id, false));

        $response->assertStatus(200);

        $routeNames = collect($response->json('allowed_routes'))->pluck('name')->all();
        $this->assertContains('storm-enquiries', $routeNames);
        $this->assertContains('api.vehicles.vrm-lookup', $routeNames);
    }

    public function testUpdateSyncsRoutes()
    {
        $apiUser = ApiUser::factory()->create();
        $apiUser->allowedRoutes()->createMany([
            ['route_name' => 'storm-enquiries'],
        ]);

        $response = $this->actingAs($this->getUser())
            ->putJson(route('api-users.update', $apiUser->id, false), [
                'name' => $apiUser->name,
                'is_active' => $apiUser->is_active,
                'token_type' => $apiUser->token_type,
                'allowed_routes' => ['api.vehicles.vrm-lookup', 'tenant.notifications.autograb.store'],
            ]);

        $response->assertStatus(200);

        $this->assertDatabaseMissing('api_user_routes', [
            'api_user_id' => $apiUser->id,
            'route_name' => 'storm-enquiries',
        ], 'master-test');
        $this->assertDatabaseHas('api_user_routes', [
            'api_user_id' => $apiUser->id,
            'route_name' => 'api.vehicles.vrm-lookup',
        ], 'master-test');
        $this->assertDatabaseHas('api_user_routes', [
            'api_user_id' => $apiUser->id,
            'route_name' => 'tenant.notifications.autograb.store',
        ], 'master-test');
    }
}
