<?php
namespace App\Models;

use App\Enums\VacancyStatus;
use App\Enums\VacancyType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;

class Vacancy extends Model
{
    use HasFactory;
    protected $fillable = [
        'title', 'slug', 'area', 'location', 'type', 'description',
        'href', 'deadline', 'remote', 'status',
        'seo_title', 'seo_description',
    ];

    protected function casts(): array
    {
        return [
            'type' => VacancyType::class,
            'status' => VacancyStatus::class,
            'remote' => 'boolean',
            'deadline' => 'date',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (Vacancy $vacancy) {
            if (empty($vacancy->slug)) {
                $vacancy->slug = Str::slug($vacancy->title);
            }
        });
    }

    public function scopePublished($query)
    {
        return $query->where('status', VacancyStatus::Published);
    }

    public function scopeOpen($query)
    {
        return $query->published()
            ->where(function ($q) {
                $q->whereNull('deadline')->orWhere('deadline', '>=', now());
            });
    }
}
