<?php
/**
 * Focused onboarding flow introduced in 3.3.19.
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}

$yatoon_wizard_step = filter_input( INPUT_GET, 'wizard_step', FILTER_VALIDATE_INT );
$yatoon_wizard_step = $yatoon_wizard_step ? max( 1, min( 5, $yatoon_wizard_step ) ) : 1;
$yatoon_is_pro_package = defined( 'YATOON_PACKAGE_TIER' ) && 'pro' === YATOON_PACKAGE_TIER;
$yatoon_platform = sanitize_key( (string) get_option( 'yatoon_active_platform', 'local' ) );
$yatoon_pages = get_pages( array( 'sort_column' => 'post_title', 'post_status' => 'publish' ) );
$yatoon_booking_page_id = absint( get_option( 'yatoon_booking_page_id', 0 ) );
$yatoon_booking_page_url = $yatoon_booking_page_id ? get_permalink( $yatoon_booking_page_id ) : '';
$yatoon_import_choice = sanitize_key( (string) get_option( 'yatoon_onboarding_import_choice', 'fresh' ) );
$yatoon_import_choice = in_array( $yatoon_import_choice, array( 'fresh', 'import' ), true ) ? $yatoon_import_choice : 'fresh';
?>
<div class="wrap yatoon-setup-wizard-page">

<div class="yw-launch-head">
    <div>
        <h1><?php esc_html_e( "Let's get your booking page live", 'yatoon-booking-system' ); ?></h1>
        <p><?php esc_html_e( 'A few quick steps, and clients can start booking with you today.', 'yatoon-booking-system' ); ?></p>
        <span class="yw-launch-step"><?php echo esc_html( sprintf( __( 'Step %1$d of 4 - about %2$d minutes remaining', 'yatoon-booking-system' ), min( $yatoon_wizard_step, 4 ), max( 1, 5 - min( $yatoon_wizard_step, 4 ) ) ) ); ?></span>
    </div>
    <span class="yw-launch-tier"><?php echo esc_html( $yatoon_is_pro_package ? __( 'Pro package', 'yatoon-booking-system' ) : __( 'Free local booking', 'yatoon-booking-system' ) ); ?></span>
</div>

<div class="yw-launch-progress" role="progressbar" aria-label="<?php esc_attr_e( 'Setup progress', 'yatoon-booking-system' ); ?>" aria-valuemin="1" aria-valuemax="4" aria-valuenow="<?php echo esc_attr( min( $yatoon_wizard_step, 4 ) ); ?>">
    <?php for ( $yatoon_progress_index = 1; $yatoon_progress_index <= 4; $yatoon_progress_index++ ) : ?>
        <span aria-hidden="true" class="<?php echo $yatoon_progress_index <= min( $yatoon_wizard_step, 4 ) ? 'is-done' : ''; ?>"></span>
    <?php endfor; ?>
</div>

<?php if ( 5 === $yatoon_wizard_step ) : ?>
    <?php
    global $wpdb;
    $yatoon_service_count = (int) yatoon_db_get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}yatoon_services WHERE is_active = 1" );
    $yatoon_staff_count = (int) yatoon_db_get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}yatoon_staff WHERE is_active = 1 AND is_bookable = 1" );
    $yatoon_booking_page_id = absint( get_option( 'yatoon_booking_page_id', 0 ) );
    $yatoon_booking_page_url = $yatoon_booking_page_id ? get_permalink( $yatoon_booking_page_id ) : '';
    $yatoon_square_required = $yatoon_is_pro_package && 'square' === $yatoon_platform;
    $yatoon_square_token = class_exists( 'YATOON_Crypto' ) ? YATOON_Crypto::get_option( 'yatoon_square_access_token', '' ) : get_option( 'yatoon_square_access_token', '' );
    $yatoon_square_ready = ! $yatoon_square_required || ( $yatoon_square_token && get_option( 'yatoon_square_location_id', '' ) );
    $yatoon_onboarding_milestones = (array) get_option( 'yatoon_onboarding_milestones', array() );
    $yatoon_test_complete = ! empty( $yatoon_onboarding_milestones['first_test_booking'] ) || ! empty( $yatoon_onboarding_milestones['first_real_booking'] );
    $yatoon_launch_ready = $yatoon_service_count > 0 && $yatoon_staff_count > 0 && $yatoon_booking_page_url && $yatoon_square_ready;
    ?>
    <section class="yw-launch-card">
        <h2><?php echo esc_html( $yatoon_launch_ready ? __( 'Your five-minute launch checklist is ready', 'yatoon-booking-system' ) : __( 'Complete the last launch items', 'yatoon-booking-system' ) ); ?></h2>
        <p class="yw-launch-sub"><?php echo esc_html( $yatoon_launch_ready ? __( 'Open the customer page and place one test booking before sharing the link.', 'yatoon-booking-system' ) : __( 'Add at least one service and one bookable staff member, then return to the launch center.', 'yatoon-booking-system' ) ); ?></p>
        <div class="yw-launch-ready">
            <div class="yw-launch-ready-row"><span class="yw-launch-status <?php echo esc_attr( $yatoon_service_count ? '' : 'is-missing' ); ?>"><?php if ( $yatoon_service_count ) : ?>&#10003;<?php else : ?>!<?php endif; ?></span><strong><?php
                /* translators: %d: number of active services. */
                echo esc_html( sprintf( _n( '%d active service', '%d active services', $yatoon_service_count, 'yatoon-booking-system' ), $yatoon_service_count ) );
            ?></strong></div>
            <div class="yw-launch-ready-row"><span class="yw-launch-status <?php echo esc_attr( $yatoon_staff_count ? '' : 'is-missing' ); ?>"><?php if ( $yatoon_staff_count ) : ?>&#10003;<?php else : ?>!<?php endif; ?></span><strong><?php
                /* translators: %d: number of bookable staff members. */
                echo esc_html( sprintf( _n( '%d bookable staff member', '%d bookable staff members', $yatoon_staff_count, 'yatoon-booking-system' ), $yatoon_staff_count ) );
            ?></strong></div>
            <div class="yw-launch-ready-row"><span class="yw-launch-status <?php echo esc_attr( $yatoon_booking_page_url ? '' : 'is-missing' ); ?>"><?php if ( $yatoon_booking_page_url ) : ?>&#10003;<?php else : ?>!<?php endif; ?></span><strong><?php echo esc_html( $yatoon_booking_page_url ? __( 'Booking page created', 'yatoon-booking-system' ) : __( 'Booking page needs attention', 'yatoon-booking-system' ) ); ?></strong></div>
            <?php if ( $yatoon_square_required ) : ?><div class="yw-launch-ready-row"><span class="yw-launch-status <?php echo esc_attr( $yatoon_square_ready ? '' : 'is-missing' ); ?>"><?php if ( $yatoon_square_ready ) : ?>&#10003;<?php else : ?>!<?php endif; ?></span><strong><?php echo esc_html( $yatoon_square_ready ? __( 'Square account and location connected', 'yatoon-booking-system' ) : __( 'Connect Square and choose a location', 'yatoon-booking-system' ) ); ?></strong><?php if ( ! $yatoon_square_ready ) : ?><a class="yw-launch-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=yatoon-booking-system&tab=sync' ) ); ?>"><?php esc_html_e( 'Connect Square', 'yatoon-booking-system' ); ?></a><?php endif; ?></div><?php endif; ?>
            <div class="yw-launch-ready-row"><span class="yw-launch-status <?php echo esc_attr( $yatoon_test_complete ? '' : 'is-missing' ); ?>"><?php if ( $yatoon_test_complete ) : ?>&#10003;<?php else : ?>!<?php endif; ?></span><strong><?php echo esc_html( $yatoon_test_complete ? __( 'Booking flow tested successfully', 'yatoon-booking-system' ) : __( 'One test booking still required', 'yatoon-booking-system' ) ); ?></strong></div>
        </div>
        <div class="yw-launch-actions">
            <a class="yw-launch-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=yatoon-dashboard' ) ); ?>"><?php esc_html_e( 'Open launch center', 'yatoon-booking-system' ); ?></a>
            <div class="yw-launch-actions-right">
                <?php if ( !$yatoon_service_count ) : ?><a class="yw-launch-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=sbs-services' ) ); ?>"><?php esc_html_e( 'Add services', 'yatoon-booking-system' ); ?></a><?php endif; ?>
                <?php if ( !$yatoon_staff_count ) : ?><a class="yw-launch-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=sbs-staff' ) ); ?>"><?php esc_html_e( 'Add staff', 'yatoon-booking-system' ); ?></a><?php endif; ?>
                <?php if ( $yatoon_launch_ready && ! $yatoon_test_complete ) : ?><a class="yw-launch-btn yw-launch-btn-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=yatoon_start_test_booking' ), 'yatoon_start_test_booking' ) ); ?>"><?php esc_html_e( 'Start test booking', 'yatoon-booking-system' ); ?></a><?php elseif ( $yatoon_booking_page_url ) : ?><a class="yw-launch-btn yw-launch-btn-primary" href="<?php echo esc_url( $yatoon_booking_page_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'View booking page', 'yatoon-booking-system' ); ?></a><?php endif; ?>
            </div>
        </div>
    </section>
