<?php

declare(strict_types=1);

namespace App\Models\List\External;

use App\Enums\Models\List\ExternalEntryStatus;
use App\Models\BaseModel;
use App\Models\List\ExternalProfile;
use App\Models\Wiki\Anime;
use Database\Factories\List\External\ExternalEntryFactory;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * @property int $entry_id
 * @property int|null $anime_id
 * @property Anime|null $anime
 * @property int $profile_id
 * @property ExternalProfile $externalprofile
 * @property bool $is_favorite
 * @property float|null $score
 * @property ExternalEntryStatus $status
 *
 * @method static ExternalEntryFactory factory(...$parameters)
 */
#[Table(ExternalEntry::TABLE, ExternalEntry::ATTRIBUTE_ID)]
class ExternalEntry extends BaseModel
{
    use HasFactory;

    final public const string TABLE = 'external_entries';

    final public const string ATTRIBUTE_ID = 'entry_id';
    final public const string ATTRIBUTE_ANIME = 'anime_id';
    final public const string ATTRIBUTE_PROFILE = 'profile_id';
    final public const string ATTRIBUTE_IS_FAVORITE = 'is_favorite';
    final public const string ATTRIBUTE_SCORE = 'score';
    final public const string ATTRIBUTE_STATUS = 'status';

    final public const string RELATION_ANIME = 'anime';
    final public const string RELATION_PROFILE = 'externalprofile';
    final public const string RELATION_USER = 'externalprofile.user';

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        ExternalEntry::ATTRIBUTE_ANIME,
        ExternalEntry::ATTRIBUTE_PROFILE,
        ExternalEntry::ATTRIBUTE_IS_FAVORITE,
        ExternalEntry::ATTRIBUTE_SCORE,
        ExternalEntry::ATTRIBUTE_STATUS,
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            ExternalEntry::ATTRIBUTE_ANIME => 'int',
            ExternalEntry::ATTRIBUTE_PROFILE => 'int',
            ExternalEntry::ATTRIBUTE_IS_FAVORITE => 'bool',
            ExternalEntry::ATTRIBUTE_SCORE => 'float',
            ExternalEntry::ATTRIBUTE_STATUS => ExternalEntryStatus::class,
        ];
    }

    public function getName(): string
    {
        return strval($this->getKey());
    }

    public function getSubtitle(): string
    {
        return $this->anime->getName();
    }

    /**
     * Get the fields to perform an update.
     *
     * @return string[]
     */
    public static function fieldsForUpdate(): array
    {
        return [
            ExternalEntry::ATTRIBUTE_IS_FAVORITE,
            ExternalEntry::ATTRIBUTE_SCORE,
            ExternalEntry::ATTRIBUTE_STATUS,
        ];
    }

    /**
     * @return BelongsTo<Anime, $this>
     */
    public function anime(): BelongsTo
    {
        return $this->belongsTo(Anime::class, ExternalEntry::ATTRIBUTE_ANIME);
    }

    /**
     * @return BelongsTo<ExternalProfile, $this>
     */
    public function externalprofile(): BelongsTo
    {
        return $this->belongsTo(ExternalProfile::class, ExternalEntry::ATTRIBUTE_PROFILE);
    }
}
