﻿<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class JobDescriptionValidationCheck extends Model
{
    use HasFactory;

    const UPDATED_AT = null;

    protected $fillable = [
        'job_description_id',
        'section_type',
        'check_key',
        'status',
        'score',
        'passed',
        'message',
    ];

    protected $casts = [
        'score'      => 'integer',
        'passed'     => 'boolean',
        'created_at' => 'datetime',
    ];

    // Status Constants

    const STATUS_PASS    = 'pass';
    const STATUS_FAIL    = 'fail';
    const STATUS_WARNING = 'warning';

    // All check keys: mirrors the checks_required array sent to the AI
    const CHECK_KEYS = [
        'role_level_salary_band',
        'department_classification',
        'competencies_framework',
        'role_summary',
        'key_responsibilities',
        'required_skills',
        'qualifications',
        'location_information',
        'equal_opportunity_language',
        'inclusive_language',
        'legal_compliance',
        'role_summary_length',
        'responsibilities_clarity',
        'skills_specificity',
    ];

    // Human-readable labels for each check key
    const CHECK_LABELS = [
        'role_level_salary_band'    => 'Role Level Matches Salary Band',
        'department_classification' => 'Department Classification',
        'competencies_framework'    => 'Competencies Framework',
        'role_summary'              => 'Role Summary',
        'key_responsibilities'      => 'Key Responsibilities',
        'required_skills'           => 'Required Skills',
        'qualifications'            => 'Qualifications',
        'location_information'      => 'Location Information',
        'equal_opportunity_language'=> 'Equal Opportunity Language',
        'inclusive_language'        => 'Inclusive Language Check',
        'legal_compliance'          => 'Legal Compliance',
        'role_summary_length'       => 'Role Summary Length',
        'responsibilities_clarity'  => 'Responsibilities Clarity',
        'skills_specificity'        => 'Skills Specificity',
    ];

    // Relationships

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

    // Scopes

    public function scopePassed($query)
    {
        return $query->where('passed', true);
    }

    public function scopeFailed($query)
    {
        return $query->where('passed', false);
    }

    public function scopeForSection($query, string $sectionType)
    {
        return $query->where('section_type', $sectionType);
    }

    // Helpers

    public function label(): string
    {
        return self::CHECK_LABELS[$this->check_key] ?? ucfirst(str_replace('_', ' ', $this->check_key));
    }

    public function needsReview(): bool
    {
        return !$this->passed;
    