<?php

namespace Tests\Feature;

use App\Models\Event;
use App\Models\EventPackage;
use App\Models\Package;
use App\Models\Tenant;
use App\Models\TenantPackage;
use App\Models\User;
use App\Services\EventJoinTokenService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class EventControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_event_with_valid_package_succeeds(): void
    {
        $tenant = Tenant::factory()->create();
        $user = User::factory()->create(['tenant_id' => $tenant->id]);
        $package = Package::factory()->create(['type' => 'endcustomer', 'max_photos' => 100]);

        $response = $this->actingAs($user)
            ->postJson('/api/v1/tenant/events', [
                'name' => 'Test Event',
                'slug' => 'test-event',
                'date' => '2025-10-01',
                'package_id' => $package->id,
            ]);

        $response->assertStatus(201);

        $this->assertDatabaseHas('events', [
            'tenant_id' => $tenant->id,
            'name' => 'Test Event',
            'slug' => 'test-event',
        ]);

        $event = Event::latest()->first();
        $this->assertDatabaseHas('event_packages', [
            'event_id' => $event->id,
            'package_id' => $package->id,
        ]);

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

        $this->assertDatabaseHas('package_purchases', [
            'event_id' => $event->id,
            'package_id' => $package->id,
            'type' => 'endcustomer_event',
            'provider' => 'manual',
            'provider_id' => 'manual',
        ]);
    }

    public function test_create_event_without_package_fails(): void
    {
        $tenant = Tenant::factory()->create();
        $user = User::factory()->create(['tenant_id' => $tenant->id]);

        $response = $this->actingAs($user)
            ->postJson('/api/v1/tenant/events', [
                'name' => 'Test Event',
                'slug' => 'test-event',
                'date' => '2025-10-01',
            ]);

        $response->assertStatus(422)
            ->assertJsonValidationErrors(['package_id']);
    }

    public function test_create_event_with_reseller_package_limits_events(): void
    {
        $tenant = Tenant::factory()->create();
        $user = User::factory()->create(['tenant_id' => $tenant->id]);
        $package = Package::factory()->create(['type' => 'reseller', 'max_events_per_year' => 1]);
        TenantPackage::factory()->create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'used_events' => 0,
            'active' => true,
            'expires_at' => now()->addYear(),
        ]);

        // First event succeeds
        $response1 = $this->actingAs($user)
            ->postJson('/api/v1/tenant/events', [
                'name' => 'First Event',
                'slug' => 'first-event',
                'date' => '2025-10-01',
                'package_id' => $package->id, // Use reseller package for event? Adjust if needed
            ]);

        $response1->assertStatus(201);

        // Second event fails due to limit
        $response2 = $this->actingAs($user)
            ->postJson('/api/v1/tenant/events', [
                'name' => 'Second Event',
                'slug' => 'second-event',
                'date' => '2025-10-02',
                'package_id' => $package->id,
            ]);

        $response2->assertStatus(402)
            ->assertJson(['error' => 'No available package for event creation']);
    }

    public function test_upload_exceeds_package_limit_fails(): void
    {
        $tenant = Tenant::factory()->create();
        $event = Event::factory()->create(['tenant_id' => $tenant->id, 'status' => 'published']);
        $package = Package::factory()->create(['type' => 'endcustomer', 'max_photos' => 0]); // Limit 0
        EventPackage::factory()->create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'used_photos' => 0,
        ]);

        Storage::fake('public');
        $token = app(EventJoinTokenService::class)->createToken($event);

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

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