<?php 

use Bookify\Models\Bookify_Notification_Models;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Appointment
function bookify_get_appointments_meta( $appointment_id, $meta_key = false ) {
	$type = 'appointments';
	$column = 'appointment_id';
	
	return bookify_get_metadata( $type, $column, $appointment_id, $meta_key );
}

function bookify_add_appointments_meta( $appointment_id, $meta_key, $meta_value ) {
	$type = 'appointments';
	$column = 'appointment_id';
	
	return bookify_add_metadata( $type, $column, $appointment_id, $meta_key, $meta_value );
}

function bookify_update_appointments_meta( $appointment_id, $meta_key, $meta_value ) {
	$type = 'appointments';
	$column = 'appointment_id';
	
	return bookify_update_metadata( $type, $column, $appointment_id, $meta_key, $meta_value );
}

function bookify_delete_appointments_meta( $appointment_id, $meta_key = false ) {
	$type = 'appointments';
	$column = 'appointment_id';
	
	return bookify_delete_metadata( $type, $column, $appointment_id, $meta_key );
}


// Location 
function bookify_get_locations_meta( $location_id, $meta_key = false ) {
	$type = 'locations';
	$column = 'location_id';
	
	return bookify_get_metadata( $type, $column, $location_id, $meta_key );
}

function bookify_add_locations_meta( $location_id, $meta_key, $meta_value ) {
	$type = 'locations';
	$column = 'location_id';
	
	return bookify_add_metadata( $type, $column, $location_id, $meta_key, $meta_value );
}

function bookify_update_locations_meta( $location_id, $meta_key, $meta_value ) {
	$type = 'locations';
	$column = 'location_id';
	
	return bookify_update_metadata( $type, $column, $location_id, $meta_key, $meta_value );
}

function bookify_delete_locations_meta( $location_id, $meta_key = false ) {
	$type = 'locations';
	$column = 'location_id';
	
	return bookify_delete_metadata( $type, $column, $location_id, $meta_key );
}


// Service Category
function bookify_get_service_categories_meta( $service_categories_id, $meta_key = false ) {
	$type = 'service_categories';
	$column = 'service_categories_id';
	
	return bookify_get_metadata( $type, $column, $service_categories_id, $meta_key );
}

function bookify_add_service_categories_meta( $service_categories_id, $meta_key, $meta_value ) {
	$type = 'service_categories';
	$column = 'service_categories_id';
	
	return bookify_add_metadata( $type, $column, $service_categories_id, $meta_key, $meta_value );
}

function bookify_update_service_categories_meta( $service_categories_id, $meta_key, $meta_value ) {
	$type = 'service_categories';
	$column = 'service_categories_id';
	
	return bookify_update_metadata( $type, $column, $service_categories_id, $meta_key, $meta_value );
}

function bookify_delete_service_categories_meta( $service_categories_id, $meta_key = false ) {
	$type = 'service_categories';
	$column = 'service_categories_id';
	
	return bookify_delete_metadata( $type, $column, $service_categories_id, $meta_key );
}


// Service 
function bookify_get_services_meta( $service_id, $meta_key = false ) {
	$type = 'services';
	$column = 'service_id';
	
	return bookify_get_metadata( $type, $column, $service_id, $meta_key );
}

function bookify_add_services_meta( $service_id, $meta_key, $meta_value ) {
	$type = 'services';
	$column = 'service_id';
	
	return bookify_add_metadata( $type, $column, $service_id, $meta_key, $meta_value );
}

function bookify_update_services_meta( $service_id, $meta_key, $meta_value ) {
	$type = 'services';
	$column = 'service_id';
	
	return bookify_update_metadata( $type, $column, $service_id, $meta_key, $meta_value );
}

function bookify_delete_services_meta( $service_id, $meta_key = false ) {
	$type = 'services';
	$column = 'service_id';
	
	return bookify_delete_metadata( $type, $column, $service_id, $meta_key );
}


// Notification
function bookify_get_notifications_meta( $notification_id, $meta_key = false ) {
	$type = 'notifications';
	$column = 'notification_id';
	
	return bookify_get_metadata( $type, $column, $notification_id, $meta_key );
}

function bookify_add_notifications_meta( $notification_id, $meta_key, $meta_value ) {
	$type = 'notifications';
	$column = 'notification_id';
	
	return bookify_add_metadata( $type, $column, $notification_id, $meta_key, $meta_value );
}

function bookify_update_notifications_meta( $notification_id, $meta_key, $meta_value ) {
	$type = 'notifications';
	$column = 'notification_id';
	
	return bookify_update_metadata( $type, $column, $notification_id, $meta_key, $meta_value );
}

