<?php

declare(strict_types=1);

namespace OnoxWhmcs;

/**
 * Onoxsoft product config-option definitions and value resolution.
 *
 * These options appear on the WHMCS Admin → Products/Services → Module Settings tab.
 */
class ConfigOptions
{
    /**
     * Schema passed to onox_ConfigOptions().
     *
     * @return array<string, array<string, mixed>>
     */
    public static function schema(): array
    {
        return [
            'package_slug' => [
                'FriendlyName' => Helper::lang('cfg_package_slug_label'),
                'Type' => 'text',
                'Size' => '30',
                'Description' => Helper::lang('cfg_package_slug_desc'),
            ],
            'auto_create_dns' => [
                'FriendlyName' => Helper::lang('cfg_auto_dns_label'),
                'Type' => 'yesno',
                'Description' => Helper::lang('cfg_auto_dns_desc'),
            ],
            'auto_provision_ssl' => [
                'FriendlyName' => Helper::lang('cfg_auto_ssl_label'),
                'Type' => 'yesno',
                'Description' => Helper::lang('cfg_auto_ssl_desc'),
            ],
            'send_welcome_email' => [
                'FriendlyName' => Helper::lang('cfg_welcome_email_label'),
                'Type' => 'yesno',
                'Description' => Helper::lang('cfg_welcome_email_desc'),
            ],
        ];
    }

    /**
     * Extract and normalise config values from WHMCS $params.
     *
     * WHMCS passes config options under $params['configoption1'], ['configoption2'], …
     * in the order they were declared.
     *
     * @param  array<string,mixed>  $params  WHMCS module params
     * @return array{package_slug: string, auto_create_dns: bool, auto_provision_ssl: bool, send_welcome_email: bool}
     */
    public static function resolve(array $params): array
    {
        // WHMCS populates named keys when using array-key style config options (WHMCS 8+)
        $slug = trim(
            $params['configoptions']['package_slug']
            ?? $params['configoption1']
            ?? ''
        );

        $autoCreateDns = self::truthy(
            $params['configoptions']['auto_create_dns']
            ?? $params['configoption2']
            ?? 'on'
        );

        $autoSsl = self::truthy(
            $params['configoptions']['auto_provision_ssl']
            ?? $params['configoption3']
            ?? 'on'
        );

        $welcomeEmail = self::truthy(
            $params['configoptions']['send_welcome_email']
            ?? $params['configoption4']
            ?? 'off'
        );

        return [
            'package_slug' => $slug,
            'auto_create_dns' => $autoCreateDns,
            'auto_provision_ssl' => $autoSsl,
            'send_welcome_email' => $welcomeEmail,
        ];
    }

    /**
     * Convert WHMCS yesno field ('on'/'off', 1/0, 'yes'/'no') to bool.
     *
     * @param  mixed  $value
     */
    private static function truthy($value): bool
    {
        if (is_bool($value)) {
            return $value;
        }
        $str = strtolower(trim((string) $value));

        return in_array($str, ['on', 'yes', '1', 'true'], true);
    }
}
