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

/* ── Detected ────────────────────────────────────────────────── */
class ChainPay_Email_Detected extends WC_Email {
    public $invoice = array();
    public function __construct() {
        $this->id='chainpay_detected';$this->title='ChainPay — Payment Detected';
        $this->description='Sent when payment is seen on-chain but unconfirmed.';
        $this->heading='Payment received — awaiting confirmation';
        $this->subject='[{site_name}] We received your payment — 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());
        $this->restore_locale();
    }
    public function get_content_html(){
        $o=$this->object;$i=$this->invoice;
        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:#3b82f6;}
.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;}
.alert{background:#fffbeb;border:1px solid #fde68a;border-radius:8px;padding:11px 14px;font-size:13px;color:#78350f;margin-bottom:14px;}
.box{background:#f8f8f8;border-radius:10px;padding:16px;margin-bottom:14px;}
.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">&#9203;</div>
<div class="h1">Payment Received!</div>
<div class="sub">Hi <?php echo esc_html($o->get_billing_first_name()); ?>, we detected your payment for order <strong>#<?php echo esc_html($o->get_order_number()); ?></strong>. Awaiting blockchain confirmation.</div>
<div class="alert"><strong>Please wait.</strong> Your payment is being confirmed by the network. We will email you once complete.</div>
<div class="box"><?php echo ChainPay_Emails::payment_details_html($o,$i); ?></div>
</div><div class="ft"><?php echo esc_html(get_bloginfo('name')); ?> &middot; chainpay.store</div></div>
</body></html>
        <?php return ob_get_clean();
    }
    public function get_content_plain(){$o=$this->object;$i=$this->invoice;return "PAYMENT DETECTED\n\nHi ".$o->get_billing_first_name().",\n\nYour payment for #".$o->get_order_number()." is awaiting confirmation.\n\n".get_bloginfo('name');}
}

/* ── Underpayment ────────────────────────────────────────────── */
class ChainPay_Email_Underpayment extends WC_Email {
    public $invoice=array();public $received='';
    public function __construct(){
        $this->id='chainpay_underpayment';$this->title='ChainPay — Underpayment';
        $this->description='Sent when customer pays less than required.';
        $this->heading='Action required: Underpayment detected';
        $this->subject='[{site_name}] Please complete your payment — 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(),$received=''){
        $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->received=$received;$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());
        $this->send(get_option('admin_email'),'[ADMIN] Underpayment — #'.$order->get_order_number(),$this->get_content(),$this->get_headers(),array());
        $this->restore_locale();
    }
    public function get_content_html(){
        $o=$this->object;$i=$this->invoice;$recv=$this->received;
        $coin=ChainPay_Coin_Registry::get(isset($i['coin'])?$i['coin']:'');
        $sym=$coin?$coin['symbol']:(isset($i['coin'])?$i['coin']:'');
        $exp=isset($i['amount_crypto'])?$i['amount_crypto']:'';
        $addr=isset($i['address'])?$i['address']:'';
        $short=number_format((float)$exp-(float)$recv,8,'.','' );
        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:#ef4444;}
.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:16px;line-height:1.6;}
.bal{background:#fff5f5;border:1px solid #fecaca;border-radius:10px;padding:16px;text-align:center;margin-bottom:14px;}
.bal-lbl{font-size:11px;color:#991b1b;text-transform:uppercase;margin-bottom:4px;}
.bal-amt{font-size:24px;font-weight:800;color:#dc2626;font-family:monospace;}
.addr{background:#f8f8f8;border:1px solid #e8e8e8;border-radius:8px;padding:10px 12px;font-family:monospace;font-size:12px;word-break:break-all;margin-bottom:14px;}
.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">&#9888;&#65039;</div>
<div class="h1">Underpayment Detected</div>
<div class="sub">Hi <?php echo esc_html($o->get_billing_first_name()); ?>, order <strong>#<?php echo esc_html($o->get_order_number()); ?></strong> is underpaid. Please send the remaining balance.</div>
<div class="bal"><div class="bal-lbl">Still required</div><div class="bal-amt"><?php echo esc_html($short); ?> <?php echo esc_html($sym); ?></div></div>
<p style="font-size:12px;color:#666;margin-bottom:6px;">Received: <?php echo esc_html($recv.' '.$sym); ?> | Expected: <?php echo esc_html($exp.' '.$sym); ?></p>
<p style="font-size:12px;color:#666;margin-bottom:6px;">Send remaining <?php echo esc_html($short.' '.$sym); ?> to:</p>
<div class="addr"><?php echo esc_html($addr); ?></div>
</div><div class="ft"><?php echo esc_html(get_bloginfo('name')); ?> &middot; chainpay.store</div></div>
</body></html>
        <?php return ob_get_clean();
    }
    public function get_content_plain(){$o=$this->object;$i=$this->invoice;return "UNDERPAYMENT\n\nHi ".$o->get_billing_first_name().",\n\nOrder #".$o->get_order_number()." is underpaid.\nExpected: ".(isset($i['amount_crypto'])?$i['amount_crypto']:'')." ".(isset($i['coin'])?$i['coin']:'')."\nReceived: ".$this->received."\nSend remaining to: ".(isset($i['address'])?$i['address']:'')."\n\n".get_bloginfo('name');}
}

/* ── Expired ─────────────────────────────────────────────────── */
class ChainPay_Email_Expired extends WC_Email {
    public $invoice=array();
    public function __construct(){
        $this->id='chainpay_expired';$this->title='ChainPay — Invoice Expired';
        $this->description='Sent when invoice expires without payment.';
        $this->heading='Your invoice has expired';
        $this->subject='[{site_name}] Invoice expired — Order #{order_number}';
        $this->customer_email=true;$this->enabled='no';
        $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());
        $this->restore_locale();
    }
    public function get_content_html(){
        $o=$this->object;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:#9ca3af;}
.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:16px;line-height:1.6;}
.btn{display:block;background:#0a0f1e;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">&#9201;</div>
<div class="h1">Invoice Expired</div>
<div class="sub">Hi <?php echo esc_html($o->get_billing_first_name()); ?>, your invoice for order <strong>#<?php echo esc_html($o->get_order_number()); ?></strong> has expired. No funds were captured.</div>
<a class="btn" href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>">Return to Shop &rarr;</a>
</div><div class="ft"><?php echo esc_html(get_bloginfo('name')); ?> &middot; chainpay.store</div></div>
</body></html>
        <?php return ob_get_clean();
    }
    public function get_content_plain(){$o=$this->object;return "INVOICE EXPIRED\n\nHi ".$o->get_billing_first_name().",\n\nInvoice for #".$o->get_order_number()." expired. Shop again: ".wc_get_page_permalink('shop')."\n\n".get_bloginfo('name');}
}
