<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Department extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'slug',
        'description',
        'cover_image',
        'cover_image_banner',
        'is_active',
        'accepts_open_applications',
        'sort_order',
    ];

    protected $casts = [
        'is_active'                 => 'boolean',
        'accepts_open_applications' => 'boolean',
    ];

    public function jobPostings()
    {
        return $this->hasMany(JobPosting::class);
    }

    public function applications()
    {
        return $this->hasMany(JobApplication::class);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeAcceptsApplications($query)
    {
        return $query->where('is_active', true)->where('accepts_open_applications', true);
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (empty($model->slug)) {
                $model->slug = Str::slug($model->name);
            }
        });
    }
}