<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Ad extends Model
{
    protected $fillable = ['name', 'code', 'placement', 'start_date', 'end_date', 'is_active'];

    protected $dates = ['start_date', 'end_date'];

    public function scopeActive($query)
    {
        return $query->where('is_active', 1)
            ->where(function($q) {
                $q->whereNull('start_date')
                   ->orWhere('start_date', '<=', now()->toDateString());
            })
            ->where(function($q) {
                $q->whereNull('end_date')
                   ->orWhere('end_date', '>=', now()->toDateString());
            });
    }

    public static function getByPlacement($placement)
    {
        return static::active()->where('placement', $placement)->get();
    }
}
