<?php

namespace Tests\Feature\Tenant;

use App\Models\Event;
use App\Models\EventType;
use App\Models\Package;
use App\Models\TenantPackage;
use Illuminate\Support\Carbon;

class EventManagementTest extends TenantTestCase
{
    public function test_event_types_endpoint_returns_translated_types(): void
    {
        $types = EventType::factory()->count(2)->create([
            'name' => [
                'de' => 'Feier',
                'en' => 'Celebration',
            ],
            'icon' => 'party',
        ]);

        $response = $this->authenticatedRequest('GET', '/api/v1/tenant/event-types');

        $response->assertOk();
        $payload = $response->json('data');

        $this->assertCount(2, $payload);
        $first = collect($payload)->firstWhere('id', $types->first()->id);

        $this->assertNotNull($first, 'Expected event type to be present');
        $this->assertSame('Feier', $first['name']);
        $this->assertArrayHasKey('slug', $first);
        $this->assertArrayHasKey('name_translations', $first);
        $this->assertArrayHasKey('icon', $first);
        $this->assertArrayHasKey('settings', $first);
    }

    public function test_event_can_be_created_with_event_type_and_date(): void
    {
        $eventType = EventType::factory()->create([
            'name' => [
                'de' => 'Hochzeit',
                'en' => 'Wedding',
            ],
            'icon' => 'ring',
        ]);

        $package = Package::factory()->endcustomer()->create([
            'price' => 79.90,
        ]);

        TenantPackage::factory()
            ->for($this->tenant)
            ->for($package)
            ->create([
                'used_events' => 0,
                'active' => true,
            ]);

        $payload = [
            'name' => 'Launch Event',
            'slug' => 'launch-event',
            'event_type_id' => $eventType->id,
            'event_date' => Carbon::now()->addDays(10)->toDateString(),
            'status' => 'draft',
            'accepted_waiver' => true,
        ];

        $response = $this->authenticatedRequest('POST', '/api/v1/tenant/events', $payload);

        $response->assertCreated();

        $response->assertJsonPath('data.slug', 'launch-event');
        $response->assertJsonPath('data.event_type_id', $eventType->id);

        $this->assertDatabaseHas(Event::class, [
            'slug' => 'launch-event',
            'event_type_id' => $eventType->id,
            'tenant_id' => $this->tenant->id,
        ]);
    }
}
