<?php

namespace WPSN\Modules\HeadCleanup;

defined('ABSPATH') || exit;

final class HeadCleanupProfile
{
    public const PRESET_CUSTOM = 'custom';
    public const PRESET_CONSERVATIVE = 'conservative';
    public const PRESET_STRICT = 'strict';

    /**
     * @return array<string, mixed>
     */
    public static function defaultConfig(): array
    {
        return [
            'enabled' => false,
            'preset' => self::PRESET_CUSTOM,
            'remove_emoji' => false,
            'remove_wlwmanifest' => false,
            'remove_rsd' => false,
            'remove_generator' => false,
            'remove_shortlink' => false,
            'remove_rest_links' => false,
            'disable_oembed_discovery' => false,
            'remove_feed_links' => false,
            'remove_pingback_link' => false,
            'disable_feed_endpoints_global' => false,
            'disable_feed_endpoints_search' => false,
            'disable_feed_endpoints_comments' => false,
            'remove_pingback_header' => false,
            'remove_powered_by_header' => false,
        ];
    }

    /**
     * @param array<string, mixed> $input
     * @return array<string, mixed>
     */
    public static function sanitize(array $input): array
    {
        $out = self::defaultConfig();

        $out['enabled'] = !empty($input['enabled']);

        $preset = isset($input['preset']) ? sanitize_key((string) $input['preset']) : self::PRESET_CUSTOM;
        if (!in_array($preset, [self::PRESET_CUSTOM, self::PRESET_CONSERVATIVE, self::PRESET_STRICT], true)) {
            $preset = self::PRESET_CUSTOM;
        }
        $out['preset'] = $preset;

        foreach (array_keys(self::labels()) as $key) {
            $out[$key] = !empty($input[$key]);
        }

        return self::applyPreset($out);
    }

    /**
     * @param array<string, mixed> $config
     * @return array<string, mixed>
     */
    public static function applyPreset(array $config): array
    {
        $out = array_replace(self::defaultConfig(), $config);
        $preset = isset($out['preset']) ? (string) $out['preset'] : self::PRESET_CUSTOM;

        if ($preset === self::PRESET_CONSERVATIVE) {
            $out['remove_emoji'] = true;
            $out['remove_wlwmanifest'] = true;
            $out['remove_rsd'] = true;
            $out['remove_generator'] = true;
            $out['remove_shortlink'] = true;
            $out['remove_rest_links'] = true;
            $out['disable_oembed_discovery'] = false;
            $out['remove_feed_links'] = false;
            $out['remove_pingback_link'] = true;
            $out['disable_feed_endpoints_global'] = false;
            $out['disable_feed_endpoints_search'] = false;
            $out['disable_feed_endpoints_comments'] = false;
            $out['remove_pingback_header'] = true;
            $out['remove_powered_by_header'] = true;
        } elseif ($preset === self::PRESET_STRICT) {
            $out['remove_emoji'] = true;
            $out['remove_wlwmanifest'] = true;
            $out['remove_rsd'] = true;
            $out['remove_generator'] = true;
            $out['remove_shortlink'] = true;
            $out['remove_rest_links'] = true;
            $out['disable_oembed_discovery'] = true;
            $out['remove_feed_links'] = true;
            $out['remove_pingback_link'] = true;
            $out['disable_feed_endpoints_global'] = true;
            $out['disable_feed_endpoints_search'] = true;
            $out['disable_feed_endpoints_comments'] = true;
            $out['remove_pingback_header'] = true;
            $out['remove_powered_by_header'] = true;
        }

        return $out;
    }

    /**
     * @return array<string, string>
     */
    public static function presetChoices(): array
    {
        return [
            self::PRESET_CUSTOM => __('Custom', 'wp-seo-next'),
            self::PRESET_CONSERVATIVE => __('Conservative', 'wp-seo-next'),
            self::PRESET_STRICT => __('Strict', 'wp-seo-next'),
        ];
    }

    /**
     * @return array<string, string>
     */
    public static function labels(): array
    {
        return [
            'remove_emoji' => __('Remove emoji detection script and styles', 'wp-seo-next'),
            'remove_wlwmanifest' => __('Remove Windows Live Writer manifest link', 'wp-seo-next'),
            'remove_rsd' => __('Remove RSD/EditURI discovery link', 'wp-seo-next'),
            'remove_generator' => __('Remove WordPress generator meta tag', 'wp-seo-next'),
            'remove_shortlink' => __('Remove shortlink tag and header', 'wp-seo-next'),
            'remove_rest_links' => __('Remove REST API discovery links and headers', 'wp-seo-next'),
            'disable_oembed_discovery' => __('Disable oEmbed discovery links and host JS', 'wp-seo-next'),
            'remove_feed_links' => __('Remove feed links from head', 'wp-seo-next'),
            'remove_pingback_link' => __('Remove pingback link tag from front-end HTML (best-effort)', 'wp-seo-next'),
            'disable_feed_endpoints_global' => __('Disable feed endpoints globally (404)', 'wp-seo-next'),
            'disable_feed_endpoints_search' => __('Disable search feed endpoints (404)', 'wp-seo-next'),
            'disable_feed_endpoints_comments' => __('Disable comments feed endpoints (404)', 'wp-seo-next'),
            'remove_pingback_header' => __('Remove X-Pingback header', 'wp-seo-next'),
            'remove_powered_by_header' => __('Remove X-Powered-By header (best-effort)', 'wp-seo-next'),
        ];
    }

    /**
     * @param array<string, mixed> $config
     * @return string[]
     */
    public static function activeItemLabels(array $config): array
    {
        $config = self::applyPreset($config);
        $out = [];

        foreach (self::labels() as $key => $label) {
            if (!empty($config[$key])) {
                $out[] = $label;
            }
        }

        return $out;
    }
}
