<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Room extends Model
{
    use HasFactory;

    protected $primaryKey = 'room_id';

    protected $fillable = [
        'room_type_id',
        'bathroom_type',
        'room_no',
        'floor',
        'description',
        'capacity',
        'amenities',
        'price_per_night',
        'room_status',
        'status',
        'check_in_status',
        'check_out_status',
        'last_cleaned_at',
        'last_maintenance_at',
        'assigned_staff_id',
        'deleteStatus',
    ];

    protected $casts = [
        'status' => 'boolean',
        'check_in_status' => 'boolean',
        'check_out_status' => 'boolean',
        'deleteStatus' => 'boolean',
        'last_cleaned_at' => 'datetime',
        'last_maintenance_at' => 'datetime',
        'price_per_night' => 'decimal:2',
    ];

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

    /**
     * Get the bookings for this room.
     */
    public function bookings()
    {
        return $this->hasMany(Booking::class, 'room_id');
    }

    /**
     * Get the current booking for this room.
     */
    public function currentBooking()
    {
        return $this->bookings()->where('check_out', '>=', now()->format('Y-m-d'))->first();
    }

    /**
     * Get the primary customer from current booking.
     */
    public function primaryCustomer()
    {
        $currentBooking = $this->currentBooking();
        return $currentBooking ? $currentBooking->primaryCustomer : null;
    }

    /**
     * Check if room is available for booking
     */
    public function isAvailable()
    {
        return $this->room_status === 'Available' && !$this->status;
    }

    /**
     * Check if room is booked
     */
    public function isBooked()
    {
        return $this->room_status === 'Booked' || $this->status;
    }

    /**
     * Check if room is occupied (checked in)
     */
    public function isOccupied()
    {
        return $this->room_status === 'Occupied' || $this->check_in_status;
    }

    /**
     * Check if room is in maintenance
     */
    public function isInMaintenance()
    {
        return $this->room_status === 'Maintenance';
    }

    /**
     * Get status badge color
     */
    public function getStatusBadgeColor()
    {
        return match($this->room_status) {
            'Available' => 'bg-green-100 text-green-800',
            'Occupied' => 'bg-red-100 text-red-800',
            'Maintenance' => 'bg-yellow-100 text-yellow-800',
            'Booked' => 'bg-blue-100 text-blue-800',
            default => 'bg-gray-100 text-gray-800',
        };
    }

    /**
     * Get check-in status text
     */
    public function getCheckInStatusText()
    {
        if ($this->isOccupied()) {
            return 'Checked In';
        } elseif ($this->isBooked() && !$this->isOccupied()) {
            return 'Check In';
        }
        return '-';
    }

    /**
     * Get check-out status text
     */
    public function getCheckOutStatusText()
    {
        if ($this->isOccupied()) {
            return 'Check Out';
        }
        return '-';
    }
}
