<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:ignoreFile

// Handle form submission
if (isset($_POST['submit_settings']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['settings_nonce'] ?? '')), 'anbarak_settings_action')) {
    // Save checkbox values using anbarak_update_option
    anbarak_update_option('allow_edit_stock', isset($_POST['allow_edit_stock']) ? 1 : 0);
    anbarak_update_option('country_selector_mode', isset($_POST['country_selector_mode']) ? 'dropdown' : 'default');
    anbarak_update_option('inventory_notifications_enabled', isset($_POST['inventory_notifications_enabled']) ? 1 : 0);


    // Show success message
    echo '<div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded">' .
        esc_html__('Settings saved successfully!', 'anbarak') .
        '</div>';
}

// Handle clear transients action
if (
    isset($_GET['action']) && sanitize_text_field(wp_unslash($_GET['action'])) === 'anbarak_clear_transients' &&
    isset($_GET['anbarak_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['anbarak_nonce'])), 'clear_transients')
) {

    anbarak_clear_transients();

    // Redirect to remove action from URL
    $redirect_url = remove_query_arg(array('action', 'anbarak_nonce'));
    wp_safe_redirect($redirect_url);
    exit;
}

$transients_stats = anbarak_get_transients_stats();
?>

<div>
    <header class="flex justify-between items-center mb-6">
        <h2 class="text-xl font-semibold text-gray-800 dark:text-white"><?php esc_html_e('General Settings', 'anbarak') ?></h2>
    </header>

    <form method="post" action="">
        <?php wp_nonce_field('anbarak_settings_action', 'settings_nonce'); ?>

        <div class="box overflow-hidden">

            <!-- Option 1: Clear Transients -->
            <div class="p-6 border-b border-gray-200">
                <div class="flex items-center justify-between">
                    <div>
                        <h3 class="text-md font-medium text-gray-900 dark:text-white mb-1"><?php esc_html_e('Clear Transients', 'anbarak') ?> (<?php echo esc_html($transients_stats['count']) ?>)</h3>
                        <p class="text-sm text-gray-500 dark:text-gray-300"><?php esc_html_e('Remove all temporary cached data from the database', 'anbarak') ?></p>
                    </div>
                    <button type="button"
                        onclick="if(confirm('<?php esc_html_e('Are you sure you want to clear all transients?', 'anbarak') ?>')) { window.location.href='<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=anbarak&tab=settings&action=anbarak_clear_transients'), 'clear_transients', 'anbarak_nonce')); ?>'; }"
                        class="button button--danger-outline">
                        <?php esc_html_e('Clear Transients', 'anbarak') ?>
                    </button>
                </div>
            </div>

            <div class="p-6 border-b border-gray-200">
                <label class="form-checkbox">
                    <input type="checkbox" name="inventory_notifications_enabled" value="1" <?php checked(anbarak_get_option('inventory_notifications_enabled', 1), 1) ?>>
                    <i class="form-icon"></i>
                    <div class="ms-3">
                        <span class="text-md font-medium text-gray-900 dark:text-white"><?php esc_html_e('Demand stock notifications', 'anbarak') ?></span>
                        <p class="text-sm text-gray-500 dark:text-gray-300 mt-1"><?php esc_html_e('Show notifications for high-demand products that are low or out of stock. Checks run every 12 hours.', 'anbarak') ?></p>
                    </div>
                </label>
            </div>

            <div class="p-6 border-b border-gray-200">
                <label class="form-checkbox">
                    <input type="checkbox"
                        name="country_selector_mode"
                        value="1"
                        <?php checked(anbarak_get_option('country_selector_mode', 'default'), 'dropdown') ?>>
                    <i class="form-icon"></i>
                    <div class="ms-3">
                        <span class="text-md font-medium text-gray-900 dark:text-white"><?php esc_html_e('Show country selector in order forms', 'anbarak') ?></span>
                        <p class="text-sm text-gray-500 dark:text-gray-300 mt-1"><?php esc_html_e('Enable a country dropdown in order forms. When disabled, the WooCommerce base country is used automatically.', 'anbarak') ?></p>
                    </div>
                </label>
            </div>

            <div class="p-5 -m-5 mt-5 bg-gray-50 border-t border-gray-200 dark:bg-[#1a1a1a] dark:border-[#2a2a2a]">
                <button type="submit"
                    name="submit_settings"
                    class="button button--primary">
                    <?php esc_html_e('Save Settings', 'anbarak') ?>
                </button>
            </div>
        </div>
    </form>
</div>
