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

function lt_core_onboarding_menu() {
    add_submenu_page(
        'lt-core-dashboard',
        __( 'Setup Wizard', 'law-temple-core' ),
        __( 'Setup Wizard', 'law-temple-core' ),
        'lt_run_setup',
        'lt-core-onboarding',
        'lt_core_onboarding_page'
    );
}
add_action( 'admin_menu', 'lt_core_onboarding_menu', 30 );

function lt_core_onboarding_save() {
    if ( ! lt_core_current_user_can( 'lt_run_setup' ) ) { wp_die( esc_html__( 'You are not allowed to run the setup wizard.', 'law-temple-core' ) ); }
    check_admin_referer( 'lt_core_onboarding', 'lt_core_onboarding_nonce' );
    $mode = sanitize_key( $_POST['lt_onboarding_mode'] ?? 'generic' );
    if ( ! in_array( $mode, array( 'generic', 'law-temple' ), true ) ) { $mode = 'generic'; }

    $current = lt_core_options();
    $map = array(
        'firm_name'       => 'firm_name',
        'legal_name'      => 'legal_name',
        'tagline'         => 'tagline',
        'general_email'   => 'general_email',
        'phone_primary'   => 'phone_primary',
        'postal_address'  => 'postal_address',
        'office_hours'    => 'office_hours',
    );
    foreach ( $map as $post_key => $option_key ) {
        if ( isset( $_POST[ $post_key ] ) ) { $current[ $option_key ] = sanitize_textarea_field( wp_unslash( $_POST[ $post_key ] ) ); }
    }
    if ( ! empty( $_POST['general_email'] ) ) { $current['general_email'] = sanitize_email( wp_unslash( $_POST['general_email'] ) ); }
    update_option( 'lt_core_options', lt_core_sanitize_options( $current ), false );

    if ( 'law-temple' === $mode ) {
        $report = lt_core_install_full_law_temple_demo();
        if ( is_wp_error( $report ) ) {
            wp_safe_redirect( add_query_arg( array( 'page'=>'lt-core-onboarding', 'lt_status'=>'error', 'lt_message'=>rawurlencode( $report->get_error_message() ) ), admin_url( 'admin.php' ) ) );
            exit;
        }
    } else {
        $result = lt_core_run_setup( ! empty( $_POST['minimal_starter'] ) );
        if ( is_wp_error( $result ) ) {
            wp_safe_redirect( add_query_arg( array( 'page'=>'lt-core-onboarding', 'lt_status'=>'error', 'lt_message'=>rawurlencode( $result->get_error_message() ) ), admin_url( 'admin.php' ) ) );
            exit;
        }
    }
    update_option( 'lt_core_onboarding_complete', gmdate( 'c' ), false );
    update_option( 'lt_core_onboarding_mode', $mode, false );
    wp_safe_redirect( add_query_arg( array( 'page'=>'lt-core-onboarding', 'lt_status'=>'success' ), admin_url( 'admin.php' ) ) );
    exit;
}
add_action( 'admin_post_lt_core_onboarding_save', 'lt_core_onboarding_save' );

