<?php

namespace App\Modules\HR\Recruitment\Models;

use App\Models\User;
use App\Modules\Admin\Tenancy\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class RecruitmentCandidate extends Model
{
    use BelongsToTenant;
    use SoftDeletes;

    protected $table = 'recruitment_candidates';

    protected $fillable = [
        'tenant_id',
        'reference_code',
        'first_name',
        'last_name',
        'email',
        'phone',
        'nationality',
        'source',
        'notes',
        'created_by',
        'updated_by',
        'employee_id',
        'hired_at',
    ];

    protected function casts(): array
    {
        return [
            'hired_at' => 'datetime',
        ];
    }

    public function getFullNameAttribute(): string
    {
        return trim($this->first_name.' '.$this->last_name);
    }

    public function cvs(): HasMany
    {
        return $this->hasMany(RecruitmentCandidateCv::class, 'candidate_id');
    }

    public function applications(): HasMany
    {
        return $this->hasMany(RecruitmentApplication::class, 'candidate_id');
    }

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

    public function employee(): BelongsTo
    {
        return $this->belongsTo(\App\Modules\HR\Employees\Models\Employee::class);
    }
}
