<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class ShippingGuard_Emails {
    public function init() {
        // Customer Email: Append protection info to the standard WC emails
        add_action( 'woocommerce_email_after_order_table', array( $this, 'add_protection_info_to_email' ), 10, 4 );
        
        // Admin Email: We will trigger this manually from the Claims API
    }

    /**
     * Notify customer in their order confirmation email.
     */
    public function add_protection_info_to_email( $order, $sent_to_admin, $plain_text, $email ) {
        if ( $sent_to_admin ) return; // Only for customer

        $is_protected = $order->get_meta( '_shipping_protection_enabled' );
        if ( 'yes' !== $is_protected ) return;

        $fee = $order->get_meta( '_shipping_protection_fee' );

        $protection_name = get_option( 'shippingguard_protection_name', 'Shipping Protection' );
        $protection_desc = get_option( 'shippingguard_protection_description', 'Protect your order from damage, loss or theft' );

        if ( $plain_text ) {
            echo "----------------------------------------\n";
            echo esc_html( strtoupper( $protection_name ) ) . "\n";
            echo "Your order is protected by " . esc_html( $protection_name ) . ".\n";
            echo "Protection Fee: " . esc_html( wp_strip_all_tags( wc_price( $fee ) ) ) . "\n";
            echo "----------------------------------------\n";
        } else {
            echo '<div style="margin-top: 20px; padding: 15px; background: #f0f7ff; border-radius: 8px; border: 1px solid #cce3ff;">';
            echo '<h3 style="margin-top: 0; color: #0056b3;">' . esc_html( $protection_name ) . '</h3>';
            echo '<p style="margin-bottom: 0;"><strong>Success!</strong> ' . esc_html( $protection_desc ) . '.</p>';
            echo '<p style="margin-top: 5px; font-size: 14px; color: #555;">Protection Fee: ' . wp_kses_post( wc_price( $fee ) ) . '</p>';
            echo '</div>';
        }
    }

    /**
     * Send standalone email to admin when a claim is submitted.
     */
    public static function send_admin_claim_notification( $claim_id, $order_id, $reason ) {
        $admin_email = get_option( 'admin_email' );
        $order = wc_get_order( $order_id );
        $customer_name = $order ? $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() : 'Unknown';

        $subject = 'New Shipping Protection Claim Submitted - Order #' . ( $order ? $order->get_order_number() : $order_id );
        
        $message  = "A new shipping protection claim has been submitted.\n\n";
        $message .= "Claim ID: #" . $claim_id . "\n";
        $message .= "Order: #" . ( $order ? $order->get_order_number() : $order_id ) . "\n";
        $message .= "Customer: " . $customer_name . "\n";
        $message .= "Reason: " . $reason . "\n\n";
        $message .= "Manage claims: " . admin_url( 'admin.php?page=shippingguard-claims' );

        wp_mail( $admin_email, $subject, $message );
    }
}
