﻿<?php

namespace App\Models;

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

class JobDescriptionSection extends Model
{
    use HasFactory;
 
    protected $fillable = [
        'job_description_id',
        'section_type',   // null for custom sections
        'title',          // user-defined for custom sections
        'content',
        'source',
        'version',
        'is_core',
        'sort_order',
    ];
 
    protected $casts = [
        'version'    => 'integer',
        'is_core'    => 'boolean',
        'sort_order' => 'integer',
    ];
 
    // Core Section Type Constants
 
    const TYPE_ROLE_SUMMARY         = 'role_summary';
    const TYPE_KEY_RESPONSIBILITIES = 'key_responsibilities';
    const TYPE_REQUIRED_SKILLS      = 'required_skills';
    const TYPE_QUALIFICATIONS       = 'qualifications';
    const TYPE_CORE_COMPETENCIES    = 'core_competencies';
 
    // Canonical order used for display, validation routing and export
    const SECTION_TYPES = [
        self::TYPE_ROLE_SUMMARY,
        self::TYPE_KEY_RESPONSIBILITIES,
        self::TYPE_REQUIRED_SKILLS,
        self::TYPE_QUALIFICATIONS,
        self::TYPE_CORE_COMPETENCIES,
    ];
 
    // Human-readable labels for core sections
    const SECTION_LABELS = [
        self::TYPE_ROLE_SUMMARY         => 'Role Summary',
        self::TYPE_KEY_RESPONSIBILITIES => 'Key Responsibilities',
        self::TYPE_REQUIRED_SKILLS      => 'Required Skills',
        self::TYPE_QUALIFICATIONS       => 'Qualifications',
        self::TYPE_CORE_COMPETENCIES    => 'Core Competencies',
    ];
 
    // Which validation checks belong to each core section
    // Used to route the user back to the right section from the validation screen
    const SECTION_CHECKS = [
        self::TYPE_ROLE_SUMMARY => [
            'role_summary',
            'role_summary_length',
            'role_level_salary_band',
        ],
        self::TYPE_KEY_RESPONSIBILITIES => [
            'key_responsibilities',
            'responsibilities_clarity',
        ],
        self::TYPE_REQUIRED_SKILLS => [
            'required_skills',
            'skills_specificity',
        ],
        self::TYPE_QUALIFICATIONS => [
            'qualifications',
            'legal_compliance',
            'equal_opportunity_language',
        ],
        self::TYPE_CORE_COMPETENCIES => [
            'competencies_framework',
            'department_classification',
            'inclusive_language',
            'location_information',
        ],
    ];
 
    // Source Constants
 
    const SOURCE_AI   = 'ai';
    const SOURCE_USER = 'user';
 
    // Relationships
 
    public function jobDescription(): BelongsTo
    {
        return $this->belongsTo(JobDescription::class);
    }
 
    // Helpers
 
    public function isCore(): bool
    {
        return (bool) $this->is_core;
    }
 
    public function isCustom(): bool
    {
        return !$this->is_core;
    }
 
    /**
     * Display label custom sections use the user-defined title.
     */
    public function label(): string
    {
        if ($this->isCustom()) {
            return $this->title ?? 'Custom Section';
        }
 
        return self::SECTION_LABELS[$this->section_type] ?? ucfirst($this->section_type);
    }
 
    /**
     * Resolve which section_type a given check_key belongs to.
     * Returns null if the check is not mapped to any section.
     */
    public static function sectionTypeForCheck(string $checkKey): ?string
    {
        foreach (self::SECTION_CHECKS as $sectionType => $checks) {
            if (in_array($checkKey, $checks, true)) {
                return $sectionType;
            }
        }
 
        return null;
    