<?php

namespace Tests\Feature;

use App\Filament\Clusters\DailyOps\Resources\Photos\Pages\ListPhotos;
use App\Models\Event;
use App\Models\Photo;
use App\Models\SuperAdminActionLog;
use App\Models\Tenant;
use App\Models\User;
use Filament\Actions\Testing\TestAction;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class PhotoModerationQueueTest extends TestCase
{
    use RefreshDatabase;

    public function test_superadmin_can_approve_pending_photo(): void
    {
        $user = User::factory()->create(['role' => 'super_admin']);
        $tenant = Tenant::factory()->create();
        $event = Event::factory()->create(['tenant_id' => $tenant->id]);
        $photo = Photo::factory()->for($event)->create([
            'status' => 'pending',
            'ingest_source' => Photo::SOURCE_GUEST_PWA,
        ]);

        $this->bootSuperAdminPanel($user);

        Livewire::test(ListPhotos::class)
            ->callAction(TestAction::make('approve')->table($photo), [
                'moderation_notes' => 'Looks good.',
            ]);

        $photo->refresh();

        $this->assertSame('approved', $photo->status);
        $this->assertSame('Looks good.', $photo->moderation_notes);
        $this->assertNotNull($photo->moderated_at);
        $this->assertSame($user->id, $photo->moderated_by);
        $this->assertTrue(SuperAdminActionLog::query()
            ->where('action', 'photo.approved')
            ->where('subject_id', $photo->id)
            ->exists());
    }

    public function test_superadmin_can_bulk_reject_pending_photos(): void
    {
        $user = User::factory()->create(['role' => 'super_admin']);
        $tenant = Tenant::factory()->create();
        $event = Event::factory()->create(['tenant_id' => $tenant->id]);
        $photoA = Photo::factory()->for($event)->create([
            'status' => 'pending',
            'ingest_source' => Photo::SOURCE_GUEST_PWA,
        ]);
        $photoB = Photo::factory()->for($event)->create([
            'status' => 'pending',
            'ingest_source' => Photo::SOURCE_GUEST_PWA,
        ]);

        $this->bootSuperAdminPanel($user);

        Livewire::test(ListPhotos::class)
            ->callTableBulkAction('reject', [$photoA, $photoB], [
                'moderation_notes' => 'Policy violation.',
            ]);

        $photoA->refresh();
        $photoB->refresh();

        $this->assertSame('rejected', $photoA->status);
        $this->assertSame('rejected', $photoB->status);
        $this->assertSame('Policy violation.', $photoA->moderation_notes);
        $this->assertSame('Policy violation.', $photoB->moderation_notes);
        $this->assertNotNull($photoA->moderated_at);
        $this->assertNotNull($photoB->moderated_at);
        $this->assertSame($user->id, $photoA->moderated_by);
        $this->assertSame($user->id, $photoB->moderated_by);
        $this->assertTrue(SuperAdminActionLog::query()
            ->where('action', 'photo.rejected')
            ->whereIn('subject_id', [$photoA->id, $photoB->id])
            ->count() === 2);
    }

    private function bootSuperAdminPanel(User $user): void
    {
        $panel = Filament::getPanel('superadmin');

        $this->assertNotNull($panel);

        Filament::setCurrentPanel($panel);
        Filament::bootCurrentPanel();
        Filament::auth()->login($user);
    }
}
