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

class ChainPay_Email_Confirmed extends WC_Email {

    public $invoice = array();

    public function __construct() {
        $this->id             = 'chainpay_confirmed';
        $this->title          = 'ChainPay — Payment Confirmed';
        $this->description    = 'Sent to the customer when their crypto payment is confirmed on-chain.';
        $this->heading        = 'Your payment has been confirmed!';
        $this->subject        = '[{site_name}] Payment confirmed — Order #{order_number}';
        $this->customer_email = true;
        $this->placeholders   = array( '{site_name}' => $this->get_from_name(), '{order_number}' => '' );
        parent::__construct();
    }

    public function trigger( $order_id, $order = false, $invoice = array() ) {
        $this->setup_locale();
        if ( $order_id && ! ( $order instanceof WC_Order ) ) $order = wc_get_order( $order_id );
        if ( ! $order ) { $this->restore_locale(); return; }
        $this->object                         = $order;
        $this->invoice                        = $invoice;
        $this->recipient                      = $order->get_billing_email();
        $this->placeholders['{order_number}'] = $order->get_order_number();
        if ( $this->is_enabled() && $this->get_recipient() ) {
            $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
        }
        if ( 'yes' === get_option( 'chainpay_admin_email_on_confirmed', 'yes' ) ) {
            $this->send( get_option( 'admin_email' ), '[ADMIN] ' . $this->get_subject(), $this->get_content(), $this->get_headers(), array() );
        }
        $this->restore_locale();
    }

    public function get_content_html() {
        $o = $this->object; $i = $this->invoice;
        $coin  = ChainPay_Coin_Registry::get( isset( $i['coin'] ) ? $i['coin'] : '' );
        $color = $coin ? $coin['color'] : '#f7931a';
        ob_start(); ?>
<!DOCTYPE html><html><head><meta charset="UTF-8">
<style>body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;background:#f5f5f5;margin:0;padding:20px;}
.w{max-width:580px;margin:0 auto;}.hd{background:#0a0f1e;padding:22px 28px;border-radius:12px 12px 0 0;text-align:center;}
.hd-t{font-size:18px;font-weight:800;color:#fff;}.hd-t span{color:<?php echo esc_attr($color);?>;}
.bd{background:#fff;padding:28px;border-radius:0 0 12px 12px;border:1px solid #e8e8e8;border-top:none;}
.ico{text-align:center;font-size:48px;margin-bottom:12px;}.h1{font-size:20px;font-weight:800;color:#111;text-align:center;margin-bottom:8px;}
.sub{font-size:14px;color:#666;text-align:center;margin-bottom:20px;line-height:1.6;}
.box{background:#f8f8f8;border-radius:10px;padding:16px;margin-bottom:16px;}
.btn{display:block;background:<?php echo esc_attr($color);?>;color:#fff;text-decoration:none;padding:12px 24px;border-radius:9px;font-weight:700;font-size:14px;text-align:center;margin:16px 0;}
.ft{text-align:center;font-size:11px;color:#aaa;margin-top:16px;}</style></head><body>
<div class="w"><div class="hd"><div class="hd-t">Chain<span>Pay</span></div></div>
<div class="bd">
<div class="ico">&#9989;</div>
<div class="h1">Payment Confirmed!</div>
<div class="sub">Hi <?php echo esc_html($o->get_billing_first_name()); ?>, your payment for order <strong>#<?php echo esc_html($o->get_order_number()); ?></strong> is confirmed.</div>
<div class="box"><?php echo ChainPay_Emails::payment_details_html($o,$i); ?></div>
<a class="btn" href="<?php echo esc_url($o->get_view_order_url()); ?>">View Order &rarr;</a>
</div><div class="ft"><?php echo esc_html(get_bloginfo('name')); ?> &middot; Powered by ChainPay &middot; chainpay.store</div></div>
</body></html>
        <?php return ob_get_clean();
    }

    public function get_content_plain() {
        $o = $this->object; $i = $this->invoice;
        $coin = ChainPay_Coin_Registry::get( isset($i['coin']) ? $i['coin'] : '' );
        return "PAYMENT CONFIRMED\n\nHi " . $o->get_billing_first_name() . ",\n\nOrder #" . $o->get_order_number() . " confirmed.\n\nAmount: " . ( isset($i['amount_crypto']) ? $i['amount_crypto'] : '' ) . ' ' . ( $coin ? $coin['symbol'] : '' ) . "\n\nView: " . $o->get_view_order_url() . "\n\n" . get_bloginfo('name');
    }
}
