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

/**
 * Handle vendor profile update (frontend)
 */
add_action('init', function () {
    $save_profile = filter_input(INPUT_POST, 'stakmv_save_profile', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    if (!$save_profile) {
        return;
    }

    $profile_nonce = filter_input(INPUT_POST, 'stakmv_profile_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    if (
        !$profile_nonce ||
        !wp_verify_nonce(
            sanitize_text_field((string) $profile_nonce),
            'stakmv_update_profile'
        )
    ) {
        wp_die('Security check failed');
    }

    if (!is_user_logged_in()) {
        wp_die('Not allowed');
    }

    $user_id = get_current_user_id();
    if (!current_user_can('edit_user', $user_id)) {
        wp_die('Not allowed');
    }

    $profile_input = isset($_POST) && is_array($_POST)
        ? map_deep(wp_unslash($_POST), 'sanitize_text_field')
        : array();

    // Store editable public profile fields in plugin-owned user meta.
    update_user_meta($user_id, 'stakmv_display_name', sanitize_text_field((string) ($profile_input['display_name'] ?? '')));
    update_user_meta($user_id, 'stakmv_website', esc_url_raw((string) ($profile_input['website'] ?? '')));
    update_user_meta($user_id, 'stakmv_bio', sanitize_textarea_field((string) ($profile_input['bio'] ?? '')));
    update_user_meta($user_id, 'stakmv_location', sanitize_text_field((string) ($profile_input['location'] ?? '')));
    update_user_meta($user_id, 'stakmv_instagram', sanitize_text_field((string) ($profile_input['instagram'] ?? '')));
    update_user_meta($user_id, 'stakmv_twitter', sanitize_text_field((string) ($profile_input['twitter'] ?? '')));
    update_user_meta($user_id, 'stakmv_ship_origin_address_1', sanitize_text_field((string) ($profile_input['ship_origin_address_1'] ?? '')));
    update_user_meta($user_id, 'stakmv_ship_origin_address_2', sanitize_text_field((string) ($profile_input['ship_origin_address_2'] ?? '')));
    update_user_meta($user_id, 'stakmv_ship_origin_city', sanitize_text_field((string) ($profile_input['ship_origin_city'] ?? '')));
    update_user_meta($user_id, 'stakmv_ship_origin_state', sanitize_text_field((string) ($profile_input['ship_origin_state'] ?? '')));
    update_user_meta($user_id, 'stakmv_ship_origin_postcode', sanitize_text_field((string) ($profile_input['ship_origin_postcode'] ?? '')));
    update_user_meta($user_id, 'stakmv_ship_origin_country', sanitize_text_field((string) ($profile_input['ship_origin_country'] ?? '')));

    $banner_slide_urls = array();
    $raw_slide_urls = (isset($profile_input['stakmv_banner_slide_urls']) && is_array($profile_input['stakmv_banner_slide_urls']))
        ? $profile_input['stakmv_banner_slide_urls']
        : array();
    if (is_array($raw_slide_urls)) {
        foreach ($raw_slide_urls as $raw_url) {
            $url = esc_url_raw((string) $raw_url);
            if ($url === '') {
                continue;
            }
            $banner_slide_urls[] = $url;
            if (count($banner_slide_urls) >= 5) {
                break;
            }
        }
    }
    update_user_meta($user_id, 'stakmv_banner_slide_urls', $banner_slide_urls);

    wp_safe_redirect(add_query_arg('profile_updated', 1, wp_get_referer()));
    exit;
});

/**
 * Handle vendor dashboard settings save (frontend)
 */
add_action('init', function () {
    $save_settings = filter_input(INPUT_POST, 'stakmv_save_settings', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    if (!$save_settings) {
        return;
    }

    $settings_nonce = filter_input(INPUT_POST, 'stakmv_settings_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    if (
        !$settings_nonce ||
        !wp_verify_nonce(
            sanitize_text_field((string) $settings_nonce),
            'stakmv_save_settings'
        )
    ) {
        wp_die('Security check failed');
    }

    if (!is_user_logged_in()) {
        wp_die('Not allowed');
    }

    $user_id = get_current_user_id();

    // Normalize boolean settings as 'yes'/'no'
    $notify_bids    = filter_input(INPUT_POST, 'stakmv_notify_bids', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ? 'yes' : 'no';
    $public_profile = filter_input(INPUT_POST, 'stakmv_public_profile', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ? 'yes' : 'no';

    update_user_meta($user_id, 'stakmv_notify_bids', $notify_bids);
    update_user_meta($user_id, 'stakmv_public_profile', $public_profile);

    wp_safe_redirect(add_query_arg('profile_updated', 1, wp_get_referer()));
    exit;
});
