<?php

/**
 * Trait ASTGDRMSYS_Exam_Label_Helper
 *
 * Provides resilient helpers for resolving assessment display labels when
 * the global helper functions are unavailable.
 */

if (! defined('ABSPATH')) {
        exit;
}

trait ASTGDRMSYS_Exam_Label_Helper
{
        /**
         * Resolve the baseline assessment display names with fallbacks when helpers are unavailable.
         *
         * @return array<string, string> Baseline assessment label values.
         */
        private function get_display_name_baseline(): array
        {
                $defaults = array(
                        'class_display_name'          => 'Class',
                        'dept_display_name'           => 'Department',
                        'section_display_name'        => 'Section',
                        'exam_year_display_name'      => 'Exam Year',
                        'exam_name_display_name'      => 'Exam Name',
                        'roll_display_name'           => 'Roll No',
                        'regno_display_name'          => 'Registration Number',
                        'practical_mark_display_name' => 'Practical Mark',
                        'class_mark_display_name'     => 'Class Mark',
                        'final_mark_display_name'     => 'Final Mark',
                        'total_mark_display_name'     => 'Total Mark',
                        'single_mark_display_name'    => 'Exam Mark',
                );

                $resolved = null;

                if (function_exists('\\ASTGDRMSYS\\ResultManagement\\astgdrmsys_get_display_name_baseline')) {
                        $resolved = \ASTGDRMSYS\ResultManagement\astgdrmsys_get_display_name_baseline();
                } elseif (function_exists('astgdrmsys_get_display_name_baseline')) {
                        $resolved = astgdrmsys_get_display_name_baseline();
                }

                if (is_array($resolved)) {
                        return array_merge($defaults, $resolved);
                }

                return $defaults;
        }

        /**
         * Provide a local fallback for resolving assessment display names when helper functions are unavailable.
         *
         * @param array<string, mixed> $exam Exam data used to derive label defaults.
         * @return array<string, string> Assessment labels keyed by column name.
         */
        private function fallback_assessment_display_names(array $exam): array
        {
                $baseline  = $this->get_display_name_baseline();
                $exam_type = isset($exam['exam_type']) ? (string) $exam['exam_type'] : 'single';

                $first  = isset($exam['first_assessment_display_name']) ? trim((string) $exam['first_assessment_display_name']) : '';
                $second = isset($exam['second_assessment_display_name']) ? trim((string) $exam['second_assessment_display_name']) : '';
                $third  = isset($exam['third_assessment_display_name']) ? trim((string) $exam['third_assessment_display_name']) : '';
                $total  = isset($exam['total_display_name']) ? trim((string) $exam['total_display_name']) : '';

                $defaults_by_type = array(
                        'single'    => array(
                                'first'  => $baseline['single_mark_display_name'],
                                'second' => '',
                                'third'  => '',
                        ),
                        'composite' => array(
                                'first'  => $baseline['class_mark_display_name'],
                                'second' => $baseline['final_mark_display_name'],
                                'third'  => '',
                        ),
                        'triple'    => array(
                                'first'  => $baseline['practical_mark_display_name'],
                                'second' => $baseline['class_mark_display_name'],
                                'third'  => $baseline['final_mark_display_name'],
                        ),
                );

                $fallbacks = $defaults_by_type[$exam_type] ?? $defaults_by_type['single'];

                return array(
                        'first_assessment_display_name'  => $first !== '' ? $first : $fallbacks['first'],
                        'second_assessment_display_name' => $second !== '' ? $second : ($fallbacks['second'] ?? ''),
                        'third_assessment_display_name'  => $third !== '' ? $third : ($fallbacks['third'] ?? ''),
                        'total_display_name'             => $total !== '' ? $total : $baseline['total_mark_display_name'],
                );
        }

        /**
         * Resolve mark display labels for the current exam with graceful degradation.
         *
         * @param array<string, mixed> $exam_row Exam record containing assessment metadata.
         * @return array<string, string> Display labels keyed by usage (practical, class, final, etc.).
         */
        private function resolve_exam_mark_labels(array $exam_row): array
        {
                if (function_exists('\\ASTGDRMSYS\\ResultManagement\\astgdrmsys_resolve_exam_mark_labels')) {
                        return \ASTGDRMSYS\ResultManagement\astgdrmsys_resolve_exam_mark_labels($exam_row);
                }

                if (function_exists('astgdrmsys_resolve_exam_mark_labels')) {
                        return astgdrmsys_resolve_exam_mark_labels($exam_row);
                }

                $baseline  = $this->get_display_name_baseline();
                $resolved  = $this->fallback_assessment_display_names($exam_row);
                $exam_type = isset($exam_row['exam_type']) ? (string) $exam_row['exam_type'] : 'single';

                $labels = $baseline;
                $labels['single_mark_display_name'] = $resolved['first_assessment_display_name'];
                $labels['total_mark_display_name']  = $resolved['total_display_name'];

                if ('triple' === $exam_type) {
                        $labels['practical_mark_display_name'] = $resolved['first_assessment_display_name'];
                        $labels['class_mark_display_name']      = $resolved['second_assessment_display_name'] !== ''
                                ? $resolved['second_assessment_display_name']
                                : $baseline['class_mark_display_name'];
                        $labels['final_mark_display_name']      = $resolved['third_assessment_display_name'] !== ''
                                ? $resolved['third_assessment_display_name']
                                : $baseline['final_mark_display_name'];
                } elseif ('composite' === $exam_type) {
                        $labels['class_mark_display_name'] = $resolved['first_assessment_display_name'];
                        $labels['final_mark_display_name'] = $resolved['second_assessment_display_name'] !== ''
                                ? $resolved['second_assessment_display_name']
                                : $baseline['final_mark_display_name'];
                        $labels['practical_mark_display_name'] = $baseline['practical_mark_display_name'];
                } else {
                        $labels['practical_mark_display_name'] = $baseline['practical_mark_display_name'];
                        $labels['class_mark_display_name']      = $baseline['class_mark_display_name'];
                        $labels['final_mark_display_name']      = $baseline['final_mark_display_name'];
                }

                return array_merge($labels, $resolved);
        }
}