function lt_core_onboarding_page() {
    if ( ! lt_core_current_user_can( 'lt_run_setup' ) ) { return; }
    $o = lt_core_options();
    $status = sanitize_key( $_GET['lt_status'] ?? '' );
    echo '<div class="wrap lt-onboarding"><h1>' . esc_html__( 'Law Firm Setup Wizard', 'law-temple-core' ) . '</h1>';
    echo '<p class="description">' . esc_html__( 'Configure the framework in four guided steps. Nothing is published or overwritten merely by opening this wizard.', 'law-temple-core' ) . '</p>';
    if ( 'success' === $status ) { echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Setup completed. Review the imported content and theme settings before launch.', 'law-temple-core' ) . '</p></div>'; }
    if ( 'error' === $status ) { echo '<div class="notice notice-error"><p>' . esc_html( wp_unslash( $_GET['lt_message'] ?? __( 'Setup could not be completed.', 'law-temple-core' ) ) ) . '</p></div>'; }

    echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" class="lt-wizard" data-lt-wizard>';
    echo '<input type="hidden" name="action" value="lt_core_onboarding_save">';
    wp_nonce_field( 'lt_core_onboarding', 'lt_core_onboarding_nonce' );
    echo '<ol class="lt-wizard-progress" aria-label="' . esc_attr__( 'Setup progress', 'law-temple-core' ) . '"><li class="is-active">1. ' . esc_html__( 'Start', 'law-temple-core' ) . '</li><li>2. ' . esc_html__( 'Firm', 'law-temple-core' ) . '</li><li>3. ' . esc_html__( 'Content', 'law-temple-core' ) . '</li><li>4. ' . esc_html__( 'Review', 'law-temple-core' ) . '</li></ol>';

    echo '<section class="lt-wizard-step is-active" data-step="1"><h2>' . esc_html__( 'Choose how to start', 'law-temple-core' ) . '</h2><div class="lt-choice-grid">';
    echo '<label class="lt-choice-card"><input type="radio" name="lt_onboarding_mode" value="generic" checked><strong>' . esc_html__( 'Clean law-firm framework', 'law-temple-core' ) . '</strong><span>' . esc_html__( 'Create essential pages and configuration without Law Temple-specific demo content.', 'law-temple-core' ) . '</span></label>';
    echo '<label class="lt-choice-card"><input type="radio" name="lt_onboarding_mode" value="law-temple"><strong>' . esc_html__( 'Full Law Temple demo', 'law-temple-core' ) . '</strong><span>' . esc_html__( 'Import the complete Law Temple brand, four lawyer profiles, hero slides, practices, sectors and review-safe draft examples.', 'law-temple-core' ) . '</span></label>';
    echo '</div></section>';

    echo '<section class="lt-wizard-step" data-step="2"><h2>' . esc_html__( 'Firm identity', 'law-temple-core' ) . '</h2><div class="lt-admin-fields">';
    $fields = array(
        'firm_name'=>array( __( 'Firm name', 'law-temple-core' ), $o['firm_name'] ?? '' ),
        'legal_name'=>array( __( 'Legal name', 'law-temple-core' ), $o['legal_name'] ?? '' ),
        'tagline'=>array( __( 'Tagline', 'law-temple-core' ), $o['tagline'] ?? '' ),
        'general_email'=>array( __( 'General email', 'law-temple-core' ), $o['general_email'] ?? '' ),
        'phone_primary'=>array( __( 'Primary telephone', 'law-temple-core' ), $o['phone_primary'] ?? '' ),
        'office_hours'=>array( __( 'Office hours', 'law-temple-core' ), $o['office_hours'] ?? '' ),
    );
    foreach ( $fields as $key => $row ) { echo '<div class="lt-admin-field"><label for="ltwiz_' . esc_attr( $key ) . '"><strong>' . esc_html( $row[0] ) . '</strong></label><input class="widefat" id="ltwiz_' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" value="' . esc_attr( $row[1] ) . '"></div>'; }
    echo '<div class="lt-admin-field" style="grid-column:1/-1"><label for="ltwiz_postal_address"><strong>' . esc_html__( 'Postal / office address', 'law-temple-core' ) . '</strong></label><textarea class="widefat" rows="3" id="ltwiz_postal_address" name="postal_address">' . esc_textarea( $o['postal_address'] ?? '' ) . '</textarea></div>';
    echo '</div></section>';

    echo '<section class="lt-wizard-step" data-step="3"><h2>' . esc_html__( 'Starter content', 'law-temple-core' ) . '</h2>';
    echo '<p>' . esc_html__( 'For a clean installation you may optionally create a minimal starter dataset as drafts. Full Law Temple demo mode ignores this checkbox and imports the complete demo manifest instead.', 'law-temple-core' ) . '</p>';
    echo '<label><input type="checkbox" name="minimal_starter" value="1"> ' . esc_html__( 'Create minimal draft starter content', 'law-temple-core' ) . '</label>';
    echo '<div class="lt-demo-summary">';
    foreach ( lt_core_demo_summary() as $key => $count ) { echo '<span><strong>' . esc_html( (string) $count ) . '</strong> ' . esc_html( ucwords( str_replace( '_', ' ', $key ) ) ) . '</span>'; }
    echo '</div></section>';

    echo '<section class="lt-wizard-step" data-step="4"><h2>' . esc_html__( 'Review and run', 'law-temple-core' ) . '</h2><div class="notice notice-warning inline"><p>' . esc_html__( 'The full Law Temple demo does not overwrite posts with matching slugs. Illustrative matters, testimonials, awards, careers, detailed Insights and legal examples remain drafts for review.', 'law-temple-core' ) . '</p></div><p><strong>' . esc_html__( 'When ready, run setup. You can revisit all options afterward.', 'law-temple-core' ) . '</strong></p></section>';

    echo '<div class="lt-wizard-actions"><button type="button" class="button" data-lt-wizard-prev disabled>' . esc_html__( 'Previous', 'law-temple-core' ) . '</button><button type="button" class="button button-primary" data-lt-wizard-next>' . esc_html__( 'Next', 'law-temple-core' ) . '</button><button type="submit" class="button button-primary" data-lt-wizard-submit hidden>' . esc_html__( 'Run setup', 'law-temple-core' ) . '</button></div>';
    echo '</form></div>';
}
