<?php
/**
 * Service and variation catalog used by customer appointment changes.
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class YATOON_Manage_Service_Catalog {
    public static function choices() {
        global $wpdb;
        $services = yatoon_db_get_results(
            "SELECT id, name, duration_minutes, price
             FROM {$wpdb->prefix}yatoon_services
             WHERE is_active = 1
             ORDER BY sort_order ASC, name ASC, id ASC"
        );
        $options = yatoon_db_get_results(
            "SELECT id, service_id, name, duration_minutes, price_modifier, square_id, square_version, sort_order
             FROM {$wpdb->prefix}yatoon_service_options
             WHERE COALESCE(group_name, '') != 'Add-on'
             ORDER BY service_id ASC, sort_order ASC, name ASC, id ASC"
        );
        $by_service = array();
        foreach ( (array) $options as $option ) {
            $by_service[ absint( $option->service_id ) ][] = $option;
        }

        $choices = array();
        foreach ( (array) $services as $service ) {
            $service_id = absint( $service->id );
            $main_options = $by_service[ $service_id ] ?? array();
            foreach ( $main_options as $option ) {
                $choices[] = array(
                    'value'      => $service_id . ':' . absint( $option->id ),
                    'service_id' => $service_id,
                    'option_id'  => absint( $option->id ),
                    'group'      => (string) $service->name,
                    'name'       => (string) $option->name,
                    'duration'   => max( 1, absint( $option->duration_minutes ) ),
                    'price'      => (float) $service->price + (float) $option->price_modifier,
                    'is_whole'   => false,
                );
            }
        }
        return $choices;
    }

    public static function current_main_option_id( $booking_id ) {
        global $wpdb;
        return absint( yatoon_db_get_var( $wpdb->prepare(
            "SELECT o.id
             FROM {$wpdb->prefix}yatoon_booking_options bo
             INNER JOIN {$wpdb->prefix}yatoon_service_options o ON o.id=bo.option_id
             WHERE bo.booking_id=%d
               AND (o.group_name IS NULL OR o.group_name='' OR o.group_name!='Add-on')
             ORDER BY bo.id ASC LIMIT 1",
            absint( $booking_id )
        ) ) );
    }

    public static function resolve( $service_id, $option_id = 0 ) {
        global $wpdb;
        $service = yatoon_db_get_row( $wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}yatoon_services WHERE id=%d AND is_active=1 LIMIT 1",
            absint( $service_id )
        ) );
        if ( ! $service || ! $option_id ) {
            return null;
        }
        $service->selected_option_id = 0;
        $option = yatoon_db_get_row( $wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}yatoon_service_options
             WHERE id=%d AND service_id=%d
               AND (group_name IS NULL OR group_name='' OR group_name!='Add-on') LIMIT 1",
            absint( $option_id ),
            absint( $service_id )
        ) );
        if ( ! $option ) {
            return null;
        }
        $service->selected_option_id      = absint( $option->id );
        $service->selected_option_name    = (string) $option->name;
        $service->selected_option_square_id = (string) $option->square_id;
        $service->selected_option_square_version = absint( $option->square_version );
        $service->duration_minutes        = max( 1, absint( $option->duration_minutes ) );
        $service->price                   = (float) $service->price + (float) $option->price_modifier;
        return $service;
    }

    public static function replace_booking_options( $booking_id, $option_id ) {
        global $wpdb;
        $booking_id = absint( $booking_id );
        $option_id  = absint( $option_id );
        if ( ! $booking_id ) {
            return false;
        }
        yatoon_db_delete( $wpdb->prefix . 'yatoon_booking_options', array( 'booking_id' => $booking_id ), array( '%d' ) );
        if ( $option_id ) {
            return false !== yatoon_db_insert(
                $wpdb->prefix . 'yatoon_booking_options',
                array( 'booking_id' => $booking_id, 'option_id' => $option_id ),
                array( '%d', '%d' )
            );
        }
        return true;
    }
}
