<?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 GroupRelease extends Model
{
    use HasFactory;
    use SoftDeletes;

    public const STATUS_PLANNED = 'planned';
    public const STATUS_IN_PROGRESS = 'in_progress';
    public const STATUS_INTERNAL_REVIEW = 'internal_review';
    public const STATUS_SCHEDULED = 'scheduled';
    public const STATUS_RELEASED = 'released';
    public const STATUS_ARCHIVED = 'archived';
    public const STATUS_CANCELLED = 'cancelled';

    public const STAGE_CONCEPT = 'concept';
    public const STAGE_PRODUCTION = 'production';
    public const STAGE_REVIEW = 'review';
    public const STAGE_PACKAGING = 'packaging';
    public const STAGE_APPROVAL = 'approval';
    public const STAGE_PUBLISHING = 'publishing';
    public const STAGE_RELEASED = 'released';

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

    protected $fillable = [
        'group_id',
        'title',
        'slug',
        'summary',
        'description',
        'cover_path',
        'status',
        'current_stage',
        'visibility',
        'planned_release_at',
        'released_at',
        'lead_user_id',
        'linked_project_id',
        'linked_collection_id',
        'featured_artwork_id',
        'release_notes',
        'created_by_user_id',
        'published_at',
        'is_featured',
    ];

    protected $casts = [
        'planned_release_at' => 'datetime',
        'released_at' => 'datetime',
        'published_at' => 'datetime',
        'is_featured' => 'boolean',
    ];

    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 lead(): BelongsTo
    {
        return $this->belongsTo(User::class, 'lead_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 featuredArtwork(): BelongsTo
    {
        return $this->belongsTo(Artwork::class, 'featured_artwork_id');
    }

    public function artworkLinks(): HasMany
    {
        return $this->hasMany(GroupReleaseArtwork::class)->orderBy('sort_order');
    }

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

    public function contributorLinks(): HasMany
    {
        return $this->hasMany(GroupReleaseContributor::class)->orderBy('sort_order');
    }

    public function contributors(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'group_release_contributors')
            ->withPivot(['role_label', 'sort_order'])
            ->withTimestamps();
    }

    public function milestones(): HasMany
    {
        return $this->hasMany(GroupReleaseMilestone::class)->orderBy('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, '/');
    }
}