<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class GroupEvent extends Model
{
    use HasFactory;
    use SoftDeletes;

    public const TYPE_LAUNCH = 'launch';
    public const TYPE_CHALLENGE = 'challenge';
    public const TYPE_LIVESTREAM = 'livestream';
    public const TYPE_MEETUP = 'meetup';
    public const TYPE_MILESTONE = 'milestone';
    public const TYPE_SHOWCASE = 'showcase';
    public const TYPE_INTERNAL_SESSION = 'internal_session';
    public const TYPE_RELEASE_WINDOW = 'release_window';

    public const VISIBILITY_PUBLIC = 'public';
    public const VISIBILITY_MEMBERS_ONLY = 'members_only';
    public const VISIBILITY_PRIVATE = 'private';

    public const STATUS_DRAFT = 'draft';
    public const STATUS_PUBLISHED = 'published';
    public const STATUS_ARCHIVED = 'archived';
    public const STATUS_CANCELLED = 'cancelled';

    protected $fillable = [
        'group_id',
        'title',
        'slug',
        'summary',
        'description',
        'event_type',
        'visibility',
        'start_at',
        'end_at',
        'timezone',
        'cover_path',
        'location',
        'external_url',
        'linked_project_id',
        'linked_collection_id',
        'linked_challenge_id',
        'status',
        'is_featured',
        'created_by_user_id',
        'published_at',
    ];

    protected $casts = [
        'start_at' => 'datetime',
        'end_at' => 'datetime',
        'is_featured' => 'boolean',
        'published_at' => 'datetime',
    ];

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

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

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

    public function linkedProject(): BelongsTo
    {
        return $this->belongsTo(GroupProject::class, 'linked_project_id');
    }

    public function linkedCollection(): BelongsTo
    {
        return $this->belongsTo(Collection::class, 'linked_collection_id');
    }

    public function linkedChallenge(): BelongsTo
    {
        return $this->belongsTo(GroupChallenge::class, 'linked_challenge_id');
    }

    public function canBeViewedBy(?User $viewer): bool
    {
        return match ($this->visibility) {
            self::VISIBILITY_PUBLIC => $this->group->canBeViewedBy($viewer),
            self::VISIBILITY_MEMBERS_ONLY => $viewer !== null && $this->group->hasActiveMember($viewer),
            default => $viewer !== null && $this->group->canViewStudio($viewer),
        };
    }

    public function coverUrl(): ?string
    {
        $path = trim((string) $this->cover_path);

        if ($path === '') {
            return null;
        }

        if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
            return $path;
        }

        return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/') . '/' . ltrim($path, '/');
    }
}