<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class VideoSlide extends Model
{
    protected $fillable = ['video_id', 'audio_track_id', 'filename', 'position'];

    public function video()
    {
        return $this->belongsTo(Video::class);
    }

    public function audioTrack()
    {
        return $this->belongsTo(VideoAudioTrack::class, 'audio_track_id');
    }

    public function getUrlAttribute(): string
    {
        return route('media.thumbnail', $this->filename);
    }

    /**
     * Absolute path to the slide image on local disk.
     * Old format: storage/app/public/thumbnails/{filename}
     * New format: storage/app/{relative-path}  (filename contains a slash)
     */
    public function localPath(): string
    {
        return str_contains($this->filename, '/')
            ? storage_path('app/' . $this->filename)
            : storage_path('app/public/thumbnails/' . $this->filename);
    }

    /**
     * Storage::delete()-compatible key for this slide file.
     */
    public function storageKey(): string
    {
        return str_contains($this->filename, '/')
            ? $this->filename
            : 'public/thumbnails/' . $this->filename;
    }
}
