<?php

namespace App\Modules\HR\Recruitment\Models;

use App\Models\User;
use App\Modules\Admin\Tenancy\Traits\BelongsToTenant;
use App\Modules\HR\Contracts\Models\EmploymentContract;
use App\Modules\HR\Employees\Models\Employee;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class RecruitmentHiring extends Model
{
    use BelongsToTenant;
    use SoftDeletes;

    protected $table = 'recruitment_hirings';

    protected $fillable = [
        'tenant_id',
        'reference_code',
        'offer_letter_id',
        'application_id',
        'candidate_id',
        'vacancy_id',
        'employee_id',
        'employment_contract_id',
        'user_id',
        'hired_by',
        'hired_at',
        'notes',
    ];

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

    public function offerLetter(): BelongsTo
    {
        return $this->belongsTo(RecruitmentOfferLetter::class, 'offer_letter_id');
    }

    public function application(): BelongsTo
    {
        return $this->belongsTo(RecruitmentApplication::class, 'application_id');
    }

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

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

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

    public function employmentContract(): BelongsTo
    {
        return $this->belongsTo(EmploymentContract::class, 'employment_contract_id');
    }

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

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

    public function onboardingCase(): HasOne
    {
        return $this->hasOne(RecruitmentOnboardingCase::class, 'hiring_id');
    }
}
