<?php

namespace App\Models;

use App\Enums\ModerationStatus;
use App\Enums\ProfileVisibility;
use App\Enums\VideoProvider;
use App\Enums\VideoType;
use App\Support\PublicId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class PlayerVideo extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'public_id',
        'player_profile_id',
        'player_game_profile_id',
        'game_id',
        'title',
        'description',
        'original_url',
        'provider',
        'external_video_id',
        'normalized_url',
        'safe_embed_url',
        'thumbnail_url',
        'video_type',
        'visibility',
        'moderation_status',
        'moderation_reason',
        'published_at',
    ];

    public function getRouteKeyName(): string
    {
        return 'public_id';
    }

    protected function casts(): array
    {
        return [
            'provider' => VideoProvider::class,
            'video_type' => VideoType::class,
            'visibility' => ProfileVisibility::class,
            'moderation_status' => ModerationStatus::class,
            'published_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (PlayerVideo $video): void {
            if (empty($video->public_id)) {
                $video->public_id = PublicId::generate();
            }
        });
    }

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

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

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