<?php

namespace App\Actions\Events;

use App\Enums\EventStatus;
use App\Enums\EventType;
use App\Enums\LocationType;
use App\Enums\ParticipationType;
use App\Enums\PricingType;
use App\Models\Event;
use App\Models\User;
use App\Services\AuditLogger;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class CreateEventAction
{
    public function __construct(private AuditLogger $auditLogger) {}

    public function execute(User $admin, array $data): Event
    {
        return DB::transaction(function () use ($admin, $data) {
            $slug = $this->uniqueSlug($data['title']);

            $event = Event::query()->create([
                'game_id' => $data['game_id'],
                'created_by' => $admin->id,
                'title' => $data['title'],
                'slug' => $slug,
                'short_description' => $data['short_description'] ?? null,
                'description' => $data['description'] ?? null,
                'cover_image' => $data['cover_image'] ?? null,
                'event_type' => EventType::from($data['event_type'] ?? EventType::Tournament->value),
                'participation_type' => ParticipationType::from($data['participation_type'] ?? ParticipationType::Individual->value),
                'location_type' => LocationType::from($data['location_type'] ?? LocationType::Online->value),
                'venue_name' => $data['venue_name'] ?? null,
                'venue_address' => $data['venue_address'] ?? null,
                'online_details' => $data['online_details'] ?? null,
                'timezone' => $data['timezone'] ?? 'UTC',
                'registration_opens_at' => $data['registration_opens_at'] ?? null,
                'registration_closes_at' => $data['registration_closes_at'] ?? null,
                'starts_at' => $data['starts_at'],
                'ends_at' => $data['ends_at'],
                'capacity' => $data['capacity'] ?? null,
                'waitlist_enabled' => (bool) ($data['waitlist_enabled'] ?? false),
                'approval_mode' => $data['approval_mode'],
                'pricing_type' => PricingType::from($data['pricing_type']),
                'fee_amount_minor' => $data['fee_amount_minor'] ?? null,
                'currency' => $data['currency'] ?? null,
                'payment_instructions' => $data['payment_instructions'] ?? null,
                'rules' => $data['rules'] ?? null,
                'prizes' => $data['prizes'] ?? null,
                'contact_information' => $data['contact_information'] ?? null,
                'status' => EventStatus::Draft,
            ]);

            $this->auditLogger->log('event.created', $event, null, $event->only(['title', 'slug', 'status']));

            return $event->load('game');
        });
    }

    private function uniqueSlug(string $title): string
    {
        $base = Str::slug($title);
        $slug = $base;
        $counter = 1;

        while (Event::query()->where('slug', $slug)->exists()) {
            $slug = $base.'-'.$counter;
            $counter++;
        }

        return $slug;
    }
}
