<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class JobApplication extends Entity
{
    protected $attributes = [
        'id'                    => null,
        'job_id'                => null,
        'candidate_profile_id'  => null,
        'resume_id'             => null,
        'cover_note'            => null,
        'status'                => 'applied',
        'assigned_recruiter_id' => null,
        'applied_at'            => null,
    ];

    protected $casts = [
        'id'                    => 'integer',
        'job_id'                => 'integer',
        'candidate_profile_id'  => 'integer',
        'resume_id'             => '?integer',
        'assigned_recruiter_id' => '?integer',
    ];

    protected $dates = ['applied_at', 'updated_at'];

    public const VALID_TRANSITIONS = [
        'applied'              => ['under_review', 'rejected'],
        'under_review'         => ['shortlisted', 'rejected'],
        'shortlisted'          => ['interview_scheduled', 'rejected'],
        'interview_scheduled'  => ['interview_completed', 'rejected'],
        'interview_completed'  => ['hired', 'rejected'],
        'rejected'             => [], // terminal
        'hired'                => [], // terminal
    ];

    public function canTransitionTo(string $newStatus): bool
    {
        return in_array($newStatus, self::VALID_TRANSITIONS[$this->status] ?? [], true);
    }
}
