<?php

namespace Bookingor\Backend\Controller\BusinessProfile;

class BusinessProfileControl
{
    /**
     * Prefix used for naming conventions in the booking system.
     *
     * @var string
     */
    public static $name_prefix = "bookingor";
    /**
     * The icon representing the business or organization.
     *
     * @var string
     */
    public $business_icon;
    /**
     * Adds options for business profile.
     *
     * This function adds options to the options table in the WordPress database.
     * The options are used to store the profile information of the business or
     * organization.
     *
     * @since 1.0.0
     * @access public
     *
     * @return void
     */
    public static function profile_adds_Options()
    {
        // business profile
        add_option(self::$name_prefix . "_business_name", "");
        add_option(self::$name_prefix . "_business_first_name", "");
        add_option(self::$name_prefix . "_business_last_name", "");
        add_option(self::$name_prefix . "_business_email", "");
        add_option(self::$name_prefix . "_business_phone", "");
        add_option(self::$name_prefix . "_business_city", "");
        add_option(self::$name_prefix . "_business_country", "");
        add_option(self::$name_prefix . "_business_website", "");
        add_option(self::$name_prefix . "_business_description", "");
        add_option(self::$name_prefix . "_business_logo", "");
        add_option(self::$name_prefix . "_business_latitude", "");
        add_option(self::$name_prefix . "_business_longitude", "");
        add_option(self::$name_prefix . "_business_hours", '[{"Monday":["09:00","17:00"],"breaks":[]},{"tuesday":["09:00","17:00"],"breaks":[]},{"wednesday":["09:00","17:00"],"breaks":[]},{"thursday":["09:00","17:00"],"breaks":[]},{"friday":["09:00","17:00"],"breaks":[]},{"saturday":["09:00","17:00"],"breaks":[]},{"sunday":1}]');
    }
    /**
     * Updates the business profile information in the database.
     *
     * This function updates the information associated with the business
     * profile in the WordPress database. The information is taken from the
     * $_POST array and is validated before being written to the
     * database.
     *
     * @since 1.0.0
     * @access public
     *
     * @return void
     */
    public static function profile_update_Options()
    {
        if (isset($_REQUEST["action"])) {
            if (isset($_POST['nonce'])) {
                if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'bookingor_ajax_nonce')) {
                    $response = [
                        'status' => htmlspecialchars('error'),
                        'message' => htmlspecialchars('Nonce verification failed.')
                    ];
                    echo wp_json_encode($response);
                    exit;
                }
            }
            if (!current_user_can('manage_options')) {
                $response = [
                    'status' => htmlspecialchars('error'),
                    'message' => htmlspecialchars('You do not have permission.')
                ];
                echo wp_json_encode($response);
                die();
            }

            if (isset($_POST['business_hours'])) {
                $business_hours = str_replace('\\', '', sanitize_text_field(wp_unslash($_POST['business_hours'])));
                update_option(self::$name_prefix . "_business_hours", $business_hours);
            }

            if (isset($_POST['business_name'])) {
                update_option(self::$name_prefix . "_business_name", sanitize_text_field(wp_unslash($_POST['business_name'])));
            }

            if (isset($_POST['business_personal_first_name'])) {
                update_option(self::$name_prefix . "_business_first_name", sanitize_text_field(wp_unslash($_POST['business_personal_first_name'])));
            }

            if (isset($_POST['business_personal_last_name'])) {
                update_option(self::$name_prefix . "_business_last_name", sanitize_text_field(wp_unslash($_POST['business_personal_last_name'])));
            }

            if (isset($_POST['business_description'])) {
                update_option(self::$name_prefix . "_business_description", sanitize_text_field(wp_unslash($_POST['business_description'])));
            }

            if (isset($_POST['business_email'])) {
                update_option(self::$name_prefix . "_business_email", sanitize_email(wp_unslash($_POST['business_email'])));
            }

            if (isset($_POST['business_phone'])) {
                update_option(self::$name_prefix . "_business_phone", sanitize_text_field(wp_unslash($_POST['business_phone'])));
            }

            if (isset($_POST['business_city'])) {
                update_option(self::$name_prefix . "_business_city", sanitize_text_field(wp_unslash($_POST['business_city'])));
            }

            if (isset($_POST['business_country'])) {
                update_option(self::$name_prefix . "_business_country", sanitize_text_field(wp_unslash($_POST['business_country'])));
            }

            if (isset($_POST['business_website'])) {
                update_option(self::$name_prefix . "_business_website", esc_url_raw(wp_unslash($_POST['business_website'])));
            }

            if (isset($_POST['business_icon_id'])) {
                update_option(self::$name_prefix . "_business_logo", sanitize_text_field(wp_unslash($_POST['business_icon_id'])));
            }

            $response = [
                'status' => htmlspecialchars('success'),
                'message' => htmlspecialchars('Profile updated successfully')
            ];
            echo wp_json_encode($response);
            exit;
        }
    }
}
