<?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 BlockedDate extends Model
{
    use HasFactory;

    protected $fillable = [
        'property_id',
        'start_date',
        'end_date',
        'reason',
        'notes',
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
    ];

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

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

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

    /**
     * Scope a query to only include upcoming blocked dates
     */
    public function scopeUpcoming($query)
    {
        return $query->where('start_date', '>', now()->toDateString());
    }

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

    /**
     * Check if this blocked date overlaps with the given date range
     */
    public function overlaps(Carbon $start, Carbon $end): bool
    {
        return $this->start_date->lte($end) && $this->end_date->gte($start);
    }
}
