<?php

namespace Tests\Feature;

use App\Enums\RetentionOverrideScope;
use App\Jobs\AnonymizeAccount;
use App\Models\Package;
use App\Models\RetentionOverride;
use App\Models\Tenant;
use App\Models\TenantPackage;
use App\Models\User;
use App\Notifications\InactiveTenantDeletionWarning;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;

class TenantRetentionCommandTest extends TestCase
{
    use RefreshDatabase;

    public function test_inactive_tenant_gets_anonymized(): void
    {
        Queue::fake();

        $tenant = Tenant::factory()->create([
            'last_activity_at' => now()->subMonths(25),
        ]);
        $user = User::factory()->create(['tenant_id' => $tenant->id]);
        $tenant->user()->associate($user)->save();

        $this->artisan('tenants:retention-scan')->assertExitCode(0);
        Queue::assertPushed(AnonymizeAccount::class, function (AnonymizeAccount $job) use ($tenant) {
            return $job->tenantId() === $tenant->id;
        });
    }

    public function test_retention_override_skips_tenant_deletion(): void
    {
        Queue::fake();

        $tenant = Tenant::factory()->create([
            'last_activity_at' => now()->subMonths(25),
        ]);
        $user = User::factory()->create(['tenant_id' => $tenant->id]);
        $tenant->user()->associate($user)->save();

        RetentionOverride::factory()->create([
            'scope' => RetentionOverrideScope::TENANT,
            'tenant_id' => $tenant->id,
            'event_id' => null,
        ]);

        $this->artisan('tenants:retention-scan')->assertExitCode(0);

        Queue::assertNothingPushed();
    }

    public function test_warning_is_sent_one_month_before(): void
    {
        Queue::fake();
        Notification::fake();

        $tenant = Tenant::factory()->create([
            'last_activity_at' => now()->subMonths(23)->subWeek(),
            'contact_email' => 'owner@example.com',
        ]);
        $user = User::factory()->create(['tenant_id' => $tenant->id]);
        $tenant->user()->associate($user)->save();

        $this->artisan('tenants:retention-scan')->assertExitCode(0);

        Notification::assertSentOnDemand(
            InactiveTenantDeletionWarning::class,
            function (InactiveTenantDeletionWarning $notification, array $channels, $notifiable) {
                return in_array('mail', $channels, true);
            }
        );

        $this->assertNotNull($tenant->fresh()->deletion_warning_sent_at);
    }

    public function test_active_subscription_is_whitelisted(): void
    {
        Queue::fake();

        $tenant = Tenant::factory()->create([
            'last_activity_at' => now()->subMonths(25),
        ]);
        $user = User::factory()->create(['tenant_id' => $tenant->id]);
        $tenant->user()->associate($user)->save();

        $package = Package::factory()->create(['type' => 'reseller']);
        TenantPackage::create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'price' => 99,
            'purchased_at' => now()->subMonth(),
            'expires_at' => now()->addYear(),
            'used_events' => 0,
            'active' => true,
        ]);

        $this->artisan('tenants:retention-scan')->assertExitCode(0);

        Queue::assertNothingPushed();
    }
}
