<?php

namespace EasyRestaurantManager\Http\Controllers;

use EasyRestaurantManager\Resources\SettingResource;
use WP_REST_Request;

class SettingController
{
    public static function postSetting(WP_REST_Request $request)
    {
        $data = $request->get_param('settings');
       
        $sanitized_data = [];
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $sanitized_data[$key] = array_map('sanitize_text_field', $value);
            } else {
                $sanitized_data[$key] = sanitize_text_field($value);
            }
        }
        // $validatedRequest = new StoreColorRequest($request);
        // if ($validatedRequest->fails()) {
        //     return rest_ensure_response([
        //         'message' => 'Validation failed',
        //         'errors'  => $validatedRequest->errors()
        //     ], 400);
        // }
        update_option('easy_restaurant_manager_menus_template_settings', $sanitized_data);
        return rest_ensure_response([
            'message' => 'Settings updated successfully',
            'data' => $sanitized_data
        ]);
    }


    public static function getSetting()
    {
        $res = SettingResource::getTemplateSettings();
    
        if (!$res) {
            return rest_ensure_response([
                'message' => 'Failed to create setting data',
            ], 500);
        }
        return rest_ensure_response([
            'message' => 'Setting data created successfully',
            'setting_data' => $res
        ]);
    }

    public static function postConfirmationSettings(WP_REST_Request $request)
    {
        $data = $request->get_param('settings');
        $sanitized_data = [];
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $sanitized_data[$key] = array_map('sanitize_text_field', $value);
            } else {
                $sanitized_data[$key] = sanitize_text_field($value);
            }
        }
        update_option('easy_restaurant_manager_menus_confirmation_settings', $sanitized_data);
        return rest_ensure_response([
            'message' => 'Settings updated successfully',
            'data' => $sanitized_data
        ]);
    }

    public static function getConfirmationSettings()
    {
        $res = get_option( 'easy_restaurant_manager_menus_confirmation_settings', [
            'confirmation_email' => 'yes',
            'customer_notification_email' => 'yes',
            'customer_email' => 'customer@gmail.com',
            'customer_subject' => 'hello',
            'customer_description' => ' description',
            'admin_notification_email' => 'yes',
            'admin_email' => 'admin@gmail.com',
            'admin_subject' => 'hello',
            'admin_description' => ' description',
        ] );
    
        if (!$res) {
            return rest_ensure_response([
                'message' => 'Failed to create setting data',
            ], 500);
        }
        return rest_ensure_response([
            'message' => 'Setting data created successfully',
            'setting_data' => $res
        ]);
    }
}
