<?php

use App\Models\AuditLog;
use App\Models\BackupItem;
use App\Models\BackupSchedule;
use App\Models\BackupScheduleRun;
use App\Models\BackupSet;
use App\Models\OperationRun;
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\RestoreRun;
use App\Models\SettingsCatalogCategory;
use App\Models\SettingsCatalogDefinition;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('purges non-persistent tenant data without touching persistent catalog data', function () {
    $tenantA = Tenant::factory()->create(['name' => 'Tenant A']);
    $tenantB = Tenant::factory()->create(['name' => 'Tenant B']);

    SettingsCatalogCategory::create([
        'category_id' => 'cat-1',
        'display_name' => 'Account Management',
        'description' => null,
    ]);

    SettingsCatalogDefinition::create([
        'definition_id' => 'def-1',
        'display_name' => 'Deletion Policy',
        'description' => null,
        'help_text' => null,
        'category_id' => 'cat-1',
        'ux_behavior' => null,
        'raw' => [],
    ]);

    $user = User::factory()->create();

    $policyA = Policy::factory()->create(['tenant_id' => $tenantA->id]);
    $policyB = Policy::factory()->create(['tenant_id' => $tenantB->id]);

    PolicyVersion::factory()->create([
        'tenant_id' => $tenantA->id,
        'policy_id' => $policyA->id,
        'version_number' => 1,
    ]);

    PolicyVersion::factory()->create([
        'tenant_id' => $tenantB->id,
        'policy_id' => $policyB->id,
        'version_number' => 1,
    ]);

    $backupSetA = BackupSet::factory()->create(['tenant_id' => $tenantA->id]);
    BackupItem::factory()->create([
        'tenant_id' => $tenantA->id,
        'backup_set_id' => $backupSetA->id,
        'policy_id' => $policyA->id,
    ]);

    RestoreRun::factory()->create([
        'tenant_id' => $tenantA->id,
        'backup_set_id' => $backupSetA->id,
    ]);

    AuditLog::create([
        'tenant_id' => $tenantA->id,
        'actor_id' => null,
        'actor_email' => null,
        'actor_name' => null,
        'action' => 'test.action',
        'resource_type' => null,
        'resource_id' => null,
        'status' => 'success',
        'metadata' => null,
        'recorded_at' => now(),
    ]);

    OperationRun::factory()->create([
        'tenant_id' => $tenantA->id,
        'user_id' => $user->id,
        'status' => 'completed',
    ]);

    $scheduleA = BackupSchedule::create([
        'tenant_id' => $tenantA->id,
        'name' => 'Schedule A',
        'is_enabled' => true,
        'timezone' => 'UTC',
        'frequency' => 'daily',
        'time_of_day' => '10:00:00',
        'days_of_week' => null,
        'policy_types' => ['deviceConfiguration'],
        'include_foundations' => true,
        'retention_keep_last' => 30,
        'last_run_at' => null,
        'last_run_status' => null,
        'next_run_at' => now()->addHour(),
    ]);

    BackupScheduleRun::create([
        'backup_schedule_id' => $scheduleA->id,
        'tenant_id' => $tenantA->id,
        'scheduled_for' => now()->startOfMinute(),
        'started_at' => null,
        'finished_at' => null,
        'status' => BackupScheduleRun::STATUS_SUCCESS,
        'summary' => null,
        'error_code' => null,
        'error_message' => null,
        'backup_set_id' => $backupSetA->id,
    ]);

    expect(Policy::query()->where('tenant_id', $tenantA->id)->count())->toBeGreaterThan(0);
    expect(BackupSet::withTrashed()->where('tenant_id', $tenantA->id)->count())->toBeGreaterThan(0);
    expect(BackupScheduleRun::query()->where('tenant_id', $tenantA->id)->count())->toBeGreaterThan(0);

    $this->artisan('tenantpilot:purge-nonpersistent', [
        'tenant' => $tenantA->id,
        '--force' => true,
        '--no-interaction' => true,
    ])->assertSuccessful();

    expect(Policy::query()->where('tenant_id', $tenantA->id)->count())->toBe(0);
    expect(PolicyVersion::withTrashed()->where('tenant_id', $tenantA->id)->count())->toBe(0);
    expect(BackupItem::withTrashed()->where('tenant_id', $tenantA->id)->count())->toBe(0);
    expect(BackupSet::withTrashed()->where('tenant_id', $tenantA->id)->count())->toBe(0);
    expect(RestoreRun::withTrashed()->where('tenant_id', $tenantA->id)->count())->toBe(0);
    expect(AuditLog::query()->where('tenant_id', $tenantA->id)->count())->toBe(0);
    expect(OperationRun::query()->where('tenant_id', $tenantA->id)->count())->toBe(0);
    expect(BackupScheduleRun::query()->where('tenant_id', $tenantA->id)->count())->toBe(0);

    expect(BackupSchedule::query()->where('tenant_id', $tenantA->id)->count())->toBe(0);

    expect(Policy::query()->where('tenant_id', $tenantB->id)->count())->toBe(1);
    expect(PolicyVersion::withTrashed()->where('tenant_id', $tenantB->id)->count())->toBe(1);

    expect(SettingsCatalogCategory::query()->count())->toBe(1);
    expect(SettingsCatalogDefinition::query()->count())->toBe(1);
});
