<?php
if (!defined('ABSPATH')) {
    exit;
}

add_action('stakmv_auctions_settings_tabs', function ($current_tab) {
    printf(
        '<a href="?page=stakmv-auctions&tab=api" class="nav-tab %s">API</a>',
        $current_tab === 'api' ? 'nav-tab-active' : ''
    );
});

add_action('stakmv_auctions_render_tab_api', function () {
    if (isset($_POST['save_api'])) {
        check_admin_referer('stakmv_save_api', 'stakmv_save_api_nonce');

        stakmv_update_option('api_enabled', isset($_POST['stakmv_api_enabled']) ? 'yes' : 'no');
        stakmv_update_option('api_group_auth', isset($_POST['stakmv_api_group_auth']) ? 'yes' : 'no');
        stakmv_update_option('api_group_meta', isset($_POST['stakmv_api_group_meta']) ? 'yes' : 'no');
        stakmv_update_option('api_group_vendor', isset($_POST['stakmv_api_group_vendor']) ? 'yes' : 'no');
        stakmv_update_option('api_group_listings', isset($_POST['stakmv_api_group_listings']) ? 'yes' : 'no');
        stakmv_update_option('api_group_auctions', isset($_POST['stakmv_api_group_auctions']) ? 'yes' : 'no');
        stakmv_update_option('api_group_shipping', isset($_POST['stakmv_api_group_shipping']) ? 'yes' : 'no');
        stakmv_update_option('api_group_notifications', isset($_POST['stakmv_api_group_notifications']) ? 'yes' : 'no');
        stakmv_update_option('api_group_pro', isset($_POST['stakmv_api_group_pro']) ? 'yes' : 'no');

        echo '<div class="updated"><p>API settings saved.</p></div>';
    }

    $api_enabled             = (string) stakmv_get_option('api_enabled', 'yes');
    $api_group_auth          = (string) stakmv_get_option('api_group_auth', 'yes');
    $api_group_meta          = (string) stakmv_get_option('api_group_meta', 'yes');
    $api_group_vendor        = (string) stakmv_get_option('api_group_vendor', 'yes');
    $api_group_listings      = (string) stakmv_get_option('api_group_listings', 'yes');
    $api_group_auctions      = (string) stakmv_get_option('api_group_auctions', 'yes');
    $api_group_shipping      = (string) stakmv_get_option('api_group_shipping', 'yes');
    $api_group_notifications = (string) stakmv_get_option('api_group_notifications', 'yes');
    $api_group_pro           = (string) stakmv_get_option('api_group_pro', 'yes');

    $api_status_rows = array(
        'Master API'     => ($api_enabled === 'yes' ? 'ON' : 'OFF'),
        'Auth'           => ($api_group_auth === 'yes' ? 'ON' : 'OFF'),
        'Meta'           => ($api_group_meta === 'yes' ? 'ON' : 'OFF'),
        'Vendor'         => ($api_group_vendor === 'yes' ? 'ON' : 'OFF'),
        'Listings'       => ($api_group_listings === 'yes' ? 'ON' : 'OFF'),
        'Auctions'       => ($api_group_auctions === 'yes' ? 'ON' : 'OFF'),
        'Shipping'       => ($api_group_shipping === 'yes' ? 'ON' : 'OFF'),
        'Notifications'  => ($api_group_notifications === 'yes' ? 'ON' : 'OFF'),
        'Pro'            => ($api_group_pro === 'yes' ? 'ON' : 'OFF'),
    );
    $api_base_url = esc_url_raw(rest_url('stakmv/v1'));
    ?>

    <form method="post">
        <?php wp_nonce_field('stakmv_save_api', 'stakmv_save_api_nonce'); ?>
        <table class="form-table">
            <tr>
                <th scope="row">Enable App API</th>
                <td>
                    <label>
                        <input type="checkbox" name="stakmv_api_enabled" <?php checked($api_enabled, 'yes'); ?>>
                        Enable REST API endpoints (`stakmv/v1`).
                    </label>
                </td>
            </tr>

            <tr>
                <th scope="row">App API Endpoint Groups</th>
                <td>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_auth" <?php checked($api_group_auth, 'yes'); ?>>
                        Auth endpoints
                    </label>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_meta" <?php checked($api_group_meta, 'yes'); ?>>
                        Meta endpoints
                    </label>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_vendor" <?php checked($api_group_vendor, 'yes'); ?>>
                        Vendor profile/listing endpoints
                    </label>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_listings" <?php checked($api_group_listings, 'yes'); ?>>
                        Public listings endpoints
                    </label>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_auctions" <?php checked($api_group_auctions, 'yes'); ?>>
                        Auction actions endpoints
                    </label>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_shipping" <?php checked($api_group_shipping, 'yes'); ?>>
                        Shipping endpoints
                    </label>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_notifications" <?php checked($api_group_notifications, 'yes'); ?>>
                        Notifications endpoints
                    </label>
                    <label style="display:block;margin:0 0 6px;">
                        <input type="checkbox" name="stakmv_api_group_pro" <?php checked($api_group_pro, 'yes'); ?>>
                        Pro endpoints
                    </label>
                    <p class="description">Disabled groups return REST forbidden errors and are unavailable to app clients.</p>
                </td>
            </tr>

            <tr>
                <th scope="row">API Status</th>
                <td>
                    <p style="margin:0 0 8px;">
                        <strong>Base URL:</strong>
                        <code><?php echo esc_html($api_base_url); ?></code>
                    </p>
                    <table class="widefat striped" style="max-width:540px;">
                        <thead>
                            <tr>
                                <th>Group</th>
                                <th style="width:90px;">Status</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($api_status_rows as $label => $status) : ?>
                            <tr>
                                <td><?php echo esc_html($label); ?></td>
                                <td>
                                    <strong style="color:<?php echo $status === 'ON' ? '#1e8c3a' : '#b32d2e'; ?>;">
                                        <?php echo esc_html($status); ?>
                                    </strong>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                    <p class="description" style="margin-top:8px;">Save changes to apply endpoint visibility/access updates.</p>
                </td>
            </tr>
        </table>
        <p><button class="button button-primary" name="save_api" value="1">Save Changes</button></p>
    </form>
    <?php
});
