<?php
defined( 'ABSPATH' ) || exit;

class ChainPay_Emails {

    public static function init() {
        add_filter( 'woocommerce_email_classes', array( __CLASS__, 'register' ) );
    }

    public static function register( $emails ) {
        require_once CHAINPAY_DIR . 'includes/emails/class-email-payment-confirmed.php';
        require_once CHAINPAY_DIR . 'includes/emails/class-email-others.php';
        $emails['ChainPay_Email_Confirmed']    = new ChainPay_Email_Confirmed();
        $emails['ChainPay_Email_Detected']     = new ChainPay_Email_Detected();
        $emails['ChainPay_Email_Underpayment'] = new ChainPay_Email_Underpayment();
        $emails['ChainPay_Email_Expired']      = new ChainPay_Email_Expired();
        return $emails;
    }

    public static function send_confirmed( $order, $invoice ) {
        if ( 'yes' !== get_option( 'chainpay_email_on_confirmed', 'yes' ) ) return;
        $mailer = WC()->mailer();
        if ( isset( $mailer->emails['ChainPay_Email_Confirmed'] ) ) {
            $mailer->emails['ChainPay_Email_Confirmed']->trigger( $order->get_id(), $order, $invoice );
        }
    }

    public static function send_detected( $order, $invoice ) {
        if ( 'yes' !== get_option( 'chainpay_email_on_detected', 'yes' ) ) return;
        $mailer = WC()->mailer();
        if ( isset( $mailer->emails['ChainPay_Email_Detected'] ) ) {
            $mailer->emails['ChainPay_Email_Detected']->trigger( $order->get_id(), $order, $invoice );
        }
    }

    public static function send_underpaid( $order, $invoice, $received ) {
        if ( 'yes' !== get_option( 'chainpay_underpayment_email', 'yes' ) ) return;
        $mailer = WC()->mailer();
        if ( isset( $mailer->emails['ChainPay_Email_Underpayment'] ) ) {
            $mailer->emails['ChainPay_Email_Underpayment']->trigger( $order->get_id(), $order, $invoice, $received );
        }
    }

    public static function send_expired( $order, $invoice ) {
        if ( 'yes' !== get_option( 'chainpay_email_on_expired', 'no' ) ) return;
        $mailer = WC()->mailer();
        if ( isset( $mailer->emails['ChainPay_Email_Expired'] ) ) {
            $mailer->emails['ChainPay_Email_Expired']->trigger( $order->get_id(), $order, $invoice );
        }
    }

    public static function payment_details_html( $order, $invoice ) {
        $coin    = ChainPay_Coin_Registry::get( isset( $invoice['coin'] ) ? $invoice['coin'] : '' );
        $color   = $coin ? $coin['color']  : '#f7931a';
        $symbol  = $coin ? $coin['symbol'] : ( isset( $invoice['coin'] ) ? $invoice['coin'] : '' );
        $name    = $coin ? $coin['name']   : $symbol;
        $tx      = ( isset( $invoice['tx_hash'] ) && $invoice['tx_hash'] ) ? '<code>' . esc_html( $invoice['tx_hash'] ) . '</code>' : '&mdash;';
        $confs   = isset( $invoice['confirmations'] ) ? $invoice['confirmations'] : 0;
        $fiat_c  = isset( $invoice['fiat_currency'] ) ? $invoice['fiat_currency'] : get_woocommerce_currency();
        $fiat_a  = isset( $invoice['amount_fiat'] )   ? $invoice['amount_fiat']   : 0;
        $crypto  = isset( $invoice['amount_crypto'] ) ? $invoice['amount_crypto'] : '';
        $rate    = isset( $invoice['exchange_rate'] ) ? $invoice['exchange_rate'] : 0;
        $inv_id  = isset( $invoice['invoice_id'] )    ? $invoice['invoice_id']    : '';
        $rows    = array(
            'Order'          => '#' . $order->get_order_number(),
            'Fiat Amount'    => wc_price( $fiat_a, array( 'currency' => $fiat_c ) ),
            'Crypto Amount'  => esc_html( $crypto . ' ' . $symbol ),
            'Coin / Network' => esc_html( $name . ( $coin ? ' — ' . $coin['network'] : '' ) ),
            'Exchange Rate'  => '1 ' . esc_html( $symbol ) . ' = $' . number_format( (float) $rate, 2 ),
            'TX Hash'        => $tx,
            'Confirmations'  => esc_html( (string) $confs ),
            'Invoice ID'     => '<code>' . esc_html( $inv_id ) . '</code>',
        );
        $html = '<table style="width:100%;border-collapse:collapse;font-size:14px;">';
        $html .= '<tr><td colspan="2" style="padding:8px 0;"><strong style="color:' . esc_attr( $color ) . ';">Payment Details</strong></td></tr>';
        foreach ( $rows as $lbl => $val ) {
            $html .= '<tr><td style="padding:6px 12px 6px 0;color:#666;width:130px;border-bottom:1px solid #f0f0f0;">' . esc_html( $lbl ) . '</td>';
            $html .= '<td style="padding:6px 0;font-weight:500;border-bottom:1px solid #f0f0f0;">' . $val . '</td></tr>';
        }
        return $html . '</table>';
    }
}
