<?php
/**
 * Database Constants for Nemtly Booking Plugin
 *
 * This file defines all database table name constants used throughout the plugin.
 * All custom tables use the 'nemtly_' prefix after the WordPress table prefix.
 *
 * @package Nemtly_Booking
 * @since   1.0.0
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Database table name constants
 * All table names include the WordPress prefix and 'nemtly_' plugin prefix
 */

// Main bookings table
if ( ! defined( 'NEMTLY_BOOKING_TABLE_BOOKINGS' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_BOOKINGS', 'nemtly_bookings' );
}

/**
 * Table for payment tracking
 *
 * @since PBOOKING_SINCE
 */
// Payment tracking table
if ( ! defined( 'NEMTLY_BOOKING_TABLE_PAYMENTS' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_PAYMENTS', 'nemtly_payments' );
}

/**
 * Booking form fields definition table
 *
 * @since PBOOKING_SINCE
 */
if ( ! defined( 'NEMTLY_BOOKING_TABLE_BOOKING_FIELDS' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_BOOKING_FIELDS', 'nemtly_booking_fields' );
}

/**
 * Field responses table
 *
 * @since PBOOKING_SINCE
 */
if ( ! defined( 'NEMTLY_BOOKING_TABLE_FIELD_RESPONSES' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_FIELD_RESPONSES', 'nemtly_field_responses' );
}

/**
 * Availability slots table
 *
 * @since PBOOKING_SINCE
 */
if ( ! defined( 'NEMTLY_BOOKING_TABLE_AVAILABILITY' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_AVAILABILITY', 'nemtly_availability' );
}

/**
 * Booking logs/auditing table
 *
 * @since PBOOKING_SINCE
 */
if ( ! defined( 'NEMTLY_BOOKING_TABLE_BOOKING_LOGS' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_BOOKING_LOGS', 'nemtly_booking_logs' );
}
/**
 * Event management tables
 *
 * @since PBOOKING_SINCE
 */
if ( ! defined( 'NEMTLY_BOOKING_TABLE_EVENT_PATTERNS' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_EVENT_PATTERNS', 'nemtly_event_patterns' );
}

/**
 * Notifications queue table
 *
 * @since PBOOKING_SINCE
 */
if ( ! defined( 'NEMTLY_BOOKING_TABLE_NOTIFICATIONS' ) ) {
    define( 'NEMTLY_BOOKING_TABLE_NOTIFICATIONS', 'nemtly_notifications' );
}

/**
 * Helper function to get full table name with WordPress prefix
 *
 * @param string $table_constant The table constant name.
 * @return string Full table name with WordPress prefix.
 */
function nemtly_booking_get_table_name( $table_constant ) {
    global $wpdb;

    $allowed_tables = [
        'nemtly_bookings',
        'nemtly_payments',
        'nemtly_booking_fields',
        'nemtly_field_responses',
        'nemtly_availability',
        'nemtly_booking_logs',
        'nemtly_event_patterns',
        'nemtly_notifications',
    ];

    // Validate the provided table constant using whitelist approach
    if ( ! is_string( $table_constant ) || ! in_array( $table_constant, $allowed_tables, true ) ) {
        wp_die( esc_html__( 'Invalid table name requested.', 'nemtly-booking' ) );
    }

    return $wpdb->prefix . $table_constant;
}

/**
 * Get the bookings table name with WordPress prefix
 *
 * @return string Full bookings table name.
 */
function nemtly_booking_get_bookings_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_BOOKINGS );
}

/**
 * Get the payments table name with WordPress prefix
 *
 * @return string Full payments table name.
 */
function nemtly_booking_get_payments_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_PAYMENTS );
}

/**
 * Get the booking fields table name with WordPress prefix
 *
 * @return string Full booking fields table name.
 */
function nemtly_booking_get_booking_fields_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_BOOKING_FIELDS );
}

/**
 * Get the field responses table name with WordPress prefix
 *
 * @return string Full field responses table name.
 */
function nemtly_booking_get_field_responses_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_FIELD_RESPONSES );
}

/**
 * Get the availability table name with WordPress prefix
 *
 * @return string Full availability table name.
 */
function nemtly_booking_get_availability_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_AVAILABILITY );
}

/**
 * Get the booking logs table name with WordPress prefix
 *
 * @return string Full booking logs table name.
 */
function nemtly_booking_get_booking_logs_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_BOOKING_LOGS );
}

/**
 * Get the event patterns table name with WordPress prefix
 *
 * @return string Full event patterns table name.
 */
function nemtly_booking_get_event_patterns_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_EVENT_PATTERNS );
}

/**
 * Get the notifications table name with WordPress prefix
 *
 * @return string Full notifications table name.
 */
function nemtly_booking_get_notifications_table() {
    return nemtly_booking_get_table_name( NEMTLY_BOOKING_TABLE_NOTIFICATIONS );
}
