<?php

declare(strict_types=1);

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class SeasonalPricing extends Model
{
    use HasFactory;

    protected $table = 'seasonal_pricing';

    protected $fillable = [
        'property_id',
        'start_date',
        'end_date',
        'price_per_night',
        'season_name',
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'price_per_night' => 'decimal:2',
    ];

    /**
     * Get the property that owns the seasonal pricing
     */
    public function property(): BelongsTo
    {
        return $this->belongsTo(Property::class);
    }

    /**
     * Scope a query to only include seasonal pricing for a specific property
     */
    public function scopeForProperty($query, int $propertyId)
    {
        return $query->where('property_id', $propertyId);
    }

    /**
     * Scope a query to only include active seasonal pricing
     */
    public function scopeActive($query)
    {
        return $query->where('end_date', '>=', now()->toDateString());
    }

    /**
     * Check if this seasonal pricing is currently active
     */
    public function isActive(): bool
    {
        return $this->end_date->isFuture() || $this->end_date->isToday();
    }

    /**
     * Check if this seasonal pricing applies to the given date
     */
    public function appliesTo(Carbon $date): bool
    {
        return $date->between($this->start_date, $this->end_date);
    }

    /**
     * Get the price for a specific date if this seasonal pricing applies
     */
    public function getPriceForDate(Carbon $date): ?float
    {
        return $this->appliesTo($date) ? (float) $this->price_per_night : null;
    }
}