function bookify_delete_notifications_meta( $notification_id, $meta_key = false ) {
	$type = 'notifications';
	$column = 'notification_id';
	
	return bookify_delete_metadata( $type, $column, $notification_id, $meta_key );
}


// Payment 
function bookify_get_payments_meta( $payment_id, $meta_key = false ) {
	$type = 'payments';
	$column = 'payment_id';
	
	return bookify_get_metadata( $type, $column, $payment_id, $meta_key );
}

function bookify_add_payments_meta( $payment_id, $meta_key, $meta_value ) {
	$type = 'payments';
	$column = 'payment_id';
	
	return bookify_add_metadata( $type, $column, $payment_id, $meta_key, $meta_value );
}

function bookify_update_payments_meta( $payment_id, $meta_key, $meta_value ) {
	$type = 'payments';
	$column = 'payment_id';
	
	return bookify_update_metadata( $type, $column, $payment_id, $meta_key, $meta_value );
}

function bookify_delete_payments_meta( $payment_id, $meta_key = false ) {
	$type = 'payments';
	$column = 'payment_id';
	
	return bookify_delete_metadata( $type, $column, $payment_id, $meta_key );
}


// Generalize meta handlers
function bookify_get_metadata( $type, $column, $object_id, $meta_key = false ) {
	global $wpdb;

	$meta_value = array();

	if ( ! $object_id ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	$table = $wpdb->prefix . 'bookify_' . $type . '_meta';
	$table_exist = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) );

	if ( ! $table_exist ) {
		return false;
	}

	if ( $meta_key ) {
		$meta_value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM %i WHERE %i = %d AND meta_key = %s", $table, $column, $object_id, $meta_key ) );

		if ( ! $meta_value ) {
			return '';
		}

		return $meta_value;

	} else {
		$result = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM %i WHERE %i = %d", $table, $column, $object_id ) );
		
		if ( $result ) {
			foreach ( $result as $row ) {
				$meta_value[ $row->meta_key ] = $row->meta_value;
			}
		}

		if ( ! $meta_value ) {
			return array();
		}
	
		return $meta_value;
	}
}

function bookify_add_metadata( $type, $column, $object_id, $meta_key, $meta_value ) {
	global $wpdb;

	if ( ! $object_id || ! $meta_key || ! $meta_value ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	$table = $wpdb->prefix . 'bookify_' . $type . '_meta';
	$table_exist = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) );

	if ( ! $table_exist ) {
		return false;
	}

	$meta_key   = wp_unslash( $meta_key );
	$meta_value = wp_unslash( $meta_value );

	$result = $wpdb->insert(
		$table,
		array(
			$column         => $object_id,
			'meta_key'      => $meta_key,
			'meta_value'    => $meta_value,
		)
	);

	if ( ! $result ) {
		return false;
	}

	$mid = (int) $wpdb->insert_id;

	return $mid;
}

function bookify_update_metadata( $type, $column, $object_id, $meta_key, $meta_value ) {
	global $wpdb;

	if ( ! $object_id || ! $meta_key ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	$table = $wpdb->prefix . 'bookify_' . $type . '_meta';
	$table_exist = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) );

	if ( ! $table_exist ) {
		return false;
	}

	$meta_key = wp_unslash( $meta_key );
	$meta_value = wp_unslash( $meta_value );

	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM %i WHERE %i = %d AND meta_key = %s", $table, $column, $object_id, $meta_key ) );

	if ( empty( $meta_ids ) ) {
		$dynamic_function = 'bookify_add_' . $type . '_meta';
		
		if ( function_exists( $dynamic_function ) ) {
			return call_user_func( $dynamic_function, $object_id, $meta_key, $meta_value );
		} else {
			return false;
		}
	}

	$data = array( 
		'meta_value' => $meta_value,
	);
	$where = array(
		$column     => $object_id,
		'meta_key'  => $meta_key,
	);

	$result = $wpdb->update( $table, $data, $where );

	if ( ! $result ) {
		return false;
	}

	return $result;
}

function bookify_delete_metadata( $type, $column, $object_id, $meta_key = false ) {
	global $wpdb;

	if ( ! $object_id ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	$table = $wpdb->prefix . 'bookify_' . $type . '_meta';
	$table_exist = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) );

	if ( ! $table_exist ) {
		return false;
	}

	$query = $wpdb->prepare( "DELETE FROM %i WHERE %i = %d", $table, $column, $object_id );

	if ( $meta_key ) {
		$query .= $wpdb->prepare( ' AND meta_key = %s', $meta_key );
	}
	
	// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	$result = $wpdb->query( $query );

	if ( ! $result ) {
		return false;
	}

	return $result;
}
