<?php

namespace Tests\Feature\Console;

use App\Models\Event;
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([
            'photobooth_enabled' => true,
            'photobooth_username' => 'pbcleanup',
            'photobooth_status' => 'active',
            'photobooth_path' => '/photobooth/demo',
            'photobooth_expires_at' => now()->subDay(),
        ]);
        $event->photobooth_password = 'CLEANUP';
        $event->save();

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

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

        $event->refresh();

        $this->assertFalse($event->photobooth_enabled);
        $this->assertNull($event->photobooth_username);
        $this->assertNotNull($event->photobooth_last_deprovisioned_at);

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