<?php

if ( !class_exists( 'MDWC_Rest_API_V1' ) ) {
    class MDWC_Rest_API_V1 {
        private static $_instance;

        private $namespace = 'mdwc/v1';

        /**
         * Single ton
         * 
         * @return MDWC_Rest_API_V1
         * 
         * @since 1.1.0
         */
        public static function get_instance() {
            if ( self::$_instance == null ) {
                self::$_instance = new self();
            }
            return self::$_instance;
        }

        /**
         * Constructor
         * 
         * @since 1.1.0
         */
        public function __construct() {
            add_action( 'rest_api_init', array($this, 'register_routes') );
        }

        /**
         * Register routes | Action Callback
         * 
         * @since 1.1.0
         */
        public function register_routes() {
            register_rest_route( $this->namespace, '/save-settings', array(
                'methods'             => 'POST',
                'callback'            => array($this, 'save_settings'),
                'permission_callback' => array($this, 'authenticate'),
            ) );
            register_rest_route( $this->namespace, '/get-settings', array(
                'methods'             => 'GET',
                'callback'            => array($this, 'get_settings'),
                'permission_callback' => array($this, 'authenticate'),
            ) );
            register_rest_route( $this->namespace, '/get-stats', array(
                'methods'             => 'GET',
                'callback'            => array($this, 'get_stats'),
                'permission_callback' => array($this, 'authenticate'),
            ) );
        }

        /**
         * Authenticate User | Permission Callback
         * 
         * @since 1.1.0
         */
        public function authenticate() {
            return current_user_can( 'manage_options' );
        }

        /**
         * Save Settings | Rest API Callback
         * 
         * @since 1.1.0
         */
        public function save_settings( WP_REST_Request $request ) {
            $is_sandbox = $request->get_param( 'is_sandbox' );
            $is_sandbox = ( $is_sandbox ? 'yes' : 'no' );
            $settings = array(
                'is_sandbox' => $is_sandbox,
            );
            $response = mdwc_save_settings( $settings );
            if ( $response ) {
                wp_send_json_success( array(
                    'message' => 'Settings saved successfully',
                ) );
            } else {
                wp_send_json_error( array(
                    'message' => 'Failed to save settings',
                ) );
            }
        }

        /**
         * Get Settings | Rest API Callback
         * 
         * @since 1.1.0
         */
        public function get_settings() {
            $settings = mdwc_get_settings();
            if ( $settings ) {
                wp_send_json_success( $settings );
            } else {
                wp_send_json_error( array(
                    'message' => 'Failed to save settings',
                ) );
            }
        }

        /**
         * Get Stats | Rest API Callback
         * 
         * @since 1.1.0
         */
        public function get_stats( WP_REST_Request $request ) {
            $midnight = $request->get_param( 'midnight' );
            $stats = mdwc_get_stats( $midnight );
            if ( $stats ) {
                wp_send_json_success( $stats );
            } else {
                wp_send_json_error( array(
                    'message' => 'Failed to get stats',
                ) );
            }
        }

    }

    MDWC_Rest_API_V1::get_instance();
}