<?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;

class JobNotification extends Model
{
    use HasFactory;

    // ── Fillable ───────────────────────────────────────────────
    protected $fillable = [
        'job_listing_id',
        'cv_profile_id',
        'professional_user_id',
        'employer_user_id',
        'message',
        'status',
        'professional_consented',
        'employer_consented',
        'connected_at',
    ];

    // ── Casts ─────────────────────────────────────────────────
    protected $casts = [
        'professional_consented' => 'boolean',
        'employer_consented'     => 'boolean',
        'connected_at'           => 'datetime',
    ];

    // ── Helpers ───────────────────────────────────────────────

    public function isConnected(): bool
    {
        return $this->status === 'connected';
    }

    public function bothConsented(): bool
    {
        return $this->professional_consented && $this->employer_consented;
    }

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

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

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

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

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

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

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

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