<?php

namespace Unlockafe_Addons\Admin;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

// Load WP_List_Table if not loaded
if (!class_exists('WP_List_Table')) {
    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

/**
 * Class Store_Credit_List_Table
 *
 * Handles the display of store credit and gift card data in a list table.
 */
class Store_Credit_List_Table extends \WP_List_Table {

    private $store_credit_manager;

    /**
     * Store_Credit_List_Table constructor.
     */
    public function __construct($store_credit_manager) {
        $this->store_credit_manager = $store_credit_manager;

        parent::__construct([
            'singular' => __('Gift Card', 'unlock-addons-for-elementor'),
            'plural'   => __('Gift Cards', 'unlock-addons-for-elementor'),
            'ajax'     => false,
        ]);
    }

    /**
     * Get the list of columns.
     *
     * @return array
     */
    public function get_columns() {
        return [
            'cb'              => '<input type="checkbox" />',
            'code'            => __('Code', 'unlock-addons-for-elementor'),
            'balance'         => __('Balance', 'unlock-addons-for-elementor'),
            'initial_balance' => __('Initial Balance', 'unlock-addons-for-elementor'),
            'recipient_email' => __('Recipient', 'unlock-addons-for-elementor'),
            'order_id'        => __('Order ID', 'unlock-addons-for-elementor'),
            'created_at'      => __('Created', 'unlock-addons-for-elementor'),
            'expires_at'      => __('Expires', 'unlock-addons-for-elementor'),
            'status'          => __('Status', 'unlock-addons-for-elementor'),
        ];
    }

    /**
     * Prepare the items for the table to process.
     */
    public function prepare_items() {
        $columns = $this->get_columns();
        $hidden = [];
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = [$columns, $hidden, $sortable];

        // Fetch the data
        $this->items = $this->store_credit_manager->get_all_gift_cards();
    }

    /**
     * Define which columns are sortable
     *
     * @return array
     */
    protected function get_sortable_columns() {
        return [
            'code'            => ['code', false],
            'balance'         => ['balance', false],
            'initial_balance' => ['initial_balance', false],
            'created_at'      => ['created_at', true], // Default sort
            'expires_at'      => ['expires_at', false],
            'status'          => ['status', false],
        ];
    }

    /**
     * Default column rendering.
     *
     * @param object $item
     * @param string $column_name
     * @return mixed
     */
    protected function column_default($item, $column_name) {
        return isset($item[$column_name]) ? esc_html($item[$column_name]) : '';
    }

    /**
     * Render the checkbox column.
     *
     * @param object $item
     * @return string
     */
    protected function column_cb($item) {
        return sprintf('<input type="checkbox" name="gift_card[]" value="%s" />', $item['id']);
    }

    /**
     * Render the balance column with currency symbol.
     *
     * @param array $item
     * @return string
     */
    protected function column_balance($item) {
        return wc_price($item['balance']);
    }

    /**
     * Render the initial balance column with currency symbol.
     *
     * @param array $item
     * @return string
     */
    protected function column_initial_balance($item) {
        return wc_price($item['initial_balance']);
    }

    /**
     * Render the code column with actions.
     *
     * @param array $item
     * @return string
     */
    protected function column_code($item) {
        $delete_nonce = wp_create_nonce('unlockafe_delete_gift_card_' . $item['id']);
        $delete_url = admin_url('admin.php?page=unlock-gift-cards&action=delete&gift_card=' . $item['id'] . '&_wpnonce=' . $delete_nonce);

        $actions = [
            'delete' => sprintf(
                '<a href="%s" style="color:red;" onclick="return confirm(\'%s\')">%s</a>',
                esc_url($delete_url),
                esc_js(__('Are you sure you want to delete this gift card?', 'unlock-addons-for-elementor')),
                __('Delete', 'unlock-addons-for-elementor')
            ),
        ];
        return sprintf('<strong>%1$s</strong>%2$s', esc_html($item['code']), $this->row_actions($actions));
    }
}
