<?php

namespace App\Services\Grading\Policy;

use App\Models\Configuration;

class GradingPolicyResolver
{
    public const LEGACY_MODE = 'legacy';
    public const STRONG_MODE = 'strong';
    public const LEGACY_VERSION = 'legacy_v1';
    public const STRONG_VERSION = 'strong_v1';

    public function calculationMode(?int $classSectionId = null, ?string $semester = null, ?string $schoolYear = null): string
    {
        $enabled = strtolower((string) (Configuration::getConfig('strong_grading_enabled') ?? ''));
        if (!in_array($enabled, ['1', 'true', 'yes', 'on'], true)) {
            return self::LEGACY_MODE;
        }

        $allowedSections = trim((string) (Configuration::getConfig('strong_grading_class_sections') ?? ''));
        if ($allowedSections === '' || $allowedSections === '*') {
            return self::STRONG_MODE;
        }

        $ids = array_filter(array_map('trim', explode(',', $allowedSections)), fn ($v) => $v !== '');
        return $classSectionId !== null && in_array((string) $classSectionId, $ids, true)
            ? self::STRONG_MODE
            : self::LEGACY_MODE;
    }

    public function policyVersion(string $mode): string
    {
        return $mode === self::STRONG_MODE ? self::STRONG_VERSION : self::LEGACY_VERSION;
    }
}
