<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Announcement extends Model
{
    protected $fillable = [
        'title', 'content', 'priority', 'audience', 'department_id',
        'publish_date', 'expiry_date', 'is_published', 'created_by',
    ];

    protected $casts = [
        'publish_date' => 'date',
        'expiry_date'  => 'date',
        'is_published' => 'boolean',
    ];

    public function department() { return $this->belongsTo(Department::class); }
    public function creator()    { return $this->belongsTo(User::class, 'created_by'); }

    public function scopeActive($q)
    {
        return $q->where('is_published', true)
                 ->where('publish_date', '<=', now());
    }
}
