<?php

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

add_action('chbk_booking_created', 'chbk_send_booking_emails', 10, 2);

function chbk_send_booking_emails($booking_id, $values)
{
    $vars = chbk_get_booking_email_vars($booking_id, $values);
    chbk_send_booking_confirmation_email($booking_id, $vars);

    if ((int) chbk_get_setting('email_admin_notification_enabled', 1) === 1) {
        $subject = chbk_render_template_variables(chbk_get_setting('email_admin_notification_subject', 'New booking'), $vars);
        $html = chbk_render_template_variables(chbk_get_setting('email_admin_notification_html', ''), $vars);
        $text = chbk_render_template_variables(chbk_get_setting('email_admin_notification_text', ''), $vars);
        chbk_send_email(get_option('admin_email'), $subject, $html, $text);
    }

    do_action('chbk_notification_dispatch', 'email', 'booking_created', $booking_id, $vars);
}

function chbk_get_booking_email_vars($booking_id, $values = [])
{
    $trip_id = isset($values['trip_id']) ? (int) $values['trip_id'] : (int) get_post_meta($booking_id, 'chbk_trip_id', true);
    $customer_email = isset($values['customer_email']) ? (string) $values['customer_email'] : (string) get_post_meta($booking_id, 'chbk_customer_email', true);
    $customer_name = isset($values['customer_name']) ? (string) $values['customer_name'] : (string) get_post_meta($booking_id, 'chbk_customer_name', true);
    $total_price = isset($values['total_price']) ? $values['total_price'] : get_post_meta($booking_id, 'chbk_total_price', true);
    $deposit_due = isset($values['deposit_due']) ? $values['deposit_due'] : get_post_meta($booking_id, 'chbk_deposit_due', true);

    return [
        'booking_id' => (int) $booking_id,
        'trip_title' => get_the_title($trip_id),
        'customer_name' => $customer_name,
        'customer_email' => $customer_email,
        'total_price' => chbk_format_decimal_value($total_price),
        'deposit_due' => chbk_format_decimal_value($deposit_due),
        'phone_number' => (string) chbk_get_setting('payment_phone_number', ''),
        'payment_message' => chbk_get_payment_message_for_trip($trip_id),
    ];
}

function chbk_send_booking_confirmation_email($booking_id, $values = [])
{
    if ((int) chbk_get_setting('email_booking_confirmation_enabled', 1) !== 1) {
        return false;
    }

    $vars = !empty($values) ? $values : chbk_get_booking_email_vars($booking_id);
    $to = isset($vars['customer_email']) ? sanitize_email((string) $vars['customer_email']) : '';
    if ($to === '') {
        return false;
    }

    $subject = chbk_render_template_variables(chbk_get_setting('email_booking_confirmation_subject', 'Booking confirmed'), $vars);
    $html = chbk_render_template_variables(chbk_get_setting('email_booking_confirmation_html', ''), $vars);
    $text = chbk_render_template_variables(chbk_get_setting('email_booking_confirmation_text', ''), $vars);

    return (bool) chbk_send_email($to, $subject, $html, $text);
}

function chbk_send_email($to, $subject, $html, $text = '')
{
    $headers = ['Content-Type: text/html; charset=UTF-8'];
    $message = $html;
    if (empty($message)) {
        $message = nl2br(esc_html($text));
    }

    return wp_mail($to, $subject, $message, $headers);
}

function chbk_format_decimal_value($value)
{
    return number_format((float) $value, 2, '.', '');
}
