<?php

namespace Tests\Feature;

use App\Enums\PhotoLiveStatus;
use App\Models\Event;
use App\Models\Photo;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LiveShowDataModelTest extends TestCase
{
    use RefreshDatabase;

    public function test_event_can_ensure_and_rotate_live_show_token(): void
    {
        $event = Event::factory()->create();

        $token = $event->ensureLiveShowToken();

        $this->assertIsString($token);
        $this->assertSame(64, strlen($token));
        $this->assertSame($token, $event->refresh()->live_show_token);
        $this->assertNotNull($event->live_show_token_rotated_at);

        $rotated = $event->rotateLiveShowToken();

        $this->assertIsString($rotated);
        $this->assertSame(64, strlen($rotated));
        $this->assertNotSame($token, $rotated);
        $this->assertSame($rotated, $event->refresh()->live_show_token);
    }

    public function test_photo_live_status_is_cast_and_defaults_to_none(): void
    {
        $photo = Photo::factory()->create();
        $photo->refresh();

        $this->assertInstanceOf(PhotoLiveStatus::class, $photo->live_status);
        $this->assertSame(PhotoLiveStatus::NONE, $photo->live_status);

        $photo->forceFill([
            'live_status' => PhotoLiveStatus::PENDING,
            'live_submitted_at' => now(),
        ])->save();

        $photo->refresh();

        $this->assertSame(PhotoLiveStatus::PENDING, $photo->live_status);
        $this->assertNotNull($photo->live_submitted_at);
    }

    public function test_photo_live_workflow_sets_expected_timestamps_and_reviewer(): void
    {
        $reviewer = User::factory()->create();
        $photo = Photo::factory()->create();

        $photo->markLivePending();
        $photo->refresh();

        $this->assertSame(PhotoLiveStatus::PENDING, $photo->live_status);
        $this->assertNotNull($photo->live_submitted_at);
        $this->assertNull($photo->live_reviewed_at);
        $this->assertNull($photo->live_approved_at);

        $photo->approveForLiveShow($reviewer);
        $photo->refresh();

        $this->assertSame(PhotoLiveStatus::APPROVED, $photo->live_status);
        $this->assertNotNull($photo->live_reviewed_at);
        $this->assertNotNull($photo->live_approved_at);
        $this->assertSame($reviewer->id, $photo->live_reviewed_by);
        $this->assertNull($photo->live_rejection_reason);

        $photo->rejectForLiveShow($reviewer, 'policy_violation');
        $photo->refresh();

        $this->assertSame(PhotoLiveStatus::REJECTED, $photo->live_status);
        $this->assertNotNull($photo->live_reviewed_at);
        $this->assertNull($photo->live_approved_at);
        $this->assertSame($reviewer->id, $photo->live_reviewed_by);
        $this->assertSame('policy_violation', $photo->live_rejection_reason);
    }
}
