<?php

namespace Tests\Feature\Photobooth;

use App\Models\Event;
use App\Models\EventPhotoboothSetting;
use App\Models\PhotoboothConnectCode;
use PHPUnit\Framework\Attributes\Test;
use Tests\Feature\Tenant\TenantTestCase;

class PhotoboothConnectCodeTest extends TenantTestCase
{
    #[Test]
    public function it_creates_a_connect_code_for_sparkbooth(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'connect-code-event',
        ]);

        EventPhotoboothSetting::factory()
            ->for($event)
            ->activeSparkbooth()
            ->create([
                'username' => 'pbconnect',
                'password' => 'SECRET12',
            ]);

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

        $response->assertOk()
            ->assertJsonPath('data.code', fn ($value) => is_string($value) && strlen($value) === 6)
            ->assertJsonPath('data.expires_at', fn ($value) => is_string($value) && $value !== '');

        $this->assertDatabaseCount('photobooth_connect_codes', 1);
    }

    #[Test]
    public function it_redeems_a_connect_code_and_returns_upload_credentials(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'connect-code-redeem',
            'name' => 'Winterhochzeit',
        ]);

        EventPhotoboothSetting::factory()
            ->for($event)
            ->activeSparkbooth()
            ->create([
                'username' => 'pbconnect',
                'password' => 'SECRET12',
            ]);

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

        $code = (string) $codeResponse->json('data.code');

        $redeem = $this->postJson('/api/v1/photobooth/connect', [
            'code' => $code,
        ]);

        $redeem->assertOk()
            ->assertJsonPath('data.event_name', 'Winterhochzeit')
            ->assertJsonPath('data.upload_url', fn ($value) => is_string($value) && $value !== '')
            ->assertJsonPath('data.username', 'pbconnect')
            ->assertJsonPath('data.password', 'SECRET12');

        $this->assertDatabaseHas('photobooth_connect_codes', [
            'event_id' => $event->id,
        ]);
    }

    #[Test]
    public function it_rejects_expired_connect_codes(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'connect-code-expired',
        ]);

        EventPhotoboothSetting::factory()
            ->for($event)
            ->activeSparkbooth()
            ->create([
                'username' => 'pbconnect',
                'password' => 'SECRET12',
            ]);

        $code = '123456';

        PhotoboothConnectCode::query()->create([
            'event_id' => $event->id,
            'code_hash' => hash('sha256', $code),
            'expires_at' => now()->subMinute(),
        ]);

        $response = $this->postJson('/api/v1/photobooth/connect', [
            'code' => $code,
        ]);

        $response->assertStatus(422);
    }
}
