<?php

namespace Tests\Feature;

use App\Enums\PhotoLiveStatus;
use App\Models\Event;
use App\Models\Photo;
use Tests\Feature\Tenant\TenantTestCase;

class LiveShowPhotoControllerTest extends TenantTestCase
{
    public function test_live_show_queue_defaults_to_pending(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'live-show-queue',
        ]);

        $pending = Photo::factory()->for($event)->create([
            'status' => 'approved',
            'live_status' => PhotoLiveStatus::PENDING,
        ]);

        Photo::factory()->for($event)->create([
            'status' => 'approved',
            'live_status' => PhotoLiveStatus::APPROVED,
            'live_approved_at' => now(),
        ]);

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

        $response->assertOk();
        $response->assertJsonCount(1, 'data');
        $response->assertJsonPath('data.0.id', $pending->id);
        $response->assertJsonPath('data.0.live_status', PhotoLiveStatus::PENDING->value);
    }

    public function test_live_show_approve_requires_gallery_approval(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'live-show-approve-guard',
        ]);

        $photo = Photo::factory()->for($event)->create([
            'status' => 'pending',
            'live_status' => PhotoLiveStatus::PENDING,
        ]);

        $response = $this->authenticatedRequest(
            'POST',
            "/api/v1/tenant/events/{$event->slug}/live-show/photos/{$photo->id}/approve"
        );

        $response->assertStatus(422);
        $response->assertJsonPath('error.code', 'photo_not_approved');
    }

    public function test_live_show_approve_and_live_updates_gallery_and_live_status(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'live-show-approve-live',
        ]);

        $photo = Photo::factory()->for($event)->create([
            'status' => 'pending',
            'live_status' => PhotoLiveStatus::PENDING,
        ]);

        $response = $this->authenticatedRequest(
            'POST',
            "/api/v1/tenant/events/{$event->slug}/live-show/photos/{$photo->id}/approve-and-live",
            ['priority' => 9]
        );

        $response->assertOk();
        $this->assertDatabaseHas('photos', [
            'id' => $photo->id,
            'status' => 'approved',
            'moderated_by' => $this->tenantUser->id,
            'live_status' => PhotoLiveStatus::APPROVED->value,
            'live_priority' => 9,
        ]);
    }

    public function test_live_show_approve_reject_and_clear_workflow(): void
    {
        $event = Event::factory()->for($this->tenant)->create([
            'slug' => 'live-show-workflow',
        ]);

        $photo = Photo::factory()->for($event)->create([
            'status' => 'approved',
            'live_status' => PhotoLiveStatus::PENDING,
        ]);

        $approve = $this->authenticatedRequest(
            'POST',
            "/api/v1/tenant/events/{$event->slug}/live-show/photos/{$photo->id}/approve",
            ['priority' => 12]
        );

        $approve->assertOk();
        $this->assertDatabaseHas('photos', [
            'id' => $photo->id,
            'live_status' => PhotoLiveStatus::APPROVED->value,
            'live_priority' => 12,
        ]);

        $reject = $this->authenticatedRequest(
            'POST',
            "/api/v1/tenant/events/{$event->slug}/live-show/photos/{$photo->id}/reject",
            ['reason' => 'policy']
        );

        $reject->assertOk();
        $this->assertDatabaseHas('photos', [
            'id' => $photo->id,
            'live_status' => PhotoLiveStatus::REJECTED->value,
            'live_rejection_reason' => 'policy',
        ]);

        $clear = $this->authenticatedRequest(
            'POST',
            "/api/v1/tenant/events/{$event->slug}/live-show/photos/{$photo->id}/clear"
        );

        $clear->assertOk();
        $this->assertDatabaseHas('photos', [
            'id' => $photo->id,
            'live_status' => PhotoLiveStatus::NONE->value,
        ]);
    }
}
