<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

function lt_core_enquiry_operations_menu() {
    add_submenu_page(
        'lt-core-dashboard',
        __( 'Enquiry Operations', 'law-temple-core' ),
        __( 'Enquiry Operations', 'law-temple-core' ),
        'lt_manage_enquiries',
        'lt-enquiry-operations',
        'lt_core_enquiry_operations_page'
    );
}
add_action( 'admin_menu', 'lt_core_enquiry_operations_menu', 32 );

function lt_core_enquiry_test_delivery() {
    if ( ! lt_core_current_user_can( 'lt_manage_enquiries' ) ) { wp_die( esc_html__( 'You are not allowed to test enquiry delivery.', 'law-temple-core' ) ); }
    check_admin_referer( 'lt_enquiry_test_delivery' );
    $user = wp_get_current_user();
    $target = is_email( $user->user_email ) ? $user->user_email : get_option( 'admin_email' );
    $subject = sprintf( __( '[%s] Website enquiry delivery test', 'law-temple-core' ), lt_core_get_firm_name() );
    $body = sprintf( __( "This is a delivery test generated by Law Temple Core at %s.\n\nNo client enquiry data is included.", 'law-temple-core' ), wp_date( 'Y-m-d H:i:s T' ) );
    $sent = wp_mail( $target, $subject, $body );
    $url = add_query_arg( array( 'page'=>'lt-enquiry-operations', 'lt_test'=>( $sent ? 'sent' : 'failed' ) ), admin_url( 'admin.php' ) );
    wp_safe_redirect( $url ); exit;
}
add_action( 'admin_post_lt_enquiry_test_delivery', 'lt_core_enquiry_test_delivery' );

function lt_core_enquiry_operations_page() {
    if ( ! lt_core_current_user_can( 'lt_manage_enquiries' ) ) { return; }
    $recipient = lt_core_option( 'enquiry_recipient' ) ?: get_option( 'admin_email' );
    $external  = lt_core_option( 'external_form_url' );
    $upload    = (bool) lt_core_option( 'allow_file_upload' );
    $status    = sanitize_key( $_GET['lt_test'] ?? '' );
    echo '<div class="wrap"><h1>' . esc_html__( 'Enquiry Operations', 'law-temple-core' ) . '</h1>';
    echo '<p>' . esc_html__( 'Operational status for website intake. The framework emails enquiries and does not retain a searchable database of client messages by default.', 'law-temple-core' ) . '</p>';
    if ( 'sent' === $status ) { echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'A test message was handed to WordPress mail for delivery. Confirm arrival in your inbox and SMTP logs.', 'law-temple-core' ) . '</p></div>'; }
    elseif ( 'failed' === $status ) { echo '<div class="notice notice-error"><p>' . esc_html__( 'WordPress reported that the test message could not be handed to the mail system. Review SMTP configuration.', 'law-temple-core' ) . '</p></div>'; }
    echo '<table class="widefat striped" style="max-width:950px"><tbody>';
    $rows = array(
        array( __( 'Form mode', 'law-temple-core' ), $external ? __( 'External intake form', 'law-temple-core' ) : __( 'Internal WordPress form', 'law-temple-core' ) ),
        array( __( 'Recipient', 'law-temple-core' ), $recipient ),
        array( __( 'CC recipients', 'law-temple-core' ), lt_core_option( 'enquiry_cc' ) ?: __( 'None', 'law-temple-core' ) ),
        array( __( 'Private upload', 'law-temple-core' ), $upload ? __( 'Enabled — PDF only by default', 'law-temple-core' ) : __( 'Disabled', 'law-temple-core' ) ),
        array( __( 'Privacy page', 'law-temple-core' ), absint( lt_core_option( 'privacy_page_id' ) ) ? get_the_title( absint( lt_core_option( 'privacy_page_id' ) ) ) : __( 'Not configured', 'law-temple-core' ) ),
    );
    foreach ( $rows as $row ) { echo '<tr><th style="width:220px">' . esc_html( $row[0] ) . '</th><td>' . esc_html( $row[1] ) . '</td></tr>'; }
    echo '</tbody></table>';
    $test = wp_nonce_url( admin_url( 'admin-post.php?action=lt_enquiry_test_delivery' ), 'lt_enquiry_test_delivery' );
    echo '<p style="margin-top:18px"><a class="button button-primary" href="' . esc_url( $test ) . '">' . esc_html__( 'Send delivery test to my account email', 'law-temple-core' ) . '</a>';
    if ( lt_core_current_user_can( 'lt_manage_firm_settings' ) ) { echo ' <a class="button" href="' . esc_url( admin_url( 'admin.php?page=lt-core-settings&tab=enquiry' ) ) . '">' . esc_html__( 'Open Enquiry Settings', 'law-temple-core' ) . '</a>'; }
    echo '</p><p class="description">' . esc_html__( 'Successful wp_mail() handoff does not guarantee inbox delivery. Use authenticated SMTP/transactional mail and review provider logs before launch.', 'law-temple-core' ) . '</p></div>';
}
