<?php
/**
 * Plugin Name: BW Feature Tour
 * Plugin URI: https://bowdenworks.com
 * Description: Create interactive, step-by-step feature tours with highlighted hotspots, tooltips, and multi-page support.
 * Version: 1.0.0
 * Author: Bowden Works
 * Author URI: https://bowdenworks.com
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: bw-feature-tour
 * Requires at least: 5.3
 * Requires PHP: 7.4
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

define( 'BW_FEATURE_TOUR_VERSION', '1.0.0' );
define( 'BW_FEATURE_TOUR_PATH', plugin_dir_path( __FILE__ ) );
define( 'BW_FEATURE_TOUR_URL', plugin_dir_url( __FILE__ ) );

require_once BW_FEATURE_TOUR_PATH . 'includes/class-bw-post-type.php';
require_once BW_FEATURE_TOUR_PATH . 'includes/class-bw-admin.php';
require_once BW_FEATURE_TOUR_PATH . 'includes/class-bw-settings.php';
require_once BW_FEATURE_TOUR_PATH . 'includes/class-bw-frontend.php';

function bw_feature_tour_init() {
    BW_Post_Type::get_instance();
    BW_Admin::get_instance();
    BW_Settings::get_instance();
    BW_Frontend::get_instance();
}
add_action( 'plugins_loaded', 'bw_feature_tour_init' );

/**
 * Activation — seed default options.
 */
function bw_feature_tour_activate() {
    $defaults = array(
        'bw_feature_tour_trigger'         => 'first_visit',
        'bw_feature_tour_show_to'         => 'logged_in',
        'bw_feature_tour_primary_color'   => '#2B2663',
        'bw_feature_tour_accent_color'    => '#1fa88e',
        'bw_feature_tour_allow_restart'   => '1',
        'bw_feature_tour_animation_speed' => '400',
        'bw_feature_tour_overlay_opacity' => '70',
    );
    foreach ( $defaults as $key => $value ) {
        if ( get_option( $key ) === false ) {
            add_option( $key, $value );
        }
    }
}
register_activation_hook( __FILE__, 'bw_feature_tour_activate' );

/**
 * AJAX: Mark tour completed for current user.
 */
function bw_feature_tour_complete_tour() {
    check_ajax_referer( 'bw_feature_tour_nonce', 'nonce' );

    if ( ! is_user_logged_in() ) {
        wp_send_json_success(); // Visitors tracked in localStorage client-side.
    }

    $guide_id = isset( $_POST['guide_id'] ) ? absint( $_POST['guide_id'] ) : 0;
    if ( ! $guide_id ) {
        wp_send_json_error( 'Invalid tour ID' );
    }

    $user_id   = get_current_user_id();
    $completed = get_user_meta( $user_id, 'bw_feature_tour_completed', true );
    if ( ! is_array( $completed ) ) {
        $completed = array();
    }
    $completed[ $guide_id ] = time();
    update_user_meta( $user_id, 'bw_feature_tour_completed', $completed );

    wp_send_json_success();
}
add_action( 'wp_ajax_bw_feature_tour_complete', 'bw_feature_tour_complete_tour' );

/**
 * AJAX: Reset completion (retake tour).
 */
function bw_feature_tour_reset_tour() {
    check_ajax_referer( 'bw_feature_tour_nonce', 'nonce' );

    if ( ! is_user_logged_in() ) {
        wp_send_json_success();
    }

    $guide_id = isset( $_POST['guide_id'] ) ? absint( $_POST['guide_id'] ) : 0;
    $user_id  = get_current_user_id();
    $completed = get_user_meta( $user_id, 'bw_feature_tour_completed', true );
    if ( is_array( $completed ) && isset( $completed[ $guide_id ] ) ) {
        unset( $completed[ $guide_id ] );
        update_user_meta( $user_id, 'bw_feature_tour_completed', $completed );
    }

    wp_send_json_success();
}
add_action( 'wp_ajax_bw_feature_tour_reset', 'bw_feature_tour_reset_tour' );
