<?php

use App\Models\ManagedEnvironment;
use App\Support\WorkspaceIsolation\TenantOwnedModelFamilies;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Symfony\Component\Finder\Finder;

uses(RefreshDatabase::class);

it('does not keep the legacy Tenant model as active core truth', function (): void {
    expect(file_exists(app_path('Models/Tenant.php')))->toBeFalse()
        ->and(class_exists('App\\Models\\Tenant', false))->toBeFalse()
        ->and(file_exists(base_path('database/factories/TenantFactory.php')))->toBeFalse()
        ->and(class_exists('Database\\Factories\\TenantFactory', false))->toBeFalse();
});

it('does not keep tenant-named platform environment owners active', function (): void {
    $legacyClasses = [
        'App\\Filament\\Resources\\TenantResource',
        'App\\Filament\\Resources\\TenantResource\\Pages\\ListTenants',
        'App\\Filament\\Resources\\TenantResource\\Pages\\ViewTenant',
        'App\\Filament\\Resources\\TenantResource\\Pages\\EditTenant',
        'App\\Filament\\Resources\\TenantResource\\Pages\\ManageTenantMemberships',
        'App\\Filament\\Resources\\TenantResource\\RelationManagers\\TenantMembershipsRelationManager',
        'App\\Filament\\Pages\\TenantDashboard',
        'App\\Filament\\Pages\\TenantDiagnostics',
        'App\\Filament\\Pages\\TenantRequiredPermissions',
    ];

    foreach ($legacyClasses as $legacyClass) {
        expect(class_exists($legacyClass, false))->toBeFalse($legacyClass.' must not remain active');
    }

    $legacyFiles = [
        app_path('Filament/Resources/TenantResource.php'),
        app_path('Filament/Resources/TenantResource/Pages/ListTenants.php'),
        app_path('Filament/Resources/TenantResource/Pages/ViewTenant.php'),
        app_path('Filament/Resources/TenantResource/Pages/EditTenant.php'),
        app_path('Filament/Resources/TenantResource/Pages/ManageTenantMemberships.php'),
        app_path('Filament/Resources/TenantResource/RelationManagers/TenantMembershipsRelationManager.php'),
        app_path('Filament/Pages/TenantDashboard.php'),
        app_path('Filament/Pages/TenantDiagnostics.php'),
        app_path('Filament/Pages/TenantRequiredPermissions.php'),
    ];

    foreach ($legacyFiles as $legacyFile) {
        expect(file_exists($legacyFile))->toBeFalse($legacyFile.' must be renamed');
    }

    expect(class_exists('App\\Filament\\Resources\\ManagedEnvironmentResource'))->toBeTrue()
        ->and(class_exists('App\\Filament\\Pages\\EnvironmentDashboard'))->toBeTrue()
        ->and(class_exists('App\\Filament\\Pages\\EnvironmentDiagnostics'))->toBeTrue()
        ->and(class_exists('App\\Filament\\Pages\\EnvironmentRequiredPermissions'))->toBeTrue();
});

it('does not import App Models Tenant or bind Filament tenancy to Tenant class', function (): void {
    $finder = Finder::create()
        ->files()
        ->name('*.php')
        ->in([
            app_path(),
            base_path('database'),
            base_path('tests'),
        ]);

    $violations = [];
    $legacyPanelTenantNeedle = '->tenant('.'Tenant::class';

    foreach ($finder as $file) {
        $contents = $file->getContents();

        if (preg_match('/^use\s+App\\\\Models\\\\Tenant;$/m', $contents) === 1) {
            $violations[] = $file->getRelativePathname().': imports App\\Models\\Tenant';
        }

        if (str_contains($contents, $legacyPanelTenantNeedle)) {
            $violations[] = $file->getRelativePathname().': binds Filament tenancy to Tenant::class';
        }
    }

    expect($violations)->toBe([]);
});

it('keeps the fresh schema on managed environment table names only', function (): void {
    expect(Schema::hasTable('tenants'))->toBeFalse()
        ->and(Schema::hasTable('tenant_user'))->toBeFalse()
        ->and(Schema::hasTable('tenant_memberships'))->toBeFalse()
        ->and(Schema::hasTable('user_tenant_preferences'))->toBeFalse()
        ->and(Schema::hasTable('managed_environments'))->toBeTrue()
        ->and(Schema::hasTable('managed_environment_user'))->toBeTrue()
        ->and(Schema::hasTable('managed_environment_memberships'))->toBeTrue()
        ->and(Schema::hasTable('user_managed_environment_preferences'))->toBeTrue();
});

it('keeps first-slice core tables on managed_environment_id instead of tenant_id', function (): void {
    foreach (TenantOwnedModelFamilies::firstSlice() as $family) {
        $table = $family['table'];

        expect(Schema::hasColumn($table, 'tenant_id'))
            ->toBeFalse("{$table} must not expose tenant_id after the managed-environment cutover");

        expect(Schema::hasColumn($table, 'managed_environment_id'))
            ->toBeTrue("{$table} must expose managed_environment_id after the managed-environment cutover");
    }
});

it('does not keep a registered tenant panel after the workspace-first cutover', function (): void {
    expect(Filament\Facades\Filament::getPanel('tenant'))->toBeNull();
});