<?php else : ?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
    <?php wp_nonce_field( 'yatoon_wizard_save', 'yatoon_wizard_nonce' ); ?>
    <input type="hidden" name="action" value="yatoon_wizard_save">
    <input type="hidden" name="wizard_step" value="<?php echo esc_attr( $yatoon_wizard_step ); ?>">

    <section class="yw-launch-card">
    <?php if ( 1 === $yatoon_wizard_step ) : ?>
        <h2><?php esc_html_e( 'Business details', 'yatoon-booking-system' ); ?></h2>
        <p class="yw-launch-sub"><?php esc_html_e( 'Used on booking confirmations and customer-facing pages.', 'yatoon-booking-system' ); ?></p>
        <div class="yw-launch-field"><label for="yw-launch-name"><?php esc_html_e( 'Business name', 'yatoon-booking-system' ); ?></label><input id="yw-launch-name" type="text" name="yatoon_business_name" value="<?php echo esc_attr( get_option( 'yatoon_business_name', get_bloginfo( 'name' ) ) ); ?>" required></div>
        <div class="yw-launch-grid">
            <div class="yw-launch-field"><label for="yw-launch-phone"><?php esc_html_e( 'Phone', 'yatoon-booking-system' ); ?></label><input id="yw-launch-phone" type="tel" name="yatoon_business_phone" value="<?php echo esc_attr( get_option( 'yatoon_business_phone', '' ) ); ?>"></div>
            <div class="yw-launch-field"><label for="yw-launch-email"><?php esc_html_e( 'Booking email', 'yatoon-booking-system' ); ?></label><input id="yw-launch-email" type="email" name="yatoon_contact_email" value="<?php echo esc_attr( get_option( 'yatoon_contact_email', get_option( 'admin_email' ) ) ); ?>"></div>
        </div>
        <div class="yw-launch-field"><label for="yw-launch-address"><?php esc_html_e( 'Address', 'yatoon-booking-system' ); ?></label><input id="yw-launch-address" type="text" name="yatoon_business_address" value="<?php echo esc_attr( get_option( 'yatoon_business_address', '' ) ); ?>"></div>
        <div class="yw-launch-field"><label for="yw-launch-site"><?php esc_html_e( 'Website', 'yatoon-booking-system' ); ?></label><input id="yw-launch-site" type="url" name="yatoon_business_website" value="<?php echo esc_attr( get_option( 'yatoon_business_website', home_url( '/' ) ) ); ?>"></div>

    <?php elseif ( 2 === $yatoon_wizard_step ) : ?>
        <h2><?php esc_html_e( 'Choose how to start', 'yatoon-booking-system' ); ?></h2>
        <p class="yw-launch-sub"><?php esc_html_e( 'Nothing is added to your service or staff lists unless you choose demo content.', 'yatoon-booking-system' ); ?></p>
        <span class="yw-launch-label" id="yw-starter-content-label"><?php esc_html_e( 'Starter content', 'yatoon-booking-system' ); ?></span>
        <div class="yw-launch-choice-grid" role="radiogroup" aria-labelledby="yw-starter-content-label">
            <label class="yw-launch-choice"><input type="radio" name="yatoon_starter_mode" value="blank" checked><span><strong><?php esc_html_e( 'Start with my own data', 'yatoon-booking-system' ); ?></strong><small><?php esc_html_e( 'Best for a live business. Your lists stay empty until you add or sync data.', 'yatoon-booking-system' ); ?></small></span></label>
            <label class="yw-launch-choice"><input type="radio" name="yatoon_starter_mode" value="demo"><span><strong><?php esc_html_e( 'Add demo services', 'yatoon-booking-system' ); ?></strong><small><?php esc_html_e( 'Adds two sample staff members and three services for a quick test.', 'yatoon-booking-system' ); ?></small></span></label>
            <label class="yw-launch-field"><?php esc_html_e( 'Demo business type', 'yatoon-booking-system' ); ?><select name="yatoon_starter_industry"><option value="nail"><?php esc_html_e( 'Nail salon', 'yatoon-booking-system' ); ?></option><option value="hair"><?php esc_html_e( 'Hair salon', 'yatoon-booking-system' ); ?></option><option value="massage"><?php esc_html_e( 'Massage studio', 'yatoon-booking-system' ); ?></option><option value="medspa"><?php esc_html_e( 'Medical spa', 'yatoon-booking-system' ); ?></option><option value="tattoo"><?php esc_html_e( 'Tattoo studio', 'yatoon-booking-system' ); ?></option></select></label>
        </div>
        <span class="yw-launch-label" id="yw-import-label"><?php esc_html_e( 'Bring your existing business data', 'yatoon-booking-system' ); ?></span>
        <div class="yw-launch-choice-grid" role="radiogroup" aria-labelledby="yw-import-label">
            <label class="yw-launch-choice"><input type="radio" name="yatoon_import_choice" value="fresh" <?php checked( $yatoon_import_choice, 'fresh' ); ?>><span><strong><?php esc_html_e( 'No, start fresh', 'yatoon-booking-system' ); ?></strong><small><?php esc_html_e( 'Continue setup and add services, staff, and clients yourself.', 'yatoon-booking-system' ); ?></small></span></label>
            <label class="yw-launch-choice"><input type="radio" name="yatoon_import_choice" value="import" <?php checked( $yatoon_import_choice, 'import' ); ?>><span><strong><?php esc_html_e( 'Yes — import from Bookly, Amelia, Square, or CSV', 'yatoon-booking-system' ); ?></strong><small><?php esc_html_e( 'Import services, staff, and clients from Bookly, Amelia, or a CSV export. Square users can connect and sync their catalog.', 'yatoon-booking-system' ); ?></small></span></label>
        </div>
        <div class="yw-launch-callout"><strong><?php esc_html_e( 'Safe migration workflow', 'yatoon-booking-system' ); ?></strong> <?php esc_html_e( 'Yatoon previews changes first and updates matching records instead of creating duplicates. After importing, return to the wizard to finish hours and booking pages.', 'yatoon-booking-system' ); ?></div>
        <?php if ( $yatoon_is_pro_package ) : ?>
            <span class="yw-launch-label"><?php esc_html_e( 'Booking data source', 'yatoon-booking-system' ); ?></span>
            <div class="yw-launch-choice-grid">
                <?php foreach ( array( 'local' => __( 'Local WordPress', 'yatoon-booking-system' ), 'square' => __( 'Square Appointments', 'yatoon-booking-system' ), 'vagaro' => __( 'Vagaro (Experimental)', 'yatoon-booking-system' ) ) as $yatoon_platform_key => $yatoon_platform_label ) : ?>
                    <label class="yw-launch-choice"><input type="radio" name="yatoon_active_platform" value="<?php echo esc_attr( $yatoon_platform_key ); ?>" <?php checked( $yatoon_platform, $yatoon_platform_key ); ?>><span><strong><?php echo esc_html( $yatoon_platform_label ); ?></strong><small><?php echo esc_html( 'local' === $yatoon_platform_key ? __( 'Manage availability inside WordPress.', 'yatoon-booking-system' ) : __( 'Connect and sync live availability.', 'yatoon-booking-system' ) ); ?></small></span></label>
                <?php endforeach; ?>
            </div>
            <div class="yw-launch-callout">
                <strong><?php esc_html_e( 'Connect integrations after the booking page is live.', 'yatoon-booking-system' ); ?></strong>
                <?php esc_html_e( 'The wizard now keeps first setup focused on business details, hours, pages, and a test booking. Square, Vagaro, Google Calendar, SMS, payments, and deposits stay available from Settings when you are ready.', 'yatoon-booking-system' ); ?>
                <div class="yw-launch-actions-right" style="margin-top:10px;">
                    <a class="yw-launch-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=yatoon-booking-system&tab=sync' ) ); ?>"><?php esc_html_e( 'Open sync settings', 'yatoon-booking-system' ); ?></a>
                    <a class="yw-launch-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=yatoon-deposit-pro' ) ); ?>"><?php esc_html_e( 'Payment settings', 'yatoon-booking-system' ); ?></a>
                </div>
            </div>
        <?php else : ?>
            <div class="yw-launch-callout"><strong><?php esc_html_e( 'Free uses local WordPress booking.', 'yatoon-booking-system' ); ?></strong> <?php esc_html_e( 'Square and Vagaro live sync are available in Pro; no integration credentials are requested in Free.', 'yatoon-booking-system' ); ?></div>
        <?php endif; ?>

    <?php elseif ( 3 === $yatoon_wizard_step ) : ?>
        <h2><?php esc_html_e( 'Hours and notifications', 'yatoon-booking-system' ); ?></h2>
        <p class="yw-launch-sub"><?php esc_html_e( 'Set a simple weekly schedule now. You can add breaks and staff-specific hours later.', 'yatoon-booking-system' ); ?></p>
        <?php
        $yatoon_day_defaults = array( 'monday'=>'09:00-18:00','tuesday'=>'09:00-18:00','wednesday'=>'09:00-18:00','thursday'=>'09:00-18:00','friday'=>'09:00-18:00','saturday'=>'09:00-17:00','sunday'=>'closed' );
        foreach ( $yatoon_day_defaults as $yatoon_day => $yatoon_default_hours ) :
            $yatoon_day_value = (string) get_option( "yatoon_hours_{$yatoon_day}", $yatoon_default_hours );
        ?>
            <div class="yw-launch-hours"><label for="yw-launch-<?php echo esc_attr( $yatoon_day ); ?>"><?php echo esc_html( $yatoon_day ); ?></label><input id="yw-launch-<?php echo esc_attr( $yatoon_day ); ?>" type="text" name="yatoon_hours_<?php echo esc_attr( $yatoon_day ); ?>" value="<?php echo esc_attr( 'closed' === $yatoon_day_value ? '' : $yatoon_day_value ); ?>" placeholder="09:00-18:00"><label class="yw-launch-closed"><input type="checkbox" name="yatoon_closed_<?php echo esc_attr( $yatoon_day ); ?>" value="1" <?php checked( $yatoon_day_value, 'closed' ); ?>> <?php esc_html_e( 'Closed', 'yatoon-booking-system' ); ?></label></div>
        <?php endforeach; ?>
        <hr>
        <label class="yw-launch-check"><input type="checkbox" name="yatoon_send_confirmation_email" value="1" <?php checked( get_option( 'yatoon_send_confirmation_email', '1' ), '1' ); ?>><span><?php esc_html_e( 'Send customers a booking confirmation email', 'yatoon-booking-system' ); ?></span></label>
        <label class="yw-launch-check"><input type="checkbox" name="yatoon_email_notify_admin" value="1" <?php checked( get_option( 'yatoon_email_notify_admin', '1' ), '1' ); ?>><span><?php esc_html_e( 'Notify the business email about new bookings', 'yatoon-booking-system' ); ?></span></label>
        <label class="yw-launch-check"><input type="checkbox" name="yatoon_reminder_enabled" value="1" <?php checked( get_option( 'yatoon_reminder_enabled', '0' ), '1' ); ?>><span><?php esc_html_e( 'Send an email reminder before the appointment', 'yatoon-booking-system' ); ?></span></label>
        <div class="yw-launch-field" style="max-width:220px"><label for="yw-launch-reminder-hours"><?php esc_html_e( 'Reminder hours before', 'yatoon-booking-system' ); ?></label><input id="yw-launch-reminder-hours" type="number" name="yatoon_reminder_hours_before" min="1" max="72" value="<?php echo esc_attr( get_option( 'yatoon_reminder_hours_before', 24 ) ); ?>"></div>

    <?php elseif ( 4 === $yatoon_wizard_step ) : ?>
        <h2><?php esc_html_e( 'Create your booking pages', 'yatoon-booking-system' ); ?></h2>
        <p class="yw-launch-sub"><?php esc_html_e( 'Yatoon can create the pages and add the correct shortcodes automatically.', 'yatoon-booking-system' ); ?></p>
        <div class="yw-launch-field"><label for="yw-launch-booking-page"><?php esc_html_e( 'Customer booking page', 'yatoon-booking-system' ); ?></label><select id="yw-launch-booking-page" name="yatoon_booking_page_id"><option value="0"><?php esc_html_e( 'Create a new Book an Appointment page', 'yatoon-booking-system' ); ?></option><?php foreach ( $yatoon_pages as $yatoon_page ) : ?><option value="<?php echo esc_attr( $yatoon_page->ID ); ?>" <?php selected( $yatoon_booking_page_id, $yatoon_page->ID ); ?>><?php echo esc_html( $yatoon_page->post_title ); ?></option><?php endforeach; ?></select><p class="description"><?php esc_html_e( 'If you select an existing page, Yatoon appends [yatoon_booking] only when it is missing.', 'yatoon-booking-system' ); ?></p></div>
        <label class="yw-launch-check"><input type="checkbox" name="yatoon_create_support_pages" value="1" checked><span><strong><?php esc_html_e( 'Also create customer self-service pages', 'yatoon-booking-system' ); ?></strong><br><?php esc_html_e( 'Creates Manage Your Appointment and My Appointments pages.', 'yatoon-booking-system' ); ?></span></label>
        <div class="yw-launch-grid"><div class="yw-launch-field"><label for="yw-launch-color"><?php esc_html_e( 'Primary color', 'yatoon-booking-system' ); ?></label><input id="yw-launch-color" type="color" name="yatoon_primary_color" value="<?php echo esc_attr( get_option( 'yatoon_primary_color', '#ec4899' ) ); ?>" style="width:72px;height:42px;padding:3px"></div><div class="yw-launch-field"><label for="yw-launch-advance"><?php esc_html_e( 'Minimum advance notice (hours)', 'yatoon-booking-system' ); ?></label><input id="yw-launch-advance" type="number" name="yatoon_min_advance_hours" min="0" max="72" value="<?php echo esc_attr( get_option( 'yatoon_min_advance_hours', 2 ) ); ?>"></div></div>
    <?php endif; ?>
    </section>

    <div class="yw-launch-actions">
        <div><?php if ( $yatoon_wizard_step > 1 ) : ?><a class="yw-launch-btn" href="<?php echo esc_url( add_query_arg( 'wizard_step', $yatoon_wizard_step - 1 ) ); ?>"><?php esc_html_e( 'Back', 'yatoon-booking-system' ); ?></a><?php endif; ?></div>
        <div class="yw-launch-actions-right"><button class="yw-launch-skip" type="submit" name="skip_step" value="1"><?php esc_html_e( 'Skip for now', 'yatoon-booking-system' ); ?></button><button class="yw-launch-btn yw-launch-btn-primary" type="submit"><?php echo esc_html( 4 === $yatoon_wizard_step ? __( 'Create pages and finish', 'yatoon-booking-system' ) : __( 'Save and continue', 'yatoon-booking-system' ) ); ?></button></div>
    </div>
</form>
<?php endif; ?>

<div class="yw-launch-footer"><a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=yatoon_dismiss_wizard' ), 'yatoon_dismiss_wizard' ) ); ?>"><?php esc_html_e( 'Exit setup wizard', 'yatoon-booking-system' ); ?></a></div>
</div>
