<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Room extends Model
{
    protected $fillable = [
        'room_number',
        'category_id',
        'price_per_night',
        'max_guests',
        'status'
    ];
public function getPriceForDate($date = null)
{
    // If no date passed, use TODAY
    $date = $date ?? Carbon::today()->toDateString();

    // Get day name (monday, tuesday...)
    $day = strtolower(Carbon::parse($date)->format('l'));

    // Find matching price rule
    $rule = $this->priceRules()
        ->whereDate('start_date', '<=', $date)
        ->whereDate('end_date', '>=', $date)
        ->first();

    // If special price exists for that day, return it
    if ($rule && !empty($rule->{$day . '_price'})) {
        return $rule->{$day . '_price'};
    }

    // Otherwise return NORMAL room price
    return $this->price_per_night;
}
 public function priceRules()
{
    return $this->hasMany(RoomPriceRule::class);
}

    public function category()
    {
        return $this->belongsTo(RoomCategory::class, 'category_id');
    }

    // Room has many images
    public function images()
    {
        return $this->hasMany(RoomImage::class, 'room_id');
    }

    // ✅ ADD THIS (IMPORTANT)
    public function amenities()
    {
        return $this->belongsToMany(
            Amenity::class,
            'room_amenities',
            'room_id',
            'amenity_id'
        );
    }
}
