<?php

namespace Tests\Feature\Console;

use App\Models\Event;
use App\Models\EventPhotoboothSetting;
use App\Models\PhotoboothSetting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class PhotoboothCleanupCommandTest extends TestCase
{
    use RefreshDatabase;

    public function test_command_disables_expired_accounts(): void
    {
        config([
            'photobooth.control_service.base_url' => 'https://control.test',
            'photobooth.control_service.token' => 'secret-token',
        ]);

        PhotoboothSetting::current()->update([
            'control_service_base_url' => 'https://control.test',
        ]);

        $event = Event::factory()->create();
        $setting = EventPhotoboothSetting::factory()
            ->for($event)
            ->create([
                'enabled' => true,
                'mode' => 'ftp',
                'username' => 'pbcleanup',
                'password' => 'CLEANUP',
                'status' => 'active',
                'path' => '/photobooth/demo',
                'expires_at' => now()->subDay(),
            ]);

        Http::fake([
            'https://control.test/*' => Http::response(['ok' => true], 200),
        ]);

        $this->artisan('photobooth:cleanup-expired')
            ->assertExitCode(0);

        $setting->refresh();

        $this->assertFalse($setting->enabled);
        $this->assertNull($setting->username);
        $this->assertNotNull($setting->last_deprovisioned_at);

        Http::assertSent(fn ($request) => $request->url() === 'https://control.test/users/pbcleanup');
    }
}
