<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Hash;

class MeetupEvent extends Model
{
    public $incrementing = false;

    protected $keyType = 'string';

    protected $table = 'meetup_events';

    protected $fillable = [
        'id',
        'organisation_id',
        'name',
        'description',
        'event_type',
        'expected_participants',
        'contact_person',
        'contact_email',
        'venue',
        'dates',
        'event_name',
        'short_name',
        'banner_path',
        'floor_plan_path',
        'signer',
        'signer_title',
        'certificate_templates',
        'mail_endpoint',
        'passcode',
        'organiser_username',
        'organiser_password',
        'is_virtual',
        'allow_voting',
        'allow_floor_plan',
        'starts_at',
        'ends_at',
        'platform_status',
        'archived_at',
    ];

    protected $hidden = [
        'organiser_password',
        'passcode',
    ];

    protected function casts(): array
    {
        return [
            'organiser_password' => 'hashed',
            'certificate_templates' => 'array',
            'is_virtual' => 'boolean',
            'allow_voting' => 'boolean',
            'allow_floor_plan' => 'boolean',
            'starts_at' => 'datetime',
            'ends_at' => 'datetime',
            'archived_at' => 'datetime',
        ];
    }

    public function fullName(): string
    {
        return trim((string) ($this->event_name ?: $this->name)) ?: 'Event';
    }

    public function shortName(): string
    {
        return trim((string) ($this->short_name ?: $this->name)) ?: $this->fullName();
    }

    public function bannerUrl(): ?string
    {
        if (! $this->banner_path) {
            return null;
        }

        return asset('storage/'.$this->banner_path);
    }

    public function floorPlanUrl(): ?string
    {
        if (! $this->allow_floor_plan || ! $this->floor_plan_path) {
            return null;
        }

        return asset('storage/'.$this->floor_plan_path);
    }

    public function verifyOrganiserPassword(string $password): bool
    {
        if ($this->organiser_password) {
            return Hash::check($password, $this->organiser_password);
        }

        return $this->passcode === $password;
    }

    public function organisation(): BelongsTo
    {
        return $this->belongsTo(MexcoOrganisation::class, 'organisation_id');
    }

    public function organisationUsers(): BelongsToMany
    {
        return $this->belongsToMany(
            MexcoOrganisationUser::class,
            'mexco_organisation_user_events',
            'event_id',
            'organisation_user_id'
        )->withPivot(['role'])->withTimestamps();
    }

    public function roles(): HasMany
    {
        return $this->hasMany(MeetupRole::class, 'event_id');
    }

    public function booths(): HasMany
    {
        return $this->hasMany(MeetupBooth::class, 'event_id');
    }

    public function sessions(): HasMany
    {
        return $this->hasMany(MeetupSession::class, 'event_id');
    }

    public function participants(): HasMany
    {
        return $this->hasMany(MeetupParticipant::class, 'event_id');
    }

    public function license(): HasOne
    {
        return $this->hasOne(MexcoEventLicense::class, 'event_id');
    }

    public function entitlements(): HasMany
    {
        return $this->hasMany(MexcoEventEntitlement::class, 'event_id');
    }

    public function usage(): HasOne
    {
        return $this->hasOne(MexcoEventUsage::class, 'event_id');
    }
}
