<?php

if (!defined('ABSPATH')) {
    exit;
}

$wpbookit_actions = [
    'wpb_customer_booking_reminder' => ['wpbookit_trigger_customer_booking_email',1],
    'wpb_customer_registration' => ['wpbookit_trigger_user_registration_email',2],
    'wpb_customer_cancel_booking' => ['wpbookit_trigger_cancellation_email',1],
    'wpb_customer_booking_approval' => ['wpbookit_trigger_approval_email',1],
    'wpb_customer_booking_confirmation' => ['wpbookit_trigger_confirmation_email',1],
    'wpb_staff_booking_reminder' => ['wpbookit_trigger_staff_booking_email',1],
    'wpb_staff_booking_request' => ['wpbookit_trigger_staff_booking_request_email',1],
    'wpb_staff_booking_cancellation' => ['wpbookit_trigger_staff_booking_cancellation_email',1],
    'wpb_staff_registration' => ['wpbookit_trigger_staff_registration_email',2],
];

foreach ($wpbookit_actions as $action => $wpbookit_details) {
    $wpbookit_function = $wpbookit_details[0];
    $wpbookit_parameter_count = $wpbookit_details[1];
    add_action($action, $wpbookit_function, 10, $wpbookit_parameter_count);
}

function wpbookit_get_booking_instances($booking_id)
{
    $booking = new WPB_Booking($booking_id);
    $booking_type_id = $booking->get_booking_type_id();
    $customer_id = $booking->get_customer_id();
    $wp_customer_instance = !empty($customer_id) ? new WP_User($customer_id) :((Object) [
        'user_login' => $booking->get_booking_name(),
        'user_email' => $booking->get_booking_email(),
    ]);
    
    $booking_type = new WPB_Booking_Type($booking_type_id);
    $staff_id = get_metadata('wpb_booking_type', $booking_type_id, 'staff', true);
    $wp_staff_instance = new WP_User($staff_id);

    return [$booking, $booking_type, $wp_customer_instance, $wp_staff_instance];
}

function wpbookit_trigger_email($email_title, $to_email, $message_replacements,$custom_email_content='')
{
    $email_data = wpbookit_get_email_data($email_title);
    if ($email_data->status) {
        $email_subject = $email_data->emails_subject;
        if(!empty($custom_email_content)){
            $email_data->emails_content=$custom_email_content;
        }
        $email_message = wpbookit_email_content_key_replace($email_data->emails_content, $message_replacements);
        $status = wpbookit_mailer($to_email, $email_subject, $email_message);
    }
}

function wpbookit_trigger_customer_booking_email($booking_id)
{
    list($booking, $booking_type, $wp_customer_instance, $wp_staff_instance) = wpbookit_get_booking_instances($booking_id);


    wpbookit_trigger_email(
        'Customer Booking Reminder',
        $booking->get_booking_email(),
        ['user' => $wp_customer_instance, 'staff' => $wp_staff_instance, 'booking' => $booking, 'booking_type' => $booking_type]
    );
}

function wpbookit_trigger_approval_email($booking_id)
{
    list($booking, $booking_type, $wp_customer_instance, $wp_staff_instance) = wpbookit_get_booking_instances($booking_id);
    wpbookit_trigger_email(
        'Customer Booking Approval',
        $booking->get_booking_email(),
        ['user' => $wp_customer_instance, 'staff' => $wp_staff_instance, 'booking' => $booking, 'booking_type' => $booking_type]
    );
}

function wpbookit_trigger_confirmation_email($booking_id)
{
    list($booking, $booking_type, $wp_customer_instance, $wp_staff_instance) = wpbookit_get_booking_instances($booking_id);
    $email_reminder = $booking_type->get_booking_type_meta('email_reminder');
    $custom_email_content= '';
    if($email_reminder=='true'){
        $custom_email_content = $booking_type->get_booking_type_meta('email_content_editor');
    }
    wpbookit_trigger_email(
        'Customer Booking Confirmation',
        $booking->get_booking_email(),
        ['user' => $wp_customer_instance, 'staff' => $wp_staff_instance, 'booking' => $booking, 'booking_type' => $booking_type],
        $custom_email_content
    );
}

