<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Facades\Storage;

class Image extends Model
{
    protected $fillable = [
        'title',
        'alt_text',
        'path',
        'url',
        'mime_type',
        'file_size',
        'width',
        'height',
    ];

    public function rooms(): MorphToMany
    {
        return $this->morphedByMany(Room::class, 'imageable')
            ->withPivot('role', 'sort_order')
            ->withTimestamps();
    }

    public function mealPackages(): MorphToMany
    {
        return $this->morphedByMany(MealPackage::class, 'imageable')
            ->withPivot('role', 'sort_order')
            ->withTimestamps();
    }
    // You can add other relationships to different models too

    protected static function booted()
    {
        static::deleting(function ($image) {
            Storage::disk('public')->delete($image->path);
        });
    }

    public function imageables()
    {
        return $this->hasMany(Imageable::class);
    }
}
