<?php

namespace Bookingor\Frontend\Controller\Location;

class LocationControlFront
{
    public static $location_get_data;
    private static $bookingorDb;
    public static $f_location;
    public static $f_location_assign;
    public static $dp_prefix = "bookingor_";

    /**
     * Retrieves and returns the list of locations for the frontend.
     *
     * Retrieves location details including ID, title, icon, country, city, address, phone number, latitude, longitude, time sheet, and status.
     *
     * @return array|null An array of location details or null if no data is found.
     */
    public static function bp_front_location_view($location_get_id = null)
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $location = $wpdb->prefix . self::$dp_prefix . 'location';

        if ($location_get_id != null) {
            return  self::$f_location = self::$bookingorDb->get_results($wpdb->prepare("SELECT location_id, location_title, location_icon, location_country, location_city, location_address, location_phone, location_latitude, location_longitude, location_time_sheet, status FROM %1\$s WHERE location_id = %2\$d", $location, $location_get_id));
        } else {
            return  self::$f_location = self::$bookingorDb->get_results($wpdb->prepare("SELECT location_id, location_title, location_icon, location_country, location_city, location_address, location_phone, location_latitude, location_longitude, location_time_sheet, status FROM %1\$s", $location));
        }
    }
    /**
     * Retrieves and returns the list of locations assigned to a given service ID.
     *
     * Retrieves location details including ID, title, icon, country, city, address, phone number, latitude, longitude, time sheet, and status.
     *
     * @return array|null An array of location details or null if no data is found.
     */
    public static function service_location_assign()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $location_assign_services = $wpdb->prefix . self::$dp_prefix . 'location_assign_service';
        $location_assign_staff = $wpdb->prefix . self::$dp_prefix . 'location_assign_staff';
        $location = $wpdb->prefix . self::$dp_prefix . 'location';
        return self::$f_location_assign  =  self::$bookingorDb->get_results($wpdb->prepare("
            SELECT 
                l.location_id, 
                l.location_title, 
                l.location_icon, 
                las.location_get_id, 
                las.service_get_id, 
                l.location_country, 
                l.location_city, 
                l.location_address, 
                l.location_phone, 
                l.location_latitude, 
                l.location_longitude, 
                last.staff_get_id 
            FROM 
                %1\$s l 
            LEFT JOIN 
                %2\$s las ON las.location_get_id = l.location_id 
            LEFT JOIN 
                %3\$s last ON last.location_get_id = l.location_id 
            GROUP BY 
                l.location_address
        ", $location, $location_assign_services, $location_assign_staff));
    }
    /**
     * Retrieves location details including ID, title, icon, country, city, address, phone number, latitude, longitude, time sheet, and status.
     *
     * @return array|null An array of location details or null if no data is found.
     */
    public static function location_all_services()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $location_assign_services = $wpdb->prefix . self::$dp_prefix . 'location_assign_service';
        $location_assign_staff = $wpdb->prefix . self::$dp_prefix . 'location_assign_staff';
        $location = $wpdb->prefix . self::$dp_prefix . 'location';
        $services = $wpdb->prefix . self::$dp_prefix . 'services';
        $staff = $wpdb->prefix . self::$dp_prefix . 'staff_add';
        if (!empty($_REQUEST['location_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.')
                    ];
                }
            }
            $location_assign_all_service = self::$bookingorDb->get_results(
                $wpdb->prepare(
                    "SELECT 
                       l.location_id, 
                       GROUP_CONCAT(lsa.service_get_id) AS service_id, 
                       l.location_title, 
                       l.location_icon, 
                       lsa.location_get_id, 
                       lsa.service_get_id, 
                       l.location_country, 
                       l.location_city, 
                       l.location_address, 
                       l.location_phone, 
                       GROUP_CONCAT(s.service_name SEPARATOR ' | ') AS service_name, 
                       l.location_latitude, 
                       l.location_longitude, 
                       las.staff_get_id 
                   FROM 
                       {$wpdb->prefix}bookingor_location l
                   INNER JOIN 
                       {$wpdb->prefix}bookingor_location_assign_service lsa ON lsa.location_get_id = l.location_id
                   LEFT JOIN 
                       {$wpdb->prefix}bookingor_location_assign_staff las ON las.location_get_id = l.location_id
                   LEFT JOIN 
                       {$wpdb->prefix}bookingor_services s ON lsa.service_get_id = s.service_id
                   GROUP BY 
                       lsa.location_get_id",
                    $location,
                    $location_assign_services,
                    $location_assign_staff,
                    $services
                )
            );

            return $location_assign_all_service;
        }
    }

    /**
     * Retrieves and returns the list of locations assigned to a staff member.
     *
     * Retrieves location details including ID, title, icon, country, city, address, phone number, latitude, longitude, and the staff IDs assigned to the location.
     *
     * @return array|null An array of location details or null if no data is found.
     */
    public static function service_staff_location_assign_maps()
    {

        global $wpdb;
        self::$bookingorDb = $wpdb;
        $location_assign_services = $wpdb->prefix . self::$dp_prefix . 'location_assign_service';
        $location_assign_staff = $wpdb->prefix . self::$dp_prefix . 'location_assign_staff';
        $location = $wpdb->prefix . self::$dp_prefix . 'location';
        $location_assign_response  =  self::$bookingorDb->get_results($wpdb->prepare("
            SELECT 
                l.location_id, 
                l.location_title, 
                l.location_icon, 
                las.location_get_id, 
                las.service_get_id, 
                l.location_country, 
                l.location_city, 
                l.location_address, 
                l.location_phone, 
                l.location_latitude, 
                l.location_longitude, 
                last.staff_get_id 
            FROM 
                %1\$s l 
            INNER JOIN 
                %2\$s las ON las.location_get_id = l.location_id 
            LEFT JOIN 
                %3\$s last ON last.location_get_id = l.location_id
        ", $location, $location_assign_services, $location_assign_staff));
        echo wp_json_encode($location_assign_response);
        die();
    }
    /**
     * Retrieves and returns the list of locations assigned to a given service ID.
     *
     * Retrieves location details including ID, title, icon, country, city, address, phone number, latitude, longitude, and the service ID assigned to the location.
     *
     * @return array|null An array of location details or null if no data is found.
     */
    public static function service_location_assign_with_id()
    {
        $response  = new \stdclass();
        $response->success = false;
        $default_img =    plugin_dir_url(dirname(dirname(__FILE__))) . 'assets/images/serv-image.jpg';
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $location_assign = $wpdb->prefix . self::$dp_prefix . 'location_assign_service';
        $location = $wpdb->prefix . self::$dp_prefix . 'location';
        $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.')
                ];
            }
        }
        $location_response  =  self::$bookingorDb->get_results($wpdb->prepare("
            SELECT 
                l.location_id, 
                l.location_title, 
                l.location_icon, 
                la.location_get_id, 
                la.service_get_id, 
                l.location_country, 
                l.location_city, 
                l.location_address, 
                l.location_phone, 
                l.location_latitude, 
                l.location_longitude 
            FROM 
                %1\$s l 
            LEFT JOIN 
                %2\$s la ON la.location_get_id = l.location_id 
            WHERE 
                la.service_get_id = %3\$d
        ", $location, $location_assign, $service_get_id));
        echo wp_json_encode($location_response);
        die();
    }
    /**
     * Retrieves location details from the database for a given location ID.
     *
     * Expects the location ID to be passed in the request as 'locations_id'.
     *
     * @return void
     */
    public function front_get_location()
    {
        $response  = new \stdclass();
        $response->success = false;
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $location = $wpdb->prefix . self::$dp_prefix . 'location';
        $location_id = sanitize_text_field(wp_unslash($_POST['locations_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.')
                ];
            }
        }
        $response = self::$bookingorDb->get_results($wpdb->prepare("SELECT location_id, location_title, location_icon, location_country, location_city, location_address, location_phone, location_latitude, location_longitude, location_time_sheet, status FROM {$wpdb->prefix}bookingor_location WHERE location_id = %d", $location_id));
        echo wp_json_encode($response);
        die;
    }

    public static function bp_get_location_view()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $location = $wpdb->prefix . self::$dp_prefix . 'location';
        $query = "SELECT l.location_id, l.location_country, l.location_city, l.location_address, l.location_phone, l.location_latitude, l.location_longitude
          FROM %1\$s AS l";
        return self::$location_get_data = self::$bookingorDb->get_results(self::$bookingorDb->prepare($query, $location));
    }
}
