<?php

namespace App\Models\DayTour;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DayTourDate extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'day_tour_dates';

    protected $fillable = [
        'day_tour_id',
        'tour_date',
        'start_time',
        'end_time',
        'status',
        'is_active',
        'is_guaranteed',
        'max_participants',
        'available_seats',
        'booked_seats',
        'reserved_seats',
        'adult_price',
        'child_price',
        'infant_price',
        'notes',
        'guide_name',
        'guide_phone',
        'vehicle_id',
        'guaranteed_at',
        'cancelled_at',
        'cancelled_reason',
        'cancelled_by',
    ];

    protected function casts(): array
    {
        return [
            'tour_date' => 'date',
            'is_active' => 'boolean',
            'is_guaranteed' => 'boolean',
            'adult_price' => 'decimal:2',
            'child_price' => 'decimal:2',
            'infant_price' => 'decimal:2',
            'guaranteed_at' => 'datetime',
            'cancelled_at' => 'datetime',
        ];
    }

    // Relationships
    public function dayTour()
    {
        return $this->belongsTo(DayTour::class);
    }

    public function cancelledBy()
    {
        return $this->belongsTo(User::class, 'cancelled_by');
    }

    public function vehicle()
    {
        return $this->belongsTo(\App\Models\Definition\Definition::class, 'vehicle_id');
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeUpcoming($query)
    {
        return $query->where('tour_date', '>=', now()->toDateString());
    }

    public function scopeAvailable($query)
    {
        return $query->whereIn('status', ['available', 'guaranteed']);
    }

    public function scopeGuaranteed($query)
    {
        return $query->where('is_guaranteed', true);
    }

    // Helper Methods
    public function updateAvailableSeats()
    {
        $maxParticipants = $this->max_participants ?? $this->dayTour->max_participants;
        $this->available_seats = $maxParticipants - ($this->booked_seats + $this->reserved_seats);
        $this->save();
    }

    public function checkAndSetGuaranteed()
    {
        if ($this->is_guaranteed) return;

        $minBookings = $this->dayTour->min_bookings_for_guaranteed ?? 1;

        if ($this->booked_seats >= $minBookings && $this->dayTour->auto_confirm_guaranteed) {
            $this->is_guaranteed = true;
            $this->guaranteed_at = now();
            $this->status = 'guaranteed';
            $this->save();
        }
    }

    public function getAdultPriceAttribute($value)
    {
        return $value ?? $this->dayTour->adult_price;
    }

    public function getChildPriceAttribute($value)
    {
        return $value ?? $this->dayTour->child_price;
    }

    public function getInfantPriceAttribute($value)
    {
        return $value ?? $this->dayTour->infant_price;
    }

    public function isFull()
    {
        return $this->available_seats <= 0;
    }

    public function isAvailable()
    {
        return $this->is_active &&
               in_array($this->status, ['available', 'guaranteed']) &&
               !$this->isFull();
    }
}
