<?php

declare(strict_types=1);

use App\Filament\Concerns\InteractsWithTenantOwnedRecords;
use App\Models\Policy;
use App\Models\ManagedEnvironment;
use App\Support\WorkspaceIsolation\TenantOwnedQueryScope;
use App\Support\WorkspaceIsolation\TenantOwnedRecordResolver;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('applies a tenant-owned scope to matching records only', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $foreignTenant = ManagedEnvironment::factory()->create();

    $ownedRecord = Policy::factory()->create(['managed_environment_id' => $tenant->getKey()]);
    Policy::factory()->create(['managed_environment_id' => $foreignTenant->getKey()]);

    $records = app(TenantOwnedQueryScope::class)
        ->apply(Policy::query(), $tenant)
        ->pluck('id')
        ->all();

    expect($records)->toBe([(int) $ownedRecord->getKey()]);
});

it('fails closed when no tenant context is available', function (): void {
    ManagedEnvironment::factory()->count(2)->create()->each(function (ManagedEnvironment $tenant): void {
        Policy::factory()->create(['managed_environment_id' => $tenant->getKey()]);
    });

    $count = app(TenantOwnedQueryScope::class)
        ->apply(Policy::query(), null)
        ->count();

    expect($count)->toBe(0);
});

it('resolves only records inside the already-scoped tenant query', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $foreignTenant = ManagedEnvironment::factory()->create();

    $ownedRecord = Policy::factory()->create(['managed_environment_id' => $tenant->getKey()]);
    $foreignRecord = Policy::factory()->create(['managed_environment_id' => $foreignTenant->getKey()]);

    $resolver = app(TenantOwnedRecordResolver::class);
    $scopedQuery = app(TenantOwnedQueryScope::class)->apply(Policy::query(), $tenant);

    expect($resolver->resolve($scopedQuery, $ownedRecord))
        ->toBeInstanceOf(Model::class)
        ->and($resolver->resolve($scopedQuery, $ownedRecord)?->is($ownedRecord))->toBeTrue();

    expect($resolver->resolve($scopedQuery, $foreignRecord))->toBeNull();
});

it('lets the shared filament trait expose the scoped query and resolver helpers', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $foreignTenant = ManagedEnvironment::factory()->create();

    $ownedRecord = Policy::factory()->create(['managed_environment_id' => $tenant->getKey()]);
    Policy::factory()->create(['managed_environment_id' => $foreignTenant->getKey()]);

    TestTenantOwnedPolicyResource::fakeTenant($tenant);

    expect(TestTenantOwnedPolicyResource::queryViaTrait()->pluck('id')->all())
        ->toBe([(int) $ownedRecord->getKey()]);

    expect(TestTenantOwnedPolicyResource::resolveViaTrait($ownedRecord)?->is($ownedRecord))
        ->toBeTrue();
});

class TestTenantOwnedPolicyResource extends Resource
{
    use InteractsWithTenantOwnedRecords;

    protected static ?string $model = Policy::class;

    protected static ?string $tenantOwnershipRelationshipName = 'tenant';

    protected static ?ManagedEnvironment $fakeTenant = null;

    public static function fakeTenant(?ManagedEnvironment $tenant): void
    {
        static::$fakeTenant = $tenant;
    }

    public static function queryViaTrait()
    {
        return static::getTenantOwnedEloquentQuery();
    }

    public static function resolveViaTrait(Model|int|string|null $record): ?Model
    {
        return static::resolveTenantOwnedRecord($record);
    }

    protected static function resolveTenantContextForTenantOwnedRecords(): ?ManagedEnvironment
    {
        return static::$fakeTenant;
    }

    public static function form(Schema $schema): Schema
    {
        return $schema;
    }
}
