<?php

namespace Tests\Feature\Photobooth;

use App\Models\Event;
use App\Models\EventPhotoboothSetting;
use App\Models\PhotoboothSetting;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\Feature\Tenant\TenantTestCase;

class PhotoboothControllerTest extends TenantTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        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',
        ]);
    }

    #[Test]
    public function it_returns_photobooth_status_for_an_event(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'photobooth-demo',
        ]);

        $response = $this->authenticatedRequest('GET', "/api/v1/tenant/events/{$event->slug}/photobooth");

        $response->assertOk()
            ->assertJsonPath('data.enabled', false)
            ->assertJsonPath('data.username', null)
            ->assertJsonPath('data.ftp.port', 2121);
    }

    #[Test]
    public function it_can_enable_and_rotate_photobooth_access(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'photobooth-event',
            'date' => now()->addDay(),
        ]);

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

        $enable = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/photobooth/enable");

        $enable->assertOk()
            ->assertJsonPath('data.enabled', true)
            ->assertJsonPath('data.username', fn ($value) => is_string($value) && strlen($value) <= 10);

        $event->refresh();
        $setting = $event->photoboothSetting;
        $this->assertNotNull($setting);
        $this->assertTrue($setting->enabled);
        $this->assertNotNull($setting->username);
        $this->assertNotNull($setting->password);
        $username = $setting->username;
        $firstPassword = $setting->password;

        Http::assertSent(fn ($request) => $request->url() === 'https://control.test/users' && $request['username'] === $username);

        $rotate = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/photobooth/rotate");

        $rotate->assertOk()
            ->assertJsonPath('data.enabled', true);

        $event->refresh();
        $this->assertNotSame($firstPassword, $event->photoboothSetting?->password);

        Http::assertSent(fn ($request) => $request->url() === "https://control.test/users/{$username}/rotate");
    }

    #[Test]
    public function it_can_disable_photobooth_access(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'photobooth-disable',
        ]);
        EventPhotoboothSetting::factory()
            ->for($event)
            ->create([
                'enabled' => true,
                'mode' => 'ftp',
                'username' => 'pb123456',
                'password' => 'SECRET12',
                'path' => '/photobooth/demo',
                'status' => 'active',
                'expires_at' => now()->subDay(),
            ]);

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

        $response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/photobooth/disable");

        $response->assertOk()
            ->assertJsonPath('data.enabled', false)
            ->assertJsonPath('data.username', null);

        $event->refresh();
        $this->assertFalse($event->photoboothSetting?->enabled);
        $this->assertNull($event->photoboothSetting?->username);

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