<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\HotelScopeTrait;
use Illuminate\Support\Facades\Auth;

class RoomType extends Model
{
    use HasFactory, SoftDeletes, HotelScopeTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'hotel_id', // ADDED
        'name',
        'code',
        'description',
        'default_capacity',
        'max_capacity',
        'is_active',
        'has_breakfast',
        'amenities'
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'amenities' => 'array',
        'is_active' => 'boolean',
        'has_breakfast' => 'boolean',
        'default_capacity' => 'integer',
        'max_capacity' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime'
    ];

    /**
     * The default values for attributes.
     *
     * @var array
     */
    protected $attributes = [
        'is_active' => true,
        'has_breakfast' => false,
        'default_capacity' => 2
    ];

    /**
     * Boot the model.
     */
    protected static function boot()
    {
        parent::boot();
    }

    /**
     * Get the hotel that owns the room type.
     */
    public function hotel()
    {
        return $this->belongsTo(Hotel::class);
    }

    /**
     * Get the rooms for this room type.
     */
    public function rooms()
    {
        return $this->hasMany(Room::class, 'room_type_id');
    }

    /**
     * Get the active rooms count.
     */
    public function getActiveRoomsCountAttribute()
    {
        return $this->rooms()->where('status', 'available')->count();
    }

    /**
     * Get the total rooms count.
     */
    public function getTotalRoomsCountAttribute()
    {
        return $this->rooms()->count();
    }

    /**
     * Scope a query to only include active room types.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope a query to only include room types with breakfast.
     */
    public function scopeWithBreakfast($query)
    {
        return $query->where('has_breakfast', true);
    }
}