<?php
/**
 * Pro delivery log admin page.
 *
 * @package AlgoForms
 */

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

/**
 * @return void
 */
function algo_forms_render_delivery_log_page()
{
    if (!current_user_can(ALGO_FORMS_CAP)) {
        wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'algo-forms'));
    }

    if (!algo_forms_pro_feature('delivery_log')) {
        algo_forms_render_pro_upsell(
            __('Delivery log', 'algo-forms'),
            __('See every Algo Forms email attempt — success, failure, recipient, and SMTP detail.', 'algo-forms')
        );
        return;
    }

    global $wpdb;
    $table = algo_forms_mail_log_table();
    algo_forms_install_mail_log_table();

    $status = isset($_GET['status']) ? sanitize_key(wp_unslash($_GET['status'])) : '';
    $where = '1=1';
    $params = array();
    if ($status === 'sent' || $status === 'failed') {
        $where .= ' AND status = %s';
        $params[] = $status;
    }

    $sql = "SELECT * FROM {$table} WHERE {$where} ORDER BY created_at DESC, id DESC LIMIT 200";
    // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $rows = $params
        ? $wpdb->get_results($wpdb->prepare($sql, $params))
        : $wpdb->get_results($sql);

    algo_forms_admin_shell_start(
        __('Delivery log', 'algo-forms'),
        __('Recent mail attempts from Algo Forms (company notifications, visitor copy, and tests).', 'algo-forms'),
        'mint'
    );
    ?>
        <form method="get" class="af-panel" style="display:flex; gap:0.6rem; align-items:center; flex-wrap:wrap;">
            <input type="hidden" name="page" value="algo-forms-delivery-log">
            <label for="af-log-status"><?php esc_html_e('Status', 'algo-forms'); ?></label>
            <select name="status" id="af-log-status">
                <option value=""><?php esc_html_e('All', 'algo-forms'); ?></option>
                <option value="sent" <?php selected($status, 'sent'); ?>><?php esc_html_e('Sent', 'algo-forms'); ?></option>
                <option value="failed" <?php selected($status, 'failed'); ?>><?php esc_html_e('Failed', 'algo-forms'); ?></option>
            </select>
            <button class="button button-primary"><?php esc_html_e('Filter', 'algo-forms'); ?></button>
        </form>

        <div class="af-panel" style="padding:0; overflow:hidden;">
            <table class="wp-list-table widefat fixed striped" style="border:0; box-shadow:none; margin:0;">
                <thead>
                    <tr>
                        <th style="width:140px;"><?php esc_html_e('When', 'algo-forms'); ?></th>
                        <th style="width:80px;"><?php esc_html_e('Status', 'algo-forms'); ?></th>
                        <th><?php esc_html_e('To', 'algo-forms'); ?></th>
                        <th><?php esc_html_e('Subject', 'algo-forms'); ?></th>
                        <th><?php esc_html_e('Detail', 'algo-forms'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($rows) : ?>
                        <?php foreach ($rows as $row) : ?>
                            <tr>
                                <td><?php echo esc_html((string) $row->created_at); ?></td>
                                <td>
                                    <span class="af-log-status af-log-status--<?php echo esc_attr($row->status); ?>">
                                        <?php echo esc_html($row->status === 'sent' ? __('Sent', 'algo-forms') : __('Failed', 'algo-forms')); ?>
                                    </span>
                                </td>
                                <td><?php echo esc_html((string) $row->to_email); ?></td>
                                <td>
                                    <?php echo esc_html((string) $row->subject); ?>
                                    <?php if (!empty($row->context)) : ?>
                                        <div class="description"><?php echo esc_html((string) $row->context); ?></div>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo esc_html((string) $row->detail); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <tr><td colspan="5"><?php esc_html_e('No delivery log entries yet.', 'algo-forms'); ?></td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    <?php
    algo_forms_admin_shell_end();
}
