<?php

if (! defined('ABSPATH')) {
    exit;
} require_once dirname(__DIR__) . '/cpt/class-puffergo-base-cpt.php';
class PufferGo_Module_Settings
{
    public const OPTION_KEY = 'puffergo_enabled_modules';

    public const MODULES = ['product' => 'puffergo_product', 'solution' => 'puffergo_solution', 'case-study' => 'puffergo_case', 'testimonial' => 'puffergo_testimonial', ];

    public const PAUSED_MODULES = ['testimonial'];

    public const DEFAULT_ENABLED = PufferGo_Base_CPT::DEFAULT_MODULES;

    public static function init(): void
    {
        add_action('rest_api_init', [__CLASS__, 'register_rest_routes']);
    }

    public static function register_rest_routes(): void
    {
        register_rest_route('puffergo/v1', '/modules/status', ['methods' => WP_REST_Server::READABLE, 'callback' => [__CLASS__, 'rest_get_status'], 'permission_callback' => [__CLASS__, 'permission_edit_posts'], ]);
        register_rest_route('puffergo/v1', '/modules/(?P<key>[a-z-]+)/toggle', ['methods' => WP_REST_Server::CREATABLE, 'callback' => [__CLASS__, 'rest_toggle_module'], 'permission_callback' => [__CLASS__, 'permission_manage_options'], 'args' => ['key' => ['required' => true, 'validate_callback' => [__CLASS__, 'validate_module_key'], ], ], ]);
    }

    public static function rest_get_status(): WP_REST_Response
    {
        $enabled = self::get_enabled_modules();
        $result = [];

        foreach (self::MODULES as $key => $cpt_slug) {
            if (in_array($key, self::PAUSED_MODULES, true)) {
                continue;
            } $count_obj = wp_count_posts($cpt_slug);
            $result[ $key ] = ['enabled' => in_array($key, $enabled, true), 'count' => isset($count_obj->publish) ? (int) $count_obj->publish : 0, 'draftCount' => isset($count_obj->draft) ? (int) $count_obj->draft : 0, 'archiveUrl' => get_post_type_archive_link($cpt_slug) ?: '', ];
        }

return new WP_REST_Response($result, 200);
    }

    public static function rest_toggle_module(WP_REST_Request $request)
    {
        $key = $request->get_param('key');

        if (in_array($key, self::PAUSED_MODULES, true)) {
            return new WP_REST_Response(array('code' => 'module_paused', 'message' => __('This module is temporarily unavailable.', 'puffergo'), ), 403);
        } $body = $request->get_json_params();
        $enable = isset($body['enabled']) ? (bool) $body['enabled'] : false;
        $current = self::get_enabled_modules();

        if ($enable) {
            if (! in_array($key, $current, true)) {
                $current[] = $key;
            }
        } else {
            $current = array_values(array_filter($current, function ($k) use ($key) { return $k !== $key; }));
        } update_option(self::OPTION_KEY, $current);
        update_option('puffergo_rewrite_hash', '');

        return new WP_REST_Response(['enabled' => $enable], 200);
    }

    public static function get_enabled_modules(): array
    {
        return (array) get_option(self::OPTION_KEY, self::DEFAULT_ENABLED);
    }

    public static function validate_module_key($value): bool
    {
        return array_key_exists($value, self::MODULES);
    }

    public static function permission_edit_posts(): bool
    {
        return current_user_can('edit_posts');
    }

    public static function permission_manage_options(): bool
    {
        return current_user_can('manage_options');
    }
}
