<?php

declare(strict_types=1);

namespace App\Pivots\Wiki;

use App\Events\Pivot\Wiki\AnimeStudio\AnimeStudioCreated;
use App\Events\Pivot\Wiki\AnimeStudio\AnimeStudioDeleted;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Studio;
use App\Pivots\BasePivot;
use Database\Factories\Pivots\Wiki\AnimeStudioFactory;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use OwenIt\Auditing\Auditable as HasAudits;
use OwenIt\Auditing\Contracts\Auditable;

/**
 * @property Anime $anime
 * @property int $anime_id
 * @property Studio $studio
 * @property int $studio_id
 *
 * @method static AnimeStudioFactory factory(...$parameters)
 */
#[Table(AnimeStudio::TABLE)]
class AnimeStudio extends BasePivot implements Auditable
{
    use HasAudits;

    final public const string TABLE = 'anime_studio';

    final public const string ATTRIBUTE_ANIME = 'anime_id';
    final public const string ATTRIBUTE_STUDIO = 'studio_id';

    final public const string RELATION_ANIME = 'anime';
    final public const string RELATION_STUDIO = 'studio';

    /**
     * The event map for the model.
     *
     * Allows for object-based events for native Eloquent events.
     *
     * @var array<string, class-string>
     */
    protected $dispatchesEvents = [
        'created' => AnimeStudioCreated::class,
        'deleted' => AnimeStudioDeleted::class,
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        AnimeStudio::ATTRIBUTE_ANIME,
        AnimeStudio::ATTRIBUTE_STUDIO,
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            AnimeStudio::ATTRIBUTE_ANIME => 'int',
            AnimeStudio::ATTRIBUTE_STUDIO => 'int',
        ];
    }

    /**
     * Gets the anime that owns the anime studio.
     *
     * @return BelongsTo<Anime, $this>
     */
    public function anime(): BelongsTo
    {
        return $this->belongsTo(Anime::class, AnimeStudio::ATTRIBUTE_ANIME);
    }

    /**
     * Gets the studio that owns the anime studio.
     *
     * @return BelongsTo<Studio, $this>
     */
    public function studio(): BelongsTo
    {
        return $this->belongsTo(Studio::class, AnimeStudio::ATTRIBUTE_STUDIO);
    }
}
