<?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 CvProfile extends Model
{
    use HasFactory;

    // ── Fillable ───────────────────────────────────────────────
    protected $fillable = [
        'user_id',
        'job_title',
        'current_industry',
        'years_experience',
        'experience_level',
        'skills',
        'hub_ids',
        'education_level',
        'education_field',
        'location_country',
        'location_city',
        'summary',
        'preferred_sectors',
        'job_type_preference',
        'open_to_relocation',
        'is_visible',
    ];

    // ── Casts ─────────────────────────────────────────────────
    protected $casts = [
        'skills'            => 'array',
        'hub_ids'           => 'array',
        'preferred_sectors' => 'array',
        'open_to_relocation'=> 'boolean',
        'is_visible'        => 'boolean',
        'years_experience'  => 'integer',
    ];

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

    public function isVisible(): bool
    {
        return $this->is_visible;
    }

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

    public function scopeVisible(Builder $query): Builder
    {
        return $query->where('is_visible', true);
    }

    public function scopeForHub(Builder $query, int $hubId): Builder
    {
        return $query->whereJsonContains('hub_ids', $hubId);
    }

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

    // Note: personal info (name, email, phone) is on the User model
    // and is NEVER exposed unless consent is given
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

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