<?php

namespace App\Models;

use App\Models\User;
use App\Models\Skill;
use App\Models\JobSkill;
use App\Models\Applicant;
use App\Models\Parameter;
use App\Models\JobParameter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Job extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function skills(): BelongsToMany
    {
        return $this->belongsToMany(Skill::class, 'job_skills', 'job_id', 'skill_id');
    }

    /**
     * Get all of the comments for the Job
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function parameters(): HasMany
    {
        return $this->hasMany(JobParameter::class, 'job_id', 'id');
    }

    public function applicants(): HasMany
    {
        return $this->hasMany(Applicant::class, 'job_id', 'id');
    }

    public function getFormattedSkillsAttribute()
    {
        return $this->skills->map(function ($skill) {
            return [
                'skill_id' => $skill->id,
                'name' => $skill->name,
            ];
        });
    }

    public function getFormattedParametersAttribute()
    {
        return $this->parameters->map(function ($parameter) {
            return [
                'parameter_id' => $parameter->parameter_id,
                'weight' => $parameter->weight,
            ];
        });
    }

    public function getFormattedApplicantAttribute()
    {
        return $this->applicants->map(function ($applicant) {
            return [
                'user_id' => $applicant->user_id
            ];
        });
    }
}
