<?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\HasMany;

class WorldSubmission extends Model
{
    use HasFactory;

    public const STATUS_PENDING = 'pending';
    public const STATUS_LIVE = 'live';
    public const STATUS_REMOVED = 'removed';
    public const STATUS_BLOCKED = 'blocked';

    protected $fillable = [
        'world_id',
        'artwork_id',
        'submitted_by_user_id',
        'status',
        'is_featured',
        'mode_snapshot',
        'note',
        'reviewer_note',
        'moderation_reason',
        'reviewed_by_user_id',
        'reviewed_at',
        'removed_at',
        'blocked_at',
        'featured_at',
    ];

    protected $casts = [
        'is_featured' => 'boolean',
        'reviewed_at' => 'datetime',
        'removed_at' => 'datetime',
        'blocked_at' => 'datetime',
        'featured_at' => 'datetime',
    ];

    public function canBeReadded(): bool
    {
        return (string) $this->status === self::STATUS_REMOVED;
    }

    public function isBlockingResubmission(): bool
    {
        return (string) $this->status === self::STATUS_BLOCKED;
    }

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

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

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

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

    public function worldRewardGrants(): HasMany
    {
        return $this->hasMany(WorldRewardGrant::class, 'world_submission_id')->orderByDesc('granted_at')->orderByDesc('id');
    }
}