<?php

namespace Bookingor\Frontend\Controller\Customer;

use Bookingor\Integrations\Email\EmailNotification;

class CustomerControlFront
{
    private static $bookingorDb;
    public static $customer_order_booked;
    public static $customer_front_profile;
    public static $dp_prefix = "bookingor_";


    /**
     * Validate customer email address. If the email address is already
     * registered with the site, return an error message. If the email
     * address is available for registration, return a success message.
     *
     * @return array
     */
    public static function validateCustomerForm()
    {
        $response = array();
        global $wpdb;
        global $user;
        self::$bookingorDb = $wpdb;
        $response['success'] = false;
        $customer_email_address = sanitize_email(wp_unslash($_POST['customer_email_address'] ?? ""));
        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.')
                ];
            }
        }
        $user = get_user_by('user_email', $customer_email_address);
        $user_query = self::$bookingorDb->get_row(
            $wpdb->prepare(
                "SELECT ID FROM $wpdb->users WHERE user_email = %s",
                $customer_email_address
            )
        );
        if (isset($_REQUEST['action']) && !is_user_logged_in()) {
            if ($user_query) {
                $response = [
                    'status' => htmlspecialchars('error'),
                    'message' => htmlspecialchars('Email already exists')
                ];
            } else {
                $response = [
                    'status' => htmlspecialchars('success'),
                    'message' => htmlspecialchars('Email is available for registration')
                ];
            }
            echo wp_json_encode($response);
            die();
        }
    }
    /**
     * Retrieve a list of booking orders for the currently logged-in user.
     *
     * This method returns a list of booking orders for the currently
     * logged-in user. The list includes the booking start time, service
     * name, service duration, service capacity, service cost, booking
     * status, picked date, start time, end time, payment method, customer
     * first name, customer last name, customer email, customer phone,
     * staff first name, staff last name, staff email, and staff phone.
     *
     * @return array List of booking orders for the currently logged-in user.
     */
    public static function bp_booking_customer_order()
    {
        $current_user = wp_get_current_user();
        $user_email = $current_user->user_email;
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $customer_booked = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
        $customer_detail = $wpdb->prefix . self::$dp_prefix . 'customer_add';
        $escaped_user_email = $wpdb->prepare('%s', $user_email);
        // print_r($escaped_user_email);
        $staff_detail = $wpdb->prefix . self::$dp_prefix . 'staff_add';
        $service_detail = $wpdb->prefix . self::$dp_prefix . 'services';
        $customer_payment = $wpdb->prefix . self::$dp_prefix . 'customers_payment';
        if (!empty($customer_booked) && !empty($customer_detail) && !empty($staff_detail) && !empty($service_detail) && !empty($customer_payment)) {
            self::$customer_order_booked = self::$bookingorDb->get_results(
                $wpdb->prepare(
                    "SELECT 
                    COUNT(cb.start_time) AS start_time_count, 
                    cb.book_id, 
                    cb.service_get_id, 
                    cb.customer_get_id, 
                    cb.staff_get_id,
                    cb.zoom_meeting_id,
                    cp.book_get_id,
                    cp.payment_status,
                    cp.paid_amount, 
                    sd.service_name, 
                    sd.service_duration,
                    sd.service_duration_type, 
                    sd.service_capacity_min, 
                    sd.service_capacity_max, 
                    sd.service_cost, 
                    cb.appoint_status, 
                    cb.picked_date, 
                    cb.picked_date_end, 
                    cb.start_time, 
                    cb.end_time, 
                    cb.payment_method, 
                    cd.customer_id, 
                    cd.customer_first_name, 
                    cd.customer_last_name, 
                    cd.customer_email, 
                    cd.customer_phone, 
                    st.staff_first_name, 
                    st.staff_last_name, 
                    st.staff_email, 
                    st.staff_phone 
                FROM 
                    %1\$s cb 
                LEFT JOIN 
                    %2\$s cd ON cb.customer_get_id = cd.customer_id 
                LEFT JOIN 
                    %3\$s sd ON cb.service_get_id = sd.service_id 
                LEFT JOIN 
                    %4\$s st ON cb.staff_get_id = st.staff_id 
                LEFT JOIN
                    %5\$s cp ON cb.book_id = cp.book_get_id
                WHERE 
                    cd.customer_email = '$user_email'
                GROUP BY 
                    cb.start_time",
                    $customer_booked,
                    $customer_detail,
                    $service_detail,
                    $staff_detail,
                    $customer_payment,
                )
            );
            return self::$customer_order_booked;
        } else {
        }
    }

}
