<?php
namespace PayCal\Modules\Bit;

defined('ABSPATH') || exit;

use PayCal\Licensing\Gate;

class BitCheckoutUI {
  public static function init(): void {
    add_action('woocommerce_thankyou', [__CLASS__, 'render_thankyou'], 20);
    add_action('woocommerce_before_pay_action', [__CLASS__, 'render_pay_for_order'], 20);
    add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_front_assets']);
  }

  public static function enqueue_front_assets(): void {
    if (!function_exists('is_checkout')) return;
    // Keep it simple for MVP
    wp_enqueue_style('paycal-front', PAYCAL_URL . 'assets/front.css', [], PAYCAL_VERSION);
    wp_enqueue_script('paycal-front', PAYCAL_URL . 'assets/front.js', [], PAYCAL_VERSION, true);
    wp_localize_script('paycal-front', 'PAYCAL_FRONT', [
      'canManageOptions' => current_user_can('manage_options'),
    ]);
  }

  public static function render_thankyou($order_id): void {
    if (!$order_id) return;
    $order = wc_get_order($order_id);
    if (!$order) return;
    self::render_if_bit($order);
  }

  public static function render_pay_for_order(): void {
    // phpcs:disable WordPress.Security.NonceVerification.Recommended -- WooCommerce pay-for-order request is read-only here and validated by WooCommerce order key.
    $order_id = isset($_GET['pay_for_order']) ? absint(wp_unslash($_GET['pay_for_order'])) : 0;
    if (!$order_id && !empty($_GET['key'])) {
      $order_id = wc_get_order_id_by_order_key(sanitize_text_field(wp_unslash($_GET['key'])));
    }
    // phpcs:enable WordPress.Security.NonceVerification.Recommended
    if (!$order_id) return;

    $order = wc_get_order($order_id);
    if (!$order) return;

    self::render_if_bit($order);
  }

  private static function render_if_bit(\WC_Order $order): void {
    if ($order->get_status() !== 'paycal-bit-pending') return;
    if (!Gate::has('bit')) return;

    $phone = BitSettings::phone();
    if (!$phone) return;

    $s = BitSettings::get();
    $amount = $order->get_total();
    $order_num = $order->get_order_number();
    $note = (string)($s['note'] ?? '');
    $display = (string)($s['display_name'] ?? '');

    $payload = 'BIT:' . $phone . '|AMOUNT:' . $amount . '|ORDER:' . $order_num;

    echo '<section class="paycal-paybox">';
    echo '<div class="paycal-paybox__header">';
    echo '<div class="paycal-paybox__title">תשלום בביט</div>';
    echo '<div class="paycal-paybox__subtitle">הזמנה #' . esc_html($order_num) . ' · סכום לתשלום: <strong>' . wp_kses_post(wc_price($amount)) . '</strong></div>';
    echo '</div>';

    echo '<div class="paycal-paybox__grid">';

    echo '<div class="paycal-paybox__left">';
    if ($display) {
      echo '<div class="paycal-row"><span class="paycal-label">מקבל התשלום:</span> <span class="paycal-value">'.esc_html($display).'</span></div>';
    }
    echo '<div class="paycal-row"><span class="paycal-label">מספר ביט:</span> <span class="paycal-value" id="paycalBitPhone">'.esc_html($phone).'</span></div>';

    echo '<div class="paycal-actions">';
    echo '<button type="button" class="paycal-btn" data-copy="#paycalBitPhone">העתק מספר</button>';
    echo '<a class="paycal-btn paycal-btn--primary" href="tel:' . esc_attr($phone) . '">פתח במובייל</a>';
    echo '</div>';

    if ($note) {
      echo '<div class="paycal-note">'.esc_html($note).'</div>';
    }

    // Timeline
    echo '<div class="paycal-timeline">';
    echo '<div class="paycal-timeline__item"><span class="paycal-badge">1</span><span>שלם בביט</span></div>';
    echo '<div class="paycal-timeline__item"><span class="paycal-badge">2</span><span>אנחנו מאשרים ידנית</span></div>';
    echo '<div class="paycal-timeline__item"><span class="paycal-badge">3</span><span>נשלח עדכון במייל</span></div>';
    echo '</div>';

    echo '<div class="paycal-tip">תודה רבה 🙏 אחרי ביצוע התשלום, אנחנו נאשר את ההזמנה ידנית.</div>';
    echo '</div>'; // left

    echo '<div class="paycal-paybox__right">';
    echo '<div class="paycal-qrwrap" data-qr="' . esc_attr($payload) . '"></div>';
    echo '<div class="paycal-muted">סרוק את ה-QR לביצוע התשלום</div>';
    echo '</div>'; // right

    echo '</div>'; // grid
    echo '</section>';
  }
}
