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

/**
 * Avoid stale dashboard/create pages after actions like delete/update.
 * This runs early on template_redirect so cache plugins/CDNs can bypass cache.
 */
add_action('template_redirect', function () {
    if (!is_user_logged_in() || !is_singular()) {
        return;
    }

    $post = get_queried_object();
    if (!$post || empty($post->post_content)) {
        return;
    }

    $content = (string) $post->post_content;
    $has_vendor_ui = has_shortcode($content, 'stakmv-vendor-dashboard')
        || has_shortcode($content, 'stakmv-vendor-create')
        || has_shortcode($content, 'stakmv-apply-vendor');
    if (!$has_vendor_ui) {
        return;
    }

    if (!defined('DONOTCACHEPAGE')) {
        define('DONOTCACHEPAGE', true);
    }
    if (!defined('DONOTCACHEOBJECT')) {
        define('DONOTCACHEOBJECT', true);
    }
    if (!defined('DONOTCACHEDB')) {
        define('DONOTCACHEDB', true);
    }

    nocache_headers();
}, 1);

$stakmv_vendor_dashboard_shortcode = function () {
    if (!is_user_logged_in()) {
        return '<p>You must be logged in.</p>';
    }

    ob_start();
    include STAKMV_PLUGIN_PATH . 'includes/vendor/dashboard.php';
    return ob_get_clean();
};

add_shortcode('stakmv-vendor-dashboard', $stakmv_vendor_dashboard_shortcode);

$stakmv_vendor_create_shortcode = function () {
    if (!is_user_logged_in() || !stakmv_is_vendor()) {
        return '<p>You must be logged in as a vendor.</p>';
    }

    ob_start();
    include STAKMV_PLUGIN_PATH . 'includes/vendor/create-listing.php';
    return ob_get_clean();
};

add_shortcode('stakmv-vendor-create', $stakmv_vendor_create_shortcode);

$stakmv_apply_vendor_shortcode = function () {
    if (!is_user_logged_in()) {
        return '<p>You must be logged in to apply as a vendor.</p>';
    }

    $user_id = get_current_user_id();
    $user    = wp_get_current_user();

    if (in_array('stakmv_vendor', (array) $user->roles, true)) {
        return '<p>You are already a vendor.</p>';
    }

    $application = get_user_meta($user_id, '_stakmv_vendor_application', true);

    if (!empty($application)) {
        if ($application['status'] === 'pending') {
            return '<p>Your vendor application is pending approval.</p>';
        }

        if ($application['status'] === 'rejected') {
            ob_start();
            ?>
            <p>Your vendor application was rejected.</p>
            <form method="post" class="wcmv-mt-10">
                <?php wp_nonce_field('stakmv_apply_vendor_action', 'stakmv_apply_vendor_nonce'); ?>
                <button type="submit" name="stakmv_apply_vendor" class="button">
                    Re-apply as vendor
                </button>
            </form>
            <?php
            return ob_get_clean();
        }
    }

    ob_start();
    ?>
    <form method="post">
        <?php wp_nonce_field('stakmv_apply_vendor_action', 'stakmv_apply_vendor_nonce'); ?>
        <button type="submit" name="stakmv_apply_vendor" class="button button-primary">
            Apply to become a vendor
        </button>
    </form>
    <?php
    return ob_get_clean();
};

add_shortcode('stakmv-apply-vendor', $stakmv_apply_vendor_shortcode);
