<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DoctorMedia extends Model
{
    use SoftDeletes;

    protected $table = 'doctor_media';

    protected $fillable = [
        'doctor_id','type','title','file_path','thumbnail',
        'mime_type','file_size','duration_seconds','is_approved','sort_order',
    ];
    protected $casts = [
        'is_approved' => 'boolean',
        'file_size'   => 'integer',
    ];

    public function doctor() { return $this->belongsTo(Doctor::class); }

    public function scopeApproved($q)  { return $q->where('is_approved', true); }
    public function scopeImages($q)    { return $q->where('type', 'image'); }
    public function scopeVideos($q)    { return $q->where('type', 'video'); }

    public function getFileSizeFormattedAttribute(): string
    {
        $size = $this->file_size;
        if ($size >= 1073741824) return round($size / 1073741824, 2) . ' GB';
        if ($size >= 1048576)    return round($size / 1048576, 2)    . ' MB';
        if ($size >= 1024)       return round($size / 1024, 2)       . ' KB';
        return $size . ' B';
    }
}
