<?php

declare(strict_types=1);

namespace App\Pivots\Wiki;

use App\Events\Pivot\Wiki\AnimeSeries\AnimeSeriesCreated;
use App\Events\Pivot\Wiki\AnimeSeries\AnimeSeriesDeleted;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Series;
use App\Pivots\BasePivot;
use Database\Factories\Pivots\Wiki\AnimeSeriesFactory;
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 Series $series
 * @property int $series_id
 *
 * @method static AnimeSeriesFactory factory(...$parameters)
 */
#[Table(AnimeSeries::TABLE)]
class AnimeSeries extends BasePivot implements Auditable
{
    use HasAudits;

    final public const string TABLE = 'anime_series';

    final public const string ATTRIBUTE_ANIME = 'anime_id';
    final public const string ATTRIBUTE_SERIES = 'series_id';

    final public const string RELATION_ANIME = 'anime';
    final public const string RELATION_SERIES = 'series';

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

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

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

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

    /**
     * Gets the series that owns the anime series.
     *
     * @return BelongsTo<Series, $this>
     */
    public function series(): BelongsTo
    {
        return $this->belongsTo(Series::class, AnimeSeries::ATTRIBUTE_SERIES);
    }
}
