<?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 RecruitmentApplication extends Model
{
    use BelongsToTenant;
    use SoftDeletes;

    protected $table = 'recruitment_applications';

    protected $fillable = [
        'tenant_id',
        'reference_code',
        'tracking_token',
        'vacancy_id',
        'candidate_id',
        'candidate_cv_id',
        'status',
        'source',
        'cover_letter',
        'applied_at',
        'created_by',
        'employee_id',
        'hired_at',
    ];

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

    public function vacancy(): BelongsTo
    {
        return $this->belongsTo(RecruitmentVacancy::class, 'vacancy_id');
    }

    public function candidate(): BelongsTo
    {
        return $this->belongsTo(RecruitmentCandidate::class, 'candidate_id');
    }

    public function candidateCv(): BelongsTo
    {
        return $this->belongsTo(RecruitmentCandidateCv::class, 'candidate_cv_id');
    }

    public function interviews(): HasMany
    {
        return $this->hasMany(RecruitmentInterview::class, 'application_id');
    }

    public function offerLetters(): HasMany
    {
        return $this->hasMany(RecruitmentOfferLetter::class, 'application_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);
    }
}
