<?php

namespace Tests\Feature;

use App\Models\NasMikrotik;
use App\Models\Tenant;
use App\Models\User;
use App\Services\Nas\NasResourceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class NasResourceServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_tenant_user_only_lists_nas_data_from_the_active_tenant(): void
    {
        $tenant = Tenant::create(['tenant_code' => 'NAS-A', 'tenant_name' => 'NAS A']);
        $otherTenant = Tenant::create(['tenant_code' => 'NAS-B', 'tenant_name' => 'NAS B']);
        $user = User::factory()->create(['access_level' => 'tenant_owner']);
        $user->userTenants()->create(['tenant_id' => $tenant->id, 'is_default' => true]);

        NasMikrotik::create([
            'tenant_id' => $tenant->id,
            'name' => 'Router A',
            'connection_type' => 'api',
            'host' => '10.0.0.1',
        ]);
        NasMikrotik::create([
            'tenant_id' => $otherTenant->id,
            'name' => 'Router B',
            'connection_type' => 'api',
            'host' => '10.0.0.2',
        ]);

        $result = app(NasResourceService::class)->list(
            'mikrotik',
            ['per_page' => 100],
            $user,
            $tenant->id
        );

        $this->assertSame(['Router A'], $result->getCollection()->pluck('name')->all());
    }

    public function test_mikrotik_credentials_are_encrypted_and_blank_update_preserves_them(): void
    {
        $tenant = Tenant::create(['tenant_code' => 'NAS-SECRET', 'tenant_name' => 'NAS Secret']);
        $master = User::factory()->create(['is_master' => true, 'access_level' => 'master_admin']);
        $service = app(NasResourceService::class);

        $mikrotik = $service->create('mikrotik', [
            'tenant_id' => $tenant->id,
            'name' => 'Secure Router',
            'connection_type' => 'radius',
            'host' => '10.10.10.1',
            'radius_secret' => 'plain-secret',
        ], $master, null);

        $this->assertNotSame('plain-secret', $mikrotik->getRawOriginal('radius_secret'));
        $this->assertSame('plain-secret', $mikrotik->radius_secret);

        $service->update('mikrotik', $mikrotik, [
            'name' => 'Secure Router Updated',
            'radius_secret' => '',
        ], $master);

        $this->assertSame('plain-secret', $mikrotik->fresh()->radius_secret);
    }

    public function test_nas_route_resolves_its_resource_configuration(): void
    {
        $tenant = Tenant::create(['tenant_code' => 'NAS-ROUTE', 'tenant_name' => 'NAS Route']);
        $master = User::factory()->create(['is_master' => true, 'access_level' => 'master_admin']);
        Sanctum::actingAs($master);

        $this->postJson('/api/nas/mikrotiks', [
            'tenant_id' => $tenant->id,
            'name' => 'Route Router',
            'connection_type' => 'api',
            'host' => '192.168.88.1',
        ])
            ->assertCreated()
            ->assertJsonPath('data.name', 'Route Router')
            ->assertJsonPath('data.connection_type', 'api');
    }
}
