<?php

namespace App\Modules\HR\Recruitment\Models;

use App\Models\User;
use App\Modules\Admin\Tenancy\Traits\BelongsToTenant;
use App\Modules\HR\Departments\Models\Department;
use App\Modules\HR\Positions\Models\Position;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class RecruitmentJobRequest extends Model
{
    use BelongsToTenant;
    use SoftDeletes;

    protected $table = 'recruitment_job_requests';

    protected $fillable = [
        'tenant_id',
        'reference_code',
        'department_id',
        'position_id',
        'title',
        'openings',
        'justification',
        'status',
        'requested_by',
        'reviewed_by',
        'reviewed_at',
        'review_notes',
    ];

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

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

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

    public function vacancies(): HasMany
    {
        return $this->hasMany(RecruitmentVacancy::class, 'job_request_id');
    }

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

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