<?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\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class GroupChallenge extends Model
{
    use HasFactory;
    use SoftDeletes;

    public const VISIBILITY_PUBLIC = 'public';
    public const VISIBILITY_UNLISTED = 'unlisted';
    public const VISIBILITY_PRIVATE = 'private';

    public const PARTICIPATION_GROUP_ONLY = 'group_only';
    public const PARTICIPATION_INVITE_ONLY = 'invite_only';
    public const PARTICIPATION_PUBLIC = 'public';

    public const STATUS_DRAFT = 'draft';
    public const STATUS_PUBLISHED = 'published';
    public const STATUS_ACTIVE = 'active';
    public const STATUS_ENDED = 'ended';
    public const STATUS_ARCHIVED = 'archived';

    protected $fillable = [
        'group_id',
        'title',
        'slug',
        'summary',
        'description',
        'cover_path',
        'visibility',
        'participation_scope',
        'status',
        'start_at',
        'end_at',
        'rules_text',
        'submission_instructions',
        'judging_mode',
        'linked_collection_id',
        'linked_project_id',
        'created_by_user_id',
        'featured_artwork_id',
    ];

    protected $casts = [
        'start_at' => 'datetime',
        'end_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 linkedCollection(): BelongsTo
    {
        return $this->belongsTo(Collection::class, 'linked_collection_id');
    }

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

    public function featuredArtwork(): BelongsTo
    {
        return $this->belongsTo(Artwork::class, 'featured_artwork_id');
    }

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

    public function artworks(): BelongsToMany
    {
        return $this->belongsToMany(Artwork::class, 'group_challenge_artworks')
            ->withPivot(['submitted_by_user_id', 'sort_order'])
            ->withTimestamps()
            ->orderBy('group_challenge_artworks.sort_order');
    }

    public function canBeViewedBy(?User $viewer): bool
    {
        if ($this->visibility !== self::VISIBILITY_PRIVATE) {
            return $this->group->canBeViewedBy($viewer);
        }

        return $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, '/');
    }
}