<?php
declare(strict_types=1);

namespace Help4BuilderSuite\Support;

if (! defined('ABSPATH')) {
    exit;
}

final class FeatureFlags
{
    public const OPTION_KEY = 'h4bs_feature_flags';

    /**
     * @return array<string, bool>
     */
    public function defaults(): array
    {
        return [
            'template_library' => true,
            'slider'           => true,
            'seo'              => true,
            'multisite'        => true,
            'commerce'         => true,
            'forms'            => true,
            'email_marketing'  => true,
            'redirection'      => true,
            'release_bridge'   => false,
            'search'           => true,
            'fields'           => true,
            'industry'         => true,
            'lms'              => true,
            'mega_menu'        => true,
            'calendar'         => true,
            'staff_directory'  => true,
            'builder'          => true,
            'addon_pack'       => true,
            'licensing'        => true,
            'dependency'       => false,
            'security'         => true,
            'performance'      => true,
            'image_optimization' => true,
            'insights'         => true,
            'cutover'          => true,
            'feedback'         => true,
            'product_insights' => true,
            'demos'            => false,
        ];
    }

    /**
     * @return array<string, bool>
     */
    public function all(): array
    {
        $saved = get_option(self::OPTION_KEY, []);
        if (! is_array($saved)) {
            $saved = [];
        }

        $flags = array_map('rest_sanitize_boolean', array_merge($this->defaults(), $saved));
        $flags['licensing'] = true;
        return $flags;
    }

    public function isEnabled(string $key): bool
    {
        $flags = $this->all();
        return ! empty($flags[$key]);
    }
}