function wpbookit_trigger_cancellation_email($booking_id)
{
    list($booking, $booking_type, $wp_customer_instance, $wp_staff_instance) = wpbookit_get_booking_instances($booking_id);
    wpbookit_trigger_email(
        'Customer Booking Cancellation',
        $booking->get_booking_email(),
        ['user' => $wp_customer_instance, 'staff' => $wp_staff_instance, 'booking' => $booking, 'booking_type' => $booking_type]
    );
}

function wpbookit_trigger_user_registration_email(WP_User $user,$password)
{
    wpbookit_trigger_email(
        'Customer Registration',
        $user->user_email,
        ['user' => $user, 'password' => $password]
    );
}

function wpbookit_trigger_staff_registration_email(WP_User $user, $password)
{
    wpbookit_trigger_email(
        'Staff Registration',
        $user->user_email,
        ['user' => $user, 'password' => $password]
    );
}

function wpbookit_trigger_staff_booking_email($booking_id)
{
    list($booking, $booking_type, $wp_customer_instance, $wp_staff_instance) = wpbookit_get_booking_instances($booking_id);
    wpbookit_trigger_email(
        'Staff Booking Reminder',
        $wp_staff_instance->user_email,
        ['user' => $wp_customer_instance, 'staff' => $wp_staff_instance, 'booking' => $booking, 'booking_type' => $booking_type]
    );
}

function wpbookit_trigger_staff_booking_cancellation_email($booking_id)
{
    list($booking, $booking_type, $wp_customer_instance, $wp_staff_instance) = wpbookit_get_booking_instances($booking_id);
    wpbookit_trigger_email(
        'Staff Booking Cancellation',
        $wp_staff_instance->user_email,
        ['user' => $wp_customer_instance, 'staff' => $wp_staff_instance, 'booking' => $booking, 'booking_type' => $booking_type]
    );
}

function wpbookit_trigger_staff_booking_request_email($booking_id)
{
    list($booking, $booking_type, $wp_customer_instance, $wp_staff_instance) = wpbookit_get_booking_instances($booking_id);
    wpbookit_trigger_email(
        'Staff Booking Request',
        $wp_staff_instance->user_email,
        ['user' => $wp_customer_instance, 'staff' => $wp_staff_instance, 'booking' => $booking, 'booking_type' => $booking_type]
    );
}

function wpbookit_get_email_data($emails_title)
{
    global $wpdb;

    $sql = "SELECT * FROM {$wpdb->wpb_booking_emails} WHERE emails_title = %s";
    // phpcs:ignore
    return $wpdb->get_row($wpdb->prepare($sql, $emails_title));
}

function wpbookit_email_content_key_replace($email_message, $replacements)
{
    $site_path = home_url('/') . 'wp-admin/';
    $keys = [
        '{{customer_name}}' => isset($replacements['user']->data->display_name) ? 
            $replacements['user']->data->display_name : (isset($replacements['booking']) ? $replacements['booking']->get_booking_name() : ''),
        '{{customer_email}}' => isset($replacements['user']->data->user_email) ? 
            $replacements['user']->data->user_email : (isset($replacements['booking']) ? $replacements['booking']->get_booking_email() : ''),
        '{{booking_type}}' => (isset($replacements['booking']) && isset($replacements['booking_type'])) ? $replacements['booking']->get_booking_type()['name'] : '',
        '{{booking_status}}' => (isset($replacements['booking'])) ? (ucfirst(str_replace('wpb-', '', $replacements['booking']->get_status('view')))) : '',
        '{{booking_date}}' => (isset($replacements['booking'])) ? $replacements['booking']->get_booking_date() : '',
        '{{booking_time}}' => (isset($replacements['booking'])) ? $replacements['booking']->get_timeslot('view', true) : '',
        '{{staff_name}}' => (isset($replacements['staff']) && isset($replacements['staff']->data->display_name)) ? $replacements['staff']->data->display_name : '',
        '{{login_url}}' => wpbookit_get_first_wpb_profile_page_url() ?? '',
        '{{meeting_url}}' => (isset($replacements['booking'])) ? get_metadata('wpb_bookings', $replacements['booking']->get_id(), 'location_source',true) : '',
        '{{password}}' => isset($replacements['password']) ? $replacements['password'] : '', 
        '{{staff_login}}' => $site_path
    ];


    $keys = apply_filters('wpbookit_template_dynamic_keys_value', $keys,$email_message, $replacements);

    return str_replace(array_keys($keys), array_values($keys), $email_message);
}
