<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class JobListing extends Model
{
    protected $fillable = [
        'title',
        'slug',
        'department',
        'location',
        'employment_type',
        'salary_text',
        'description',
        'closing_date',
        'status',
        'is_published',
    ];

    protected function casts(): array
    {
        return [
            'closing_date' => 'date',
            'is_published' => 'boolean',
        ];
    }

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

    public function scopeOpenPublic($query)
    {
        return $query->where('is_published', true)
            ->where('status', 'open')
            ->where(function ($q) {
                $q->whereNull('closing_date')
                    ->orWhere('closing_date', '>=', now()->toDateString());
            });
    }

    public function getRouteKeyName(): string
    {
        return 'slug';
    }
}
