<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class TopbarNotification extends Model
{
    protected $fillable = [
        'content',
        'button_label',
        'button_url',
        'start_date',
        'end_date',
        'is_active',
        'order',
    ];

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

    /**
     * Scope: active notifications (is_active=true AND not expired).
     */
    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true)
                     ->where(function (Builder $q) {
                         $q->whereNull('end_date')
                           ->orWhere('end_date', '>=', now()->toDateString());
                     });
    }

    /**
     * Scope: ordered by order field ascending.
     */
    public function scopeOrdered(Builder $query): Builder
    {
        return $query->orderBy('order', 'asc');
    }
}
