<?php

namespace App\Services\Grading\Display;

use App\Models\SemesterScore;
use App\Models\SemesterScoreSnapshot;

class GradeCalculationDisplayResolver
{
    public function resolve(SemesterScore $score): array
    {
        $mode = (string) ($score->calculation_mode ?: 'legacy');
        $version = (string) ($score->calculation_policy_version ?: 'legacy_v1');

        if ($mode === 'strong' && $score->snapshot_id) {
            $snapshot = SemesterScoreSnapshot::query()->find($score->snapshot_id);
            if ($snapshot) {
                return [
                    'mode' => 'strong',
                    'policy_version' => $version,
                    'label' => 'Strong Calculation',
                    'score' => $score,
                    'snapshot' => $snapshot,
                    'inputs' => $snapshot->input_json,
                    'calculation' => $snapshot->calculation_json,
                ];
            }
        }

        return [
            'mode' => 'legacy',
            'policy_version' => $version ?: 'legacy_v1',
            'label' => 'Legacy Calculation',
            'score' => $score,
            'snapshot' => null,
            'legacy_note' => 'This score is displayed from stored legacy semester_scores values. Legacy averages ignored blank scores, PTAP used legacy dynamic weighting, and attendance includes one-absence grace.',
        ];
    }
}
