<?php

namespace App\Models;

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

class ChatOffer extends Model
{
    use HasFactory;

    protected $table = 'offers';

    protected $guarded = ['id'];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'priority' => 'integer',
        'version' => 'integer',
        'is_active' => 'boolean',
    ];

    public function scopeActiveWindow($query)
    {
        $today = Carbon::today()->toDateString();

        return $query->where('is_active', true)
            ->where(function ($builder) use ($today) {
                $builder->whereNull('start_date')->orWhereDate('start_date', '<=', $today);
            })
            ->where(function ($builder) use ($today) {
                $builder->whereNull('end_date')->orWhereDate('end_date', '>=', $today);
            });
    }
}
