<?php

namespace App\Models;

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

class Booking extends Model
{
    use HasFactory;

    protected $primaryKey = 'booking_id';

    protected $fillable = [
        'room_id',
        'customer_id',
        'user_id',
        'cashier_id',
        'number_of_customers',
        'price_per_night',
        'number_of_nights',
        'booking_date',
        'check_in',
        'check_out',
        'total_price',
        'advance_payment',
        'due_amount',
        'remaining_price',
        'observations',
        'booking_status',
        'payment_status',
        'checked_in_at',
        'checked_out_at',
    ];

    protected $casts = [
        'booking_date' => 'datetime',
        'payment_status' => 'boolean',
        'price_per_night' => 'decimal:2',
        'advance_payment' => 'decimal:2',
        'due_amount' => 'decimal:2',
        'checked_in_at' => 'datetime',
        'checked_out_at' => 'datetime',
    ];



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

    /**
     * Get the booking customers for this booking.
     */
    public function bookingCustomers()
    {
        return $this->hasMany(BookingCustomer::class, 'booking_id');
    }

    /**
     * Get the primary customer (first customer).
     */
    public function primaryCustomer()
    {
        return $this->hasOne(BookingCustomer::class, 'booking_id')->oldest();
    }

    /**
     * Get the transactions for this booking.
     */
    public function transactions()
    {
        return $this->hasMany(Transaction::class, 'booking_id');
    }

    /**
     * Get the receipt for this booking.
     */
    public function receipt()
    {
        return $this->hasOne(Receipt::class, 'booking_id');
    }

    /**
     * Calculate total price based on nights and price per night.
     */
    public function calculateTotalPrice()
    {
        return $this->price_per_night * $this->number_of_nights;
    }

    /**
     * Calculate remaining amount.
     */
    public function calculateRemainingAmount()
    {
        return $this->total_price - $this->advance_payment;
    }



    /**
     * Check if booking is checked in.
     */
    public function isCheckedIn()
    {
        return $this->booking_status === 'Checked In';
    }

    /**
     * Check if booking is checked out.
     */
    public function isCheckedOut()
    {
        return $this->booking_status === 'Checked Out';
    }

    /**
     * Check if booking is cancelled.
     */
    public function isCancelled()
    {
        return $this->booking_status === 'Cancelled';
    }



    /**
     * Check if booking is rejected.
     */
    public function isRejected()
    {
        return $this->booking_status === 'Rejected';
    }

    /**
     * Get status badge color.
     */
    public function getStatusBadgeColor()
    {
        return match($this->booking_status) {
            'Checked In' => 'bg-green-100 text-green-800',
            'Checked Out' => 'bg-gray-100 text-gray-800',
            'Rejected' => 'bg-red-100 text-red-800',
            'Cancelled' => 'bg-red-100 text-red-800',
            default => 'bg-gray-100 text-gray-800',
        };
    }

    /**
     * Get the user who processed this booking.
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
