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

class ChainPay_Order_Meta {

    public function __construct() {
        add_action( 'add_meta_boxes', array( $this, 'add' ) );
    }

    public function add() {
        $screens = wc_get_order_types( 'order-meta-boxes' );
        if ( empty( $screens ) ) $screens = array( 'shop_order' );
        foreach ( $screens as $screen ) {
            add_meta_box( 'chainpay-order-meta', 'ChainPay Payment', array( $this, 'render' ), $screen, 'side', 'high' );
        }
    }

    public function render( $post_or_order ) {
        $order     = ( $post_or_order instanceof WC_Order ) ? $post_or_order : wc_get_order( $post_or_order->ID );
        if ( ! $order ) return;
        if ( 'chainpay' !== $order->get_payment_method() ) { echo '<p style="color:#aaa;font-size:12px;">Not a ChainPay order.</p>'; return; }

        $invoice   = ChainPay_Invoice::get_by_order( $order->get_id() );
        $coin_key  = $order->get_meta( '_chainpay_coin' );
        $coin      = $coin_key ? ChainPay_Coin_Registry::get( $coin_key ) : null;
        $status    = $invoice ? $invoice['status'] : $order->get_meta( '_chainpay_status' );
        $address   = $invoice ? $invoice['address']       : $order->get_meta( '_chainpay_address' );
        $crypto    = $invoice ? $invoice['amount_crypto'] : $order->get_meta( '_chainpay_amount_crypto' );
        $tx        = $invoice ? $invoice['tx_hash']       : $order->get_meta( '_chainpay_tx_hash' );
        $confs     = $invoice ? $invoice['confirmations'] : $order->get_meta( '_chainpay_confirmations' );
        $expires   = $invoice ? $invoice['expires_at']    : $order->get_meta( '_chainpay_expires_at' );
        $inv_id    = $invoice ? $invoice['invoice_id']    : $order->get_meta( '_chainpay_invoice_id' );

        $color     = $coin ? $coin['color'] : '#f7931a';
        $symbol    = $coin ? $coin['symbol'] : $coin_key;
        $name      = $coin ? $coin['name']   : $coin_key;
        $network   = $coin ? $coin['network'] : '';

        $status_colors = array(
            'confirmed' => '#059669', 'pending' => '#f59e0b', 'detected'   => '#2563eb',
            'expired'   => '#6b7280', 'underpaid'=> '#ef4444', 'overpaid'  => '#8b5cf6',
        );
        $badge_color = isset( $status_colors[ $status ] ) ? $status_colors[ $status ] : '#aaa';
        ?>
        <div style="font-size:12px;line-height:1.7;">
            <div style="display:flex;align-items:center;gap:8px;margin-bottom:10px;">
                <div style="width:28px;height:28px;border-radius:50%;background:<?php echo esc_attr($color);?>;color:#fff;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:900;flex-shrink:0;">
                    <?php echo esc_html( $coin ? $coin['icon'] : '⚡' ); ?>
                </div>
                <div>
                    <div style="font-weight:700;font-size:13px;"><?php echo esc_html( $name ); ?></div>
                    <div style="color:#aaa;font-size:10px;"><?php echo esc_html( $network ); ?></div>
                </div>
                <div style="margin-left:auto;">
                    <span style="background:<?php echo esc_attr($badge_color);?>;color:#fff;border-radius:4px;padding:2px 8px;font-size:10px;font-weight:700;text-transform:uppercase;">
                        <?php echo esc_html( $status ?: 'N/A' ); ?>
                    </span>
                </div>
            </div>

            <?php
            $rows = array(
                'Amount'   => esc_html( $crypto . ' ' . $symbol ),
                'Address'  => '<code style="font-size:10px;word-break:break-all;">' . esc_html( $address ) . '</code>',
                'TX Hash'  => $tx ? '<code style="font-size:10px;word-break:break-all;">' . esc_html( $tx ) . '</code>' : '<span style="color:#aaa;">—</span>',
                'Confirms' => esc_html( (string) ( $confs ?: 0 ) ),
                'Expires'  => $expires ? esc_html( wp_date( 'M j, g:i a', strtotime( $expires ) ) ) : '<span style="color:#aaa;">—</span>',
                'Inv ID'   => '<code style="font-size:9px;">' . esc_html( $inv_id ) . '</code>',
            );
            foreach ( $rows as $lbl => $val ) :
                ?>
                <div style="display:flex;gap:8px;padding:4px 0;border-bottom:1px solid #f0f0f0;">
                    <span style="color:#aaa;width:60px;flex-shrink:0;"><?php echo esc_html( $lbl ); ?></span>
                    <span><?php echo $val; ?></span>
                </div>
                <?php endforeach; ?>

            <div style="margin-top:10px;display:flex;gap:6px;flex-wrap:wrap;">
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=chainpay&tab=invoices' ) ); ?>"
                   style="font-size:11px;color:#2563eb;text-decoration:none;">View Invoices</a>
            </div>
        </div>
        <?php
    }
}
