<?php

/**
 * JCOGS Image Pro - CpLicenseStatusService
 * ========================================
 * Reads and normalises add-on license status for CP governance.
 *
 * @category   ExpressionEngine Add-on
 * @package    JCOGS Image Pro
 * @author     JCOGS Design <contact@jcogs.net>
 * @copyright  Copyright (c) 2021 - 2026 JCOGS Design
 * @license    https://jcogs.net/add-ons/license/jcogs_img_pro
 * @version    2.0.6
 * @link       https://jcogs.net/
 * @since      2.0.0
 */

namespace JCOGSDesign\JCOGSImagePro\Service\License;

class CpLicenseStatusService implements CpLicenseStatusServiceInterface
{
    /**
     * Optional local test override for CP smoke testing.
     *
     * Set in config as {addon_short_name}_license_test_override with one of:
     * valid, update_available, trial, expired, invalid, na
     */
    private function getTestOverrideStatus(string $addonShortName): ?string
    {
        if (!(defined('REQ') && REQ === 'CP')) {
            return null;
        }

        $override_key = strtolower(trim($addonShortName)) . '_license_test_override';

        $override = ee()->config->item($override_key);
        if ((!is_string($override) || $override === '')
            && isset(ee()->config->config)
            && is_array(ee()->config->config)
            && isset(ee()->config->config[$override_key])) {
            $override = ee()->config->config[$override_key];
        }

        if (!is_string($override) || $override === '') {
            $override = $GLOBALS['config'][$override_key] ?? null;
        }

        if (!is_string($override) || $override === '') {
            return null;
        }

        $override = strtolower(trim($override));
        if ($override === 'update available' || $override === 'update-available') {
            $override = 'update_available';
        }

        $allowed = ['valid', 'update_available', 'trial', 'expired', 'invalid', 'na'];

        if (!in_array($override, $allowed, true)) {
            return null;
        }

        return $override;
    }

    public function getRawStatus(string $addonShortName): string
    {
        $override = $this->getTestOverrideStatus($addonShortName);
        if ($override !== null) {
            return $override;
        }

        try {
            $addon = ee('Addon')->get($addonShortName);
            if (!$addon) {
                return 'na';
            }

            $status = (string) $addon->checkCachedLicenseResponse();
            return $status !== '' ? $status : 'na';
        } catch (\Throwable $e) {
            return 'na';
        }
    }

    public function getNormalisedStatus(string $addonShortName): string
    {
        $raw = $this->getRawStatus($addonShortName);

        if (in_array($raw, ['valid', 'update_available'], true)) {
            return 'ok';
        }

        if ($raw === 'trial') {
            return 'warn';
        }

        if (in_array($raw, ['expired', 'invalid'], true)) {
            return 'restricted';
        }

        return 'bypass';
    }
}
