<?php

namespace Unlockafe_addons\Classes;

if (!defined('ABSPATH')) {
    exit();
}

class Store_Credit_Admin {

    public function __construct() {
        add_action('admin_init', [$this, 'process_gift_card_actions']);
        add_action('admin_notices', [$this, 'show_admin_notices']);
    }

    public function render_admin_page() {
        ?>
        <div class="wrap">
            <h1><?php _e('Gift Cards', 'unlock-addons-for-elementor'); ?></h1>
            <form method="post">
                <?php
                $list_table = new \Unlockafe_addons\Classes\Gift_Card_List_Table();
                $list_table->prepare_items();
                $list_table->display();
                ?>
            </form>
        </div>
        <?php
    }

    public function process_gift_card_actions() {
        $list_table = new \Unlockafe_addons\Classes\Gift_Card_List_Table();
        $action = $list_table->current_action();

        // Single delete action
        if ('delete' === $action) {
            if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'unlockafe_delete_gift_card')) {
                wp_die(__('Security check failed', 'unlock-addons-for-elementor'));
            }

            $gift_card_id = absint($_GET['gift_card']);
            global $wpdb;
            $table_name = $wpdb->prefix . 'unlockafe_store_credit';
            $wpdb->delete($table_name, ['id' => $gift_card_id], ['%d']);

            wp_redirect(admin_url('admin.php?page=unlock-gift-cards&deleted=1'));
            exit;
        }

        // Bulk delete action
        if ('bulk-delete' === $action) {
            if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'bulk-gift_cards')) {
                wp_die(__('Security check failed', 'unlock-addons-for-elementor'));
            }

            $gift_card_ids = array_map('absint', $_REQUEST['gift_card']);
            $deleted_count = 0;

            if (!empty($gift_card_ids)) {
                global $wpdb;
                $table_name = $wpdb->prefix . 'unlockafe_store_credit';
                foreach ($gift_card_ids as $id) {
                    if ($wpdb->delete($table_name, ['id' => $id], ['%d'])) {
                        $deleted_count++;
                    }
                }
            }

            wp_redirect(admin_url('admin.php?page=unlock-gift-cards&deleted=' . $deleted_count));
            exit;
        }
    }

    public function show_admin_notices() {
        if (isset($_GET['page']) && $_GET['page'] === 'unlock-gift-cards' && isset($_GET['deleted'])) {
            $deleted_count = absint($_GET['deleted']);
            if ($deleted_count > 0) {
                $message = sprintf(
                    _n(
                        '%s gift card deleted successfully.',
                        '%s gift cards deleted successfully.',
                        $deleted_count,
                        'unlock-addons-for-elementor'
                    ),
                    $deleted_count
                );
                ?>
                <div class="notice notice-success is-dismissible">
                    <p><?php echo esc_html($message); ?></p>
                </div>
                <?php
            }
        }
    }
}
