<?php
namespace PayCal\Admin;

defined('ABSPATH') || exit;

class Page_Invoices {
  private static function can_access(): bool {
    return current_user_can('manage_woocommerce')
      || current_user_can('edit_shop_orders')
      || current_user_can('manage_options');
  }

  public static function render(): void {
    if (!self::can_access()) {
      wp_die(
        esc_html__('You do not have permission to view PayCal invoices.', 'klamra-paycal-for-aspaclaria'),
        esc_html__('Permission denied', 'klamra-paycal-for-aspaclaria'),
        ['response' => 403]
      );
    }

    $settings = function_exists('paycal_invoice_get_settings') ? paycal_invoice_get_settings() : [];
    $enabled = !isset($settings['enabled']) || (string) $settings['enabled'] === 'yes';

    $invoice_ids = get_posts([
      'post_type' => 'asp_invoice',
      'post_status' => 'publish',
      'fields' => 'ids',
      'posts_per_page' => 50,
      'orderby' => 'date',
      'order' => 'DESC',
    ]);
    if (!is_array($invoice_ids)) {
      $invoice_ids = [];
    }

    echo '<div class="wrap paycal-wrap">';
    echo '<div class="paycal-header"><h1>PayCal Invoices</h1><p>Invoice administration for WooCommerce orders.</p></div>';

    if (!$enabled) {
      echo '<div class="notice notice-warning"><p>' . esc_html__('Invoice module is currently disabled. Enable it in PayCal settings.', 'klamra-paycal-for-aspaclaria') . '</p></div>';
    }

    echo '<div class="paycal-card">';
    echo '<p><strong>Module status:</strong> ' . esc_html($enabled ? 'enabled' : 'disabled by settings') . '</p>';
    echo '<p><a class="button button-primary" href="' . esc_url(admin_url('admin.php?page=paycal-invoices-settings')) . '">' . esc_html__('Open invoice settings', 'klamra-paycal-for-aspaclaria') . '</a></p>';
    echo '</div>';

    echo '<div class="paycal-card">';
    echo '<h2>' . esc_html__('Invoices', 'klamra-paycal-for-aspaclaria') . '</h2>';

    if (empty($invoice_ids)) {
      echo '<p>' . esc_html__('No invoices found yet. Invoices will appear here after paid WooCommerce orders are created.', 'klamra-paycal-for-aspaclaria') . '</p>';
      echo '</div></div>';
      return;
    }

    echo '<table class="widefat striped"><thead><tr>';
    echo '<th>' . esc_html__('Invoice', 'klamra-paycal-for-aspaclaria') . '</th>';
    echo '<th>' . esc_html__('Order', 'klamra-paycal-for-aspaclaria') . '</th>';
    echo '<th>' . esc_html__('Customer', 'klamra-paycal-for-aspaclaria') . '</th>';
    echo '<th>' . esc_html__('Date', 'klamra-paycal-for-aspaclaria') . '</th>';
    echo '<th>' . esc_html__('Download', 'klamra-paycal-for-aspaclaria') . '</th>';
    echo '</tr></thead><tbody>';

    foreach ($invoice_ids as $invoice_id) {
      $invoice_id = absint($invoice_id);
      $order_id = absint(get_post_meta($invoice_id, '_order_id', true));
      $order = $order_id && function_exists('wc_get_order') ? wc_get_order($order_id) : null;
      $order_edit_url = $order && method_exists($order, 'get_edit_order_url') ? $order->get_edit_order_url() : admin_url('post.php?post=' . $order_id . '&action=edit');
      $download_url = wp_nonce_url(
        admin_url('admin-post.php?action=paycal_invoice_download&invoice_id=' . absint($invoice_id)),
        'paycal_invoice_download_' . absint($invoice_id)
      );

      echo '<tr>';
      echo '<td><a href="' . esc_url(get_edit_post_link($invoice_id)) . '">' . esc_html(get_the_title($invoice_id)) . '</a></td>';
      echo '<td>' . ($order_id ? '<a href="' . esc_url($order_edit_url) . '">#' . esc_html((string) $order_id) . '</a>' : esc_html__('Unavailable', 'klamra-paycal-for-aspaclaria')) . '</td>';
      echo '<td>' . esc_html($order ? trim($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()) : '') . '</td>';
      echo '<td>' . esc_html(get_the_date('', $invoice_id)) . '</td>';
      echo '<td><a class="button" href="' . esc_url($download_url) . '">' . esc_html__('Download HTML', 'klamra-paycal-for-aspaclaria') . '</a></td>';
      echo '</tr>';
    }

    echo '</tbody></table>';
    echo '</div></div>';
  }
}
