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

/**
 * Vendor public profile: rewrite rule, query var, template loader, and helper.
 *
 * Place this file in: mv-auctions/includes/public/vendor-profile.php
 *
 * Note: The plugin's main file must require or include this file so the hooks run.
 */

/** Register rewrite rule for /vendor/<slug>/ */
add_action('init', function () {
    add_rewrite_rule(
        '^vendor/([^/]+)/?$',
        'index.php?stakmv_vendor=$matches[1]',
        'top'
    );
}, 10);

/** Add query var */
add_filter('query_vars', function ($vars) {
    $vars[] = 'stakmv_vendor';
    return $vars;
});

/** Vendor profile document title */
add_filter('pre_get_document_title', function ($title) {
    $vendor_slug = get_query_var('stakmv_vendor');
    if (!$vendor_slug) {
        return $title;
    }

    $vendor_user = get_user_by('slug', $vendor_slug);
    if (!$vendor_user) {
        return $title;
    }

    $vendor_name = function_exists('stakmv_get_vendor_display_name')
        ? stakmv_get_vendor_display_name((int) $vendor_user->ID)
        : (string) $vendor_user->display_name;

    return sprintf('%s Profile', $vendor_name);
}, 20);

/** Template loader */
add_action('template_redirect', function () {
    // Only act on frontend main queries
    if ( is_admin() || is_feed() || ! is_main_query() ) {
        return;
    }

    $vendor_slug = get_query_var('stakmv_vendor');
    if ( ! $vendor_slug ) {
        return;
    }

    $stakmv_vendor_user = get_user_by('slug', $vendor_slug);
    if ( ! $stakmv_vendor_user ) {
        status_header(404);
        nocache_headers();
        // Friendly output using theme header/footer if available
        if ( function_exists('get_header') ) {
            get_header();
            echo '<div class="vintage-card"><p>Vendor not found.</p></div>';
            get_footer();
        } else {
            echo '<h1>Vendor not found</h1>';
        }
        exit;
    }

    // Public profile is vendor-only.
    if ( ! function_exists( 'stakmv_is_vendor' ) || ! stakmv_is_vendor( (int) $stakmv_vendor_user->ID ) ) {
        status_header(404);
        nocache_headers();
        if ( function_exists('get_header') ) {
            get_header();
            echo '<div class="vintage-card"><p>Vendor not found.</p></div>';
            get_footer();
        } else {
            echo '<h1>Vendor not found</h1>';
        }
        exit;
    }

    // Always use plugin template for consistent layout/behavior.
    $plugin_root     = dirname( dirname( dirname( __FILE__ ) ) ); // mv-auctions/
    $plugin_template = $plugin_root . '/templates/vendor-profile.php';

    if ( file_exists( $plugin_template ) ) {
        set_query_var( 'stakmv_vendor_obj', $stakmv_vendor_user );
        include $plugin_template;
        exit;
    }

    // If no template found, show a clear error
    wp_die( 'Vendor profile template missing.', '', array( 'response' => 500 ) );
});

/** Helper: vendor profile URL used by core and extensions. */
if (!function_exists('stakmv_get_vendor_profile_url')) {
    function stakmv_get_vendor_profile_url($user_id) {
        $user = get_userdata($user_id);
        if (!$user) {
            return home_url('/');
        }
        $nicename = $user->user_nicename ?: sanitize_title($user->display_name);
        return home_url('/vendor/' . $nicename . '/');
    }
}
