<?php

namespace Tests\Feature\Api;

use App\Jobs\Packages\SendEventPackagePhotoLimitNotification;
use App\Jobs\Packages\SendEventPackagePhotoThresholdWarning;
use App\Models\Emotion;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\GuestPolicySetting;
use App\Models\MediaStorageTarget;
use App\Models\Package;
use App\Models\Photo;
use App\Models\Tenant;
use App\Services\EventJoinTokenService;
use App\Services\Packages\PackageLimitEvaluator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class EventGuestUploadLimitTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        Config::set('filesystems.default', 'local');
        Storage::fake('local');

        MediaStorageTarget::query()->create([
            'key' => 'local',
            'name' => 'Local',
            'driver' => 'local',
            'config' => [],
            'is_hot' => true,
            'is_default' => true,
            'is_active' => true,
            'priority' => 1,
        ]);
    }

    public function test_guest_upload_blocked_when_photo_limit_reached(): void
    {
        Bus::fake();

        $tenant = Tenant::factory()->create();
        $event = Event::factory()->for($tenant)->create([
            'status' => 'published',
        ]);

        $package = Package::factory()->endcustomer()->create([
            'max_photos' => 1,
            'max_guests' => null,
        ]);

        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now(),
            'used_photos' => 1,
            'used_guests' => 0,
            'gallery_expires_at' => now()->addDays(7),
        ]);

        $emotion = Emotion::factory()->create();
        $emotion->eventTypes()->attach($event->event_type_id);

        /** @var EventJoinTokenService $tokenService */
        $tokenService = $this->app->make(EventJoinTokenService::class);
        $joinToken = $tokenService->createToken($event, ['label' => 'Test']);
        $token = $joinToken->plain_token;

        $response = $this->post("/api/v1/events/{$token}/upload", [
            'photo' => UploadedFile::fake()->image('limit.jpg', 800, 600),
        ], [
            'X-Device-Id' => 'device-123',
        ]);

        $response->assertStatus(402);
        $response->assertJsonPath('error.code', 'photo_limit_exceeded');

        Bus::assertNothingDispatched();

        $this->assertDatabaseHas('guest_notifications', [
            'event_id' => $event->id,
            'type' => 'upload_alert',
            'audience_scope' => 'all',
        ]);
    }

    public function test_guest_upload_increments_usage_and_succeeds(): void
    {
        Bus::fake();

        $tenant = Tenant::factory()->create();
        $event = Event::factory()->for($tenant)->create([
            'status' => 'published',
        ]);

        $package = Package::factory()->endcustomer()->create([
            'max_photos' => 2,
            'max_guests' => null,
        ]);

        $eventPackage = EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now(),
            'used_photos' => 1,
            'used_guests' => 0,
            'gallery_expires_at' => now()->addDays(7),
        ]);

        $emotion = Emotion::factory()->create();
        $emotion->eventTypes()->attach($event->event_type_id);

        /** @var EventJoinTokenService $tokenService */
        $tokenService = $this->app->make(EventJoinTokenService::class);
        $token = $tokenService->createToken($event, ['label' => 'Test'])->plain_token;

        $response = $this->post("/api/v1/events/{$token}/upload", [
            'photo' => UploadedFile::fake()->image('success.jpg', 1024, 768),
        ], [
            'X-Device-Id' => 'device-456',
        ]);

        $response->assertCreated();
        $this->assertEquals(
            2,
            $eventPackage->refresh()->used_photos
        );

        $thresholdJobs = Bus::dispatched(SendEventPackagePhotoThresholdWarning::class);
        $this->assertGreaterThanOrEqual(2, $thresholdJobs->count());
        Bus::assertDispatched(SendEventPackagePhotoLimitNotification::class);
    }

    public function test_guest_package_endpoint_returns_limits_summary(): void
    {
        $tenant = Tenant::factory()->create();
        $event = Event::factory()->for($tenant)->create([
            'status' => 'published',
        ]);

        $package = Package::factory()->endcustomer()->create([
            'max_photos' => 10,
            'max_guests' => 20,
            'gallery_days' => 7,
        ]);

        $eventPackage = EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now()->subDay(),
            'used_photos' => 8,
            'used_guests' => 5,
            'gallery_expires_at' => now()->addDays(3),
        ]);

        $token = app(EventJoinTokenService::class)->createToken($event)->plain_token;

        $response = $this->getJson("/api/v1/events/{$token}/package");

        $response->assertOk();
        $response->assertJsonPath('id', $eventPackage->id);
        $response->assertJsonPath('limits.photos.limit', 10);
        $response->assertJsonPath('limits.photos.used', 8);
        $response->assertJsonPath('limits.photos.state', 'warning');
        $response->assertJsonPath('limits.gallery.state', 'warning');
        $response->assertJsonPath('limits.can_upload_photos', true);
    }

    public function test_device_limit_creates_targeted_notification(): void
    {
        $tenant = Tenant::factory()->create();
        $event = Event::factory()->for($tenant)->create([
            'status' => 'published',
        ]);

        $mock = \Mockery::mock(PackageLimitEvaluator::class);
        $mock->shouldReceive('assessPhotoUpload')->andReturn(null);
        $mock->shouldReceive('resolveEventPackageForPhotoUpload')->andReturn(null);
        $this->instance(PackageLimitEvaluator::class, $mock);

        Photo::factory()->count(50)->for($event)->create([
            'guest_name' => 'device-abc',
        ]);

        $emotion = Emotion::factory()->create();
        $emotion->eventTypes()->attach($event->event_type_id);

        $token = app(EventJoinTokenService::class)->createToken($event)->plain_token;

        $response = $this->post("/api/v1/events/{$token}/upload", [
            'photo' => UploadedFile::fake()->image('limit-device.jpg', 800, 600),
        ], [
            'X-Device-Id' => 'device-abc',
        ]);

        $response->assertStatus(429);

        $this->assertDatabaseHas('guest_notifications', [
            'event_id' => $event->id,
            'type' => 'upload_alert',
            'target_identifier' => 'device-abc',
            'audience_scope' => 'guest',
        ]);
    }

    public function test_guest_policy_overrides_device_upload_limit(): void
    {
        GuestPolicySetting::flushCache();
        GuestPolicySetting::query()->create([
            'id' => 1,
            'per_device_upload_limit' => 2,
        ]);

        $tenant = Tenant::factory()->create();
        $event = Event::factory()->for($tenant)->create([
            'status' => 'published',
        ]);

        $mock = \Mockery::mock(PackageLimitEvaluator::class);
        $mock->shouldReceive('assessPhotoUpload')->andReturn(null);
        $mock->shouldReceive('resolveEventPackageForPhotoUpload')->andReturn(null);
        $this->instance(PackageLimitEvaluator::class, $mock);

        Photo::factory()->count(2)->for($event)->create([
            'guest_name' => 'device-limit',
        ]);

        $emotion = Emotion::factory()->create();
        $emotion->eventTypes()->attach($event->event_type_id);

        $token = app(EventJoinTokenService::class)->createToken($event)->plain_token;

        $response = $this->post("/api/v1/events/{$token}/upload", [
            'photo' => UploadedFile::fake()->image('limit-device.jpg', 800, 600),
        ], [
            'X-Device-Id' => 'device-limit',
        ]);

        $response->assertStatus(429);
        $response->assertJsonPath('error.meta.limit', 2);
    }

    public function test_photo_limit_violation_creates_notification(): void
    {
        $tenant = Tenant::factory()->create();
        $event = Event::factory()->for($tenant)->create([
            'status' => 'published',
        ]);

        $violation = [
            'code' => 'photo_limit_exceeded',
            'title' => 'Photo upload limit reached',
            'message' => 'Limit reached',
            'status' => 402,
            'meta' => ['scope' => 'photos'],
        ];

        $mock = \Mockery::mock(PackageLimitEvaluator::class);
        $mock->shouldReceive('assessPhotoUpload')->andReturn($violation);
        $mock->shouldReceive('resolveEventPackageForPhotoUpload')->andReturn(null);
        $this->instance(PackageLimitEvaluator::class, $mock);

        $emotion = Emotion::factory()->create();
        $emotion->eventTypes()->attach($event->event_type_id);

        $token = app(EventJoinTokenService::class)->createToken($event)->plain_token;

        $response = $this->post("/api/v1/events/{$token}/upload", [
            'photo' => UploadedFile::fake()->image('limit.jpg', 800, 600),
        ], [
            'X-Device-Id' => 'device-xyz',
        ]);

        $response->assertStatus(402);

        $this->assertDatabaseHas('guest_notifications', [
            'event_id' => $event->id,
            'type' => 'upload_alert',
            'audience_scope' => 'all',
        ]);
    }
}
