<?php

namespace Bookingor\Frontend\Controller\Staff;

use stdClass;

class StaffControlFront
{
    private static $bookingorDb;
    public static $f_staff;
    public static $f_staff_assign;
    public static $f_select_daily_time;
    public static $staff_order_booked;
    public static $customer_order_booked;
    public static $front_staff_profile;
    public static $staff_view;
    public static $staff_data;
    public static $staff_data_sp_day;
    public static $staff_data_leave;
    public static $staff_assign;
    public static $staff_add_location;
    public static $service_view;
    public static $staff_location;
    public static $get_staff_email;
    public static $dp_prefix = 'bookingor_';

    public static function bp_front_staff_data_by_id_shortcode()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $staff_get_id = isset($_REQUEST['shortcode_staff_id']) ? intval($_REQUEST['shortcode_staff_id']) : 0;


        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);
            }
        }
        return self::$f_staff = self::$bookingorDb->get_results($wpdb->prepare(
            "SELECT 
				DISTINCT s.staff_id, 
				s.staff_first_name, 
				s.staff_last_name, 
				s.staff_icon, 
				s.staff_email,
				s.staff_tag_line,
				s.staff_about, 
				s.staff_phone,
				s.staff_sheet_weekly, 
			FROM 
			  {$wpdb->prefix}bookingor_staff_add
			WHERE 
			  status = 1 
			  AND staff_id = %d",
            $staff_get_id
        ));
    }

    /**
     * Retrieves staff view data including staff ID, first name, last name, icon, email, phone number, weekly schedule, assigned services, and locations.
     * Staff members are filtered by active status.
     *
     * @return array An array of staff view data.
     */
    public static function bp_front_staff_view($staff_get_id = null)
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $staff = $wpdb->prefix . self::$dp_prefix . 'staff_add';
        $staff_assign_service = $wpdb->prefix . self::$dp_prefix . 'staff_assign_service';
        $staff_assign_location = $wpdb->prefix . self::$dp_prefix . 'location_assign_staff';

        // $get_staff_id = isset($_REQUEST['shortcode_staff_id']) ? intval($_REQUEST['shortcode_staff_id']) : 0;
        $staff_id = $staff_get_id;
        // exit;
        if (!empty($staff_id)) {
            // print_r($staff_id);
            return self::$f_staff = self::$bookingorDb->get_results($wpdb->prepare(
                'SELECT 
			DISTINCT s.staff_id, 
			GROUP_CONCAT(DISTINCT ss.service_get_id) AS service_get_id, 
			s.staff_first_name, 
			s.staff_last_name, 
			s.staff_icon, 
			s.staff_email,
			s.staff_tag_line,
			s.staff_about, 
			s.staff_phone,
			s.staff_location,
			s.staff_sheet_weekly, 
			GROUP_CONCAT(DISTINCT sl.location_get_id) AS location_get_id 
		FROM 
			%1$s s 
		LEFT JOIN 
			%2$s ss ON s.staff_id = ss.staff_get_id 
		LEFT JOIN 
			%3$s sl ON s.staff_id = sl.staff_get_id 
		WHERE 
			s.status = %4$s AND 
            s.staff_id = %5$s',
                $staff,
                $staff_assign_service,
                $staff_assign_location,
                1,
                $staff_id
            ));
        } else {

            return self::$f_staff = self::$bookingorDb->get_results($wpdb->prepare(
                'SELECT 
				DISTINCT s.staff_id, 
				GROUP_CONCAT(DISTINCT ss.service_get_id) AS service_get_id, 
				s.staff_first_name, 
				s.staff_last_name, 
				s.staff_icon, 
				s.staff_email,
				s.staff_tag_line,
				s.staff_about, 
				s.staff_phone,
				s.staff_location,
				s.staff_sheet_weekly, 
				GROUP_CONCAT(DISTINCT sl.location_get_id) AS location_get_id 
			FROM 
				%1$s s 
			LEFT JOIN 
				%2$s ss ON s.staff_id = ss.staff_get_id 
			LEFT JOIN 
				%3$s sl ON s.staff_id = sl.staff_get_id 
			WHERE 
				s.status = %4$s 
			GROUP BY 
				s.staff_id',
                $staff,
                $staff_assign_service,
                $staff_assign_location,
                1
            ));
        }
    }

    /**
     * Retrieves staff assignment data including staff ID, first name, last name, icon, email, phone number, weekly schedule, and assigned services.
     * Staff members are filtered by active status and the provided service ID.
     *
     * @return array An array of staff assignment data.
     */
    public static function bp_front_staff_assigns()
    {
        global $wpdb;
        $staff_assign = $wpdb->prefix . self::$dp_prefix . 'staff_assign_service';
        $staff = $wpdb->prefix . self::$dp_prefix . 'staff_add';
        $service_get_id = isset($_REQUEST['service_get_id']) ? intval($_REQUEST['service_get_id']) : 0;
        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.')
                ];
            }
        }
        $service_get_price_symbol = get_option('bookingor_settings_currency_symbol');
        $date_format = get_option('bookingor_date_format');
        self::$f_staff_assign = self::$bookingorDb->get_results(
            $wpdb->prepare(
                'SELECT 
				DISTINCT sa.assign_id, 
				sa.service_get_id, 
				sa.staff_get_id, 
				s.staff_id, 
				s.staff_icon, 
				s.staff_first_name, 
				s.staff_last_name, 
				s.staff_email, 
				s.staff_phone, 
				s.staff_sheet_weekly 
			FROM 
				%1$s s 
			INNER JOIN 
				%2$s sa ON s.staff_id = sa.staff_get_id 
			WHERE 
				sa.service_get_id = %3$s',
                $staff,
                $staff_assign,
                $service_get_id
            )
        );
        $staff_all = [];
        foreach (self::$f_staff_assign as $key => $value) {
            $staff_img = wp_get_attachment_url($value->staff_icon);
            $staff_all[] = [
                'staff_id' => $value->staff_id,
                'staff_get_id' => $value->staff_get_id,
                'service_get_id' => $value->service_get_id,
                'assign_id' => $value->assign_id,
                'staff_first_name' => $value->staff_first_name,
                'staff_last_name' => $value->staff_last_name,
                'staff_email' => $value->staff_email,
                'staff_icon' => $value->staff_icon,
                'staff_phone' => $value->staff_phone,
                'staff_img_url' => $staff_img,
                'staff_sheet_weekly' => $value->staff_sheet_weekly,
                'price_symbol' => $service_get_price_symbol,
                'date_format' => $date_format,
            ];
        }
        echo wp_json_encode($staff_all);
        die();
    }

    public static function bp_get_staff_email()
    {
        global $wpdb;
        $get_id_staff = sanitize_text_field(wp_unslash($_POST['staff_id'] ?? ''));
        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.')
                ];
            }
        }
        self::$get_staff_email = self::$bookingorDb->get_results(
            $wpdb->prepare(
                "SELECT staff_first_name, staff_last_name, staff_email, staff_phone, zoom_user_id FROM {$wpdb->prefix}bookingor_staff_add WHERE staff_id = %d",
                $get_id_staff
            )
        );
        echo wp_json_encode(self::$get_staff_email);
        die();
    }


}
