<?php
/**
 * 🎛️ DASHBOARD SETTINGS CONTROLLER - V2.2 PURE
 *
 * Manages dashboard configuration settings:
 * - Demo mode toggle
 * - Future dashboard preferences
 *
 * @package FleekDash\Controllers
 * @since 2.2.0
 */

namespace FleekDash\Controllers;

use WP_REST_Request;
use WP_REST_Response;
use FleekDash\Services\ResponseService;

/**
 * Class DashboardSettingsController
 *
 * Pure V2.2 Controller - No legacy dependencies
 *
 * @package FleekDash\Controllers
 */
class DashboardSettingsController
{
    /**
     * 🎯 Get demo mode setting
     *
     * @param WP_REST_Request $request
     * @return WP_REST_Response
     */
    public function get_demo_mode_setting(WP_REST_Request $request)
    {
        try {
            // Get demo mode setting (default: false for real data)
            $is_demo_mode = \get_option('fleekdash_demo_mode', false);

            return ResponseService::success('Demo mode setting retrieved', [
                'isDemoMode' => (bool) $is_demo_mode,
            ]);

        } catch (\Exception $e) {
            return ResponseService::error(
                'Failed to retrieve demo mode setting',
                ['error' => $e->getMessage()],
                500
            );
        }
    }

    /**
     * 🔧 Update demo mode setting
     *
     * @param WP_REST_Request $request
     * @return WP_REST_Response
     */
    public function update_demo_mode_setting(WP_REST_Request $request)
    {
        try {
            $body = $request->get_json_params();

            if (!isset($body['isDemoMode'])) {
                return ResponseService::error(
                    'isDemoMode parameter is required',
                    null,
                    400
                );
            }

            $is_demo_mode = (bool) $body['isDemoMode'];

            // Save to WordPress options
            $result = \update_option('fleekdash_demo_mode', $is_demo_mode);

            if ($result === false) {
                // Option already has this value or save failed
                $current_value = \get_option('fleekdash_demo_mode', false);

                if ($current_value === $is_demo_mode) {
                    return ResponseService::success('Demo mode setting unchanged (already set)', [
                        'isDemoMode' => $is_demo_mode,
                    ]);
                }

                return ResponseService::error(
                    'Failed to save demo mode setting',
                    ['current_value' => $current_value],
                    500
                );
            }

            return ResponseService::success('Demo mode setting saved successfully', [
                'isDemoMode' => $is_demo_mode,
            ]);

        } catch (\Exception $e) {
            return ResponseService::error(
                'Failed to update demo mode setting',
                ['error' => $e->getMessage()],
                500
            );
        }
    }
}
