<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class JobListing extends Model
{
    use HasFactory;

    // ── Fillable ───────────────────────────────────────────────
    protected $fillable = [
        'posted_by',
        'hub_id',
        'job_title',
        'company_name',
        'industry',
        'location_city',
        'job_type',
        'description',
        'required_skills',
        'min_experience_years',
        'salary_range',
        'status',
    ];

    // ── Casts ─────────────────────────────────────────────────
    protected $casts = [
        'required_skills'      => 'array',
        'min_experience_years' => 'integer',
    ];

    // ── Scopes ────────────────────────────────────────────────

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('status', 'active');
    }

    // ── Relationships ─────────────────────────────────────────

    public function employer(): BelongsTo
    {
        return $this->belongsTo(User::class, 'posted_by');
    }

    public function hub(): BelongsTo
    {
        return $this->belongsTo(Hub::class);
    }

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