<?php

namespace App\Models;

use App\Enums\ApprovalMode;
use App\Enums\EventStatus;
use App\Enums\EventType;
use App\Enums\LocationType;
use App\Enums\ParticipationType;
use App\Enums\PricingType;
use App\Support\PublicId;
use Database\Factories\EventFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Event extends Model
{
    /** @use HasFactory<EventFactory> */
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'public_id',
        'game_id',
        'created_by',
        'title',
        'slug',
        'short_description',
        'description',
        'cover_image',
        'event_type',
        'participation_type',
        'location_type',
        'venue_name',
        'venue_address',
        'online_details',
        'timezone',
        'registration_opens_at',
        'registration_closes_at',
        'starts_at',
        'ends_at',
        'capacity',
        'waitlist_enabled',
        'approval_mode',
        'pricing_type',
        'fee_amount_minor',
        'currency',
        'payment_instructions',
        'rules',
        'prizes',
        'contact_information',
        'status',
        'published_at',
    ];

    public function getRouteKeyName(): string
    {
        return 'slug';
    }

    protected function casts(): array
    {
        return [
            'event_type' => EventType::class,
            'participation_type' => ParticipationType::class,
            'location_type' => LocationType::class,
            'registration_opens_at' => 'datetime',
            'registration_closes_at' => 'datetime',
            'starts_at' => 'datetime',
            'ends_at' => 'datetime',
            'capacity' => 'integer',
            'waitlist_enabled' => 'boolean',
            'approval_mode' => ApprovalMode::class,
            'pricing_type' => PricingType::class,
            'fee_amount_minor' => 'integer',
            'status' => EventStatus::class,
            'published_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (Event $event): void {
            if (empty($event->public_id)) {
                $event->public_id = PublicId::generate();
            }
        });
    }

    public function game(): BelongsTo
    {
        return $this->belongsTo(Game::class);
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function translations(): HasMany
    {
        return $this->hasMany(EventTranslation::class);
    }

    public function eventRules(): HasMany
    {
        return $this->hasMany(EventRule::class);
    }

    public function prizes(): HasMany
    {
        return $this->hasMany(EventPrize::class);
    }

    public function paymentMethods(): HasMany
    {
        return $this->hasMany(EventPaymentMethod::class);
    }

    public function registrations(): HasMany
    {
        return $this->hasMany(EventRegistration::class);
    }
}
