<?php
/**
 * Default Numerology Form Template - Dynamic & Admin-Styled
 *
 * Variables expected:
 *   - $endpoint (string)
 *   - $params (array)
 *   - $param_desc (array)   <-- descriptions for each param
 *   - $styles (array)
 *   - $template (string, optional)
 */

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

$dakidarts_styles         = isset( $styles ) && is_array( $styles ) ? $styles : get_option( 'dakidarts_numerology_style_settings', [] );
$dakidarts_params         = isset( $params ) && is_array( $params ) ? $params : [];
$dakidarts_param_desc     = isset( $param_desc ) && is_array( $param_desc ) ? $param_desc : [];
$dakidarts_endpoint       = isset( $endpoint ) ? $endpoint : '';
$dakidarts_template       = isset( $template ) ? $template : 'default';
$dakidarts_required_params = isset( $required_params ) ? $required_params : $dakidarts_params;

// Form container styles
$dakidarts_form_style = sprintf(
    'background:%s;padding:%s;margin:%s;border-radius:%s;',
    esc_attr( $dakidarts_styles['form_bg'] ?? '#fff' ),
    esc_attr( $dakidarts_styles['form_padding'] ?? '25px' ),
    esc_attr( $dakidarts_styles['form_margin'] ?? '2rem auto' ),
    esc_attr( $dakidarts_styles['form_radius'] ?? '20px' )
);

// Button text & style mode
$dakidarts_button_text = ! empty( $dakidarts_styles['calculate_text'] ) ? $dakidarts_styles['calculate_text'] : __( 'Calculate Report', 'dakidarts-numerology-core' );
$dakidarts_button_mode = $dakidarts_styles['button_style_mode'] ?? 'inherit';

// Button inline styles
$dakidarts_button_inline_style = '';
if ( $dakidarts_button_mode === 'custom' ) {
    $dakidarts_button_inline_style = sprintf(
        'background:%s;color:%s;padding:%s;border:%s %s %s;border-radius:%s;display:block;margin:%s;text-align:center;',
        esc_attr( $dakidarts_styles['button_bg'] ?? '#4f46e5' ),
        esc_attr( $dakidarts_styles['button_color'] ?? '#fff' ),
        esc_attr( $dakidarts_styles['button_padding'] ?? '12px 24px' ),
        esc_attr( $dakidarts_styles['button_border_width'] ?? '1px' ),
        esc_attr( $dakidarts_styles['button_border_style'] ?? 'solid' ),
        esc_attr( $dakidarts_styles['button_border_color'] ?? '#4f46e5' ),
        esc_attr( $dakidarts_styles['button_radius'] ?? '12px' ),
        ( $dakidarts_styles['button_position'] ?? 'center' ) === 'center' ? '0 auto' :
        ( ( $dakidarts_styles['button_position'] ?? 'center' ) === 'right' ? '0 0 0 auto' : '0 0 0 0' )
    );
}
?>

<div class="dakidarts-form-wrapper" style="position: relative; max-width: 550px; margin: 2rem auto;">
    <!-- Cinematic Celestial Loader overlay -->
    <div class="dakidarts-loader-overlay">
        <div class="dakidarts-orbital-loader">
            <div class="dakidarts-orbital-ring"></div>
            <div class="dakidarts-orbital-ring"></div>
            <div class="dakidarts-orbital-ring"></div>
        </div>
        <div class="dakidarts-loader-text"><?php esc_html_e( 'Invoking Celestial Wisdom...', 'dakidarts-numerology-core' ); ?></div>
    </div>

    <form method="post" action="<?php echo esc_url( get_permalink() ); ?>" class="dakidarts-form dakidarts-form-<?php echo esc_attr( $dakidarts_template ); ?> dakidarts-numerology-form" data-endpoint="<?php echo esc_attr( $dakidarts_endpoint ); ?>" style="<?php echo esc_attr( $dakidarts_form_style ); ?>">
        <?php wp_nonce_field( 'dakidarts_numerology_submit', 'dakidarts_nonce' ); ?>
        
        <?php if ( ! empty( $dakidarts_params ) ) : ?>
            <?php
            $dakidarts_is_compatibility = $dakidarts_endpoint === 'compatibility-score';

            $dakidarts_render_field = function( $dakidarts_param ) use ( $dakidarts_param_desc, $dakidarts_required_params ) {
                $dakidarts_required = in_array( $dakidarts_param, $dakidarts_required_params ?? [], true );
                $field_label = ucwords( str_replace( '_', ' ', $dakidarts_param ) );
                ?>
                <p class="dakidarts-form-group">
                    <label for="<?php echo esc_attr( $dakidarts_param ); ?>">
                        <?php echo esc_html( $field_label ); ?><?php echo $dakidarts_required ? ' *' : ' (' . esc_html__( 'optional', 'dakidarts-numerology-core' ) . ')'; ?>
                    </label>
                    
                    <?php if ( in_array( $dakidarts_param, [ 'reduce', 'include_gaps', 'expanded', 'allow_masters' ], true ) ) : ?>
                        <!-- Modern Segment Toggle Control -->
                        <div class="dakidarts-toggle-wrapper">
                            <input type="hidden" name="<?php echo esc_attr( $dakidarts_param ); ?>" class="dakidarts-toggle-hidden" value="false" />
                            <div class="dakidarts-toggle-option" data-value="true"><?php esc_html_e( 'Yes', 'dakidarts-numerology-core' ); ?></div>
                            <div class="dakidarts-toggle-option active" data-value="false"><?php esc_html_e( 'No', 'dakidarts-numerology-core' ); ?></div>
                        </div>
                        
                        <?php if ( $dakidarts_param === 'expanded' ) : ?>
                            <span class="dakidarts-param-desc">
                                <span class="dakidarts-info-dot">i</span>
                                <?php echo esc_html__( 'Tip: returns full cycles and deep diagnostic meanings.', 'dakidarts-numerology-core' ); ?>
                            </span>
                        <?php endif; ?>
                        <?php if ( $dakidarts_param === 'allow_masters' ) : ?>
                            <span class="dakidarts-param-desc">
                                <span class="dakidarts-info-dot">i</span>
                                <?php echo esc_html__( 'Tip: Enable master-number reductions (e.g. 11, 22).', 'dakidarts-numerology-core' ); ?>
                            </span>
                        <?php endif; ?>
                    <?php else : ?>
                        <?php 
                        // Intelligent input placeholders
                        $placeholder = '';
                        if ( strpos($dakidarts_param, 'year') !== false ) {
                            $placeholder = 'e.g. 1994';
                        } elseif ( strpos($dakidarts_param, 'month') !== false ) {
                            $placeholder = 'MM (01-12)';
                        } elseif ( strpos($dakidarts_param, 'day') !== false ) {
                            $placeholder = 'DD (01-31)';
                        } elseif ( strpos($dakidarts_param, 'dob') !== false ) {
                            $placeholder = 'YYYY-MM-DD';
                        } elseif ( strpos($dakidarts_param, 'name') !== false ) {
                            $placeholder = 'Enter full birth name...';
                        }
                        ?>
                        <input type="text" id="<?php echo esc_attr( $dakidarts_param ); ?>" name="<?php echo esc_attr( $dakidarts_param ); ?>" placeholder="<?php echo esc_attr( $placeholder ); ?>" <?php echo $dakidarts_required ? 'required' : ''; ?> />
                    <?php endif; ?>
                    
                    <?php if ( ! empty( $dakidarts_param_desc[ $dakidarts_param ] ) ) : ?>
                        <span class="dakidarts-param-desc"><?php echo esc_html( $dakidarts_param_desc[ $dakidarts_param ] ); ?></span>
                    <?php endif; ?>
                </p>
                <?php
            };

            $dakidarts_is_compatibility = $dakidarts_endpoint === 'compatibility-score';

            $dakidarts_render_field = function( $dakidarts_param ) use ( $dakidarts_param_desc, $dakidarts_required_params ) {
                $dakidarts_required = in_array( $dakidarts_param, $dakidarts_required_params ?? [], true );
                $field_label = ucwords( str_replace( '_', ' ', $dakidarts_param ) );
                ?>
                <p class="dakidarts-form-group">
                    <label for="<?php echo esc_attr( $dakidarts_param ); ?>">
                        <?php echo esc_html( $field_label ); ?><?php echo $dakidarts_required ? ' *' : ' (' . esc_html__( 'optional', 'dakidarts-numerology-core' ) . ')'; ?>
                    </label>
                    
                    <?php if ( in_array( $dakidarts_param, [ 'reduce', 'include_gaps', 'expanded', 'allow_masters' ], true ) ) : ?>
                        <!-- Modern Segment Toggle Control -->
                        <div class="dakidarts-toggle-wrapper">
                            <input type="hidden" name="<?php echo esc_attr( $dakidarts_param ); ?>" class="dakidarts-toggle-hidden" value="false" />
                            <div class="dakidarts-toggle-option" data-value="true"><?php esc_html_e( 'Yes', 'dakidarts-numerology-core' ); ?></div>
                            <div class="dakidarts-toggle-option active" data-value="false"><?php esc_html_e( 'No', 'dakidarts-numerology-core' ); ?></div>
                        </div>
                        
                        <?php if ( $dakidarts_param === 'expanded' ) : ?>
                            <span class="dakidarts-param-desc">
                                <span class="dakidarts-info-dot">i</span>
                                <?php echo esc_html__( 'Tip: returns full cycles and deep diagnostic meanings.', 'dakidarts-numerology-core' ); ?>
                            </span>
                        <?php endif; ?>
                        <?php if ( $dakidarts_param === 'allow_masters' ) : ?>
                            <span class="dakidarts-param-desc">
                                <span class="dakidarts-info-dot">i</span>
                                <?php echo esc_html__( 'Tip: Enable master-number reductions (e.g. 11, 22).', 'dakidarts-numerology-core' ); ?>
                            </span>
                        <?php endif; ?>
                    <?php elseif ( in_array( $dakidarts_param, [ 'zodiac_sign', 'sign_a', 'sign_b' ], true ) ) : ?>
                        <!-- Zodiac Sign Dropdown -->
                        <select id="<?php echo esc_attr( $dakidarts_param ); ?>" name="<?php echo esc_attr( $dakidarts_param ); ?>" <?php echo $dakidarts_required ? 'required' : ''; ?>>
                            <option value=""><?php esc_html_e( '-- Select Zodiac Sign --', 'dakidarts-numerology-core' ); ?></option>
                            <?php 
                            $signs = [ 'aries', 'taurus', 'gemini', 'cancer', 'leo', 'virgo', 'libra', 'scorpio', 'sagittarius', 'capricorn', 'aquarius', 'pisces' ];
                            foreach ( $signs as $s ) :
                            ?>
                                <option value="<?php echo esc_attr( $s ); ?>"><?php echo esc_html( ucfirst( $s ) ); ?></option>
                            <?php endforeach; ?>
                        </select>
                    <?php elseif ( in_array( $dakidarts_param, [ 'planet_name', 'planet' ], true ) ) : ?>
                        <!-- Planets Dropdown -->
                        <select id="<?php echo esc_attr( $dakidarts_param ); ?>" name="<?php echo esc_attr( $dakidarts_param ); ?>" <?php echo $dakidarts_required ? 'required' : ''; ?>>
                            <option value=""><?php esc_html_e( '-- Select Planet --', 'dakidarts-numerology-core' ); ?></option>
                            <?php 
                            $planets = [ 'solar', 'mercury', 'venus', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto' ];
                            foreach ( $planets as $p ) :
                            ?>
                                <option value="<?php echo esc_attr( $p ); ?>"><?php echo esc_html( ucfirst( $p ) ); ?></option>
                            <?php endforeach; ?>
                        </select>
                    <?php elseif ( $dakidarts_param === 'num_cards' ) : ?>
                        <!-- Tarot Cards Dropdown -->
                        <select id="<?php echo esc_attr( $dakidarts_param ); ?>" name="<?php echo esc_attr( $dakidarts_param ); ?>" <?php echo $dakidarts_required ? 'required' : ''; ?>>
                            <option value="1">1 Card</option>
                            <option value="2">2 Cards</option>
                            <option value="3" selected>3 Cards</option>
                        </select>
                    <?php elseif ( $dakidarts_param === 'house_number' ) : ?>
                        <!-- House Dropdown -->
                        <select id="<?php echo esc_attr( $dakidarts_param ); ?>" name="<?php echo esc_attr( $dakidarts_param ); ?>" <?php echo $dakidarts_required ? 'required' : ''; ?>>
                            <option value=""><?php esc_html_e( '-- Select House --', 'dakidarts-numerology-core' ); ?></option>
                            <?php for ( $i = 1; $i <= 12; $i++ ) : ?>
                                <option value="<?php echo $i; ?>"><?php echo sprintf( esc_html__( 'House %d', 'dakidarts-numerology-core' ), $i ); ?></option>
                            <?php endfor; ?>
                        </select>
                    <?php else : ?>
                        <?php 
                        // Intelligent input placeholders
                        $placeholder = '';
                        if ( strpos($dakidarts_param, 'year') !== false ) {
                            $placeholder = 'e.g. 1994';
                        } elseif ( strpos($dakidarts_param, 'month') !== false ) {
                            $placeholder = 'MM (01-12)';
                        } elseif ( strpos($dakidarts_param, 'day') !== false ) {
                            $placeholder = 'DD (01-31)';
                        } elseif ( strpos($dakidarts_param, 'dob') !== false ) {
                            $placeholder = 'YYYY-MM-DD';
                        } elseif ( strpos($dakidarts_param, 'name') !== false ) {
                            $placeholder = 'Enter full birth name...';
                        }
                        ?>
                        <input type="text" id="<?php echo esc_attr( $dakidarts_param ); ?>" name="<?php echo esc_attr( $dakidarts_param ); ?>" placeholder="<?php echo esc_attr( $placeholder ); ?>" <?php echo $dakidarts_required ? 'required' : ''; ?> />
                    <?php endif; ?>
                    
                    <?php if ( ! empty( $dakidarts_param_desc[ $dakidarts_param ] ) ) : ?>
                        <span class="dakidarts-param-desc"><?php echo esc_html( $dakidarts_param_desc[ $dakidarts_param ] ); ?></span>
                    <?php endif; ?>
                </p>
                <?php
            };

            $is_split = $dakidarts_is_compatibility || in_array( $dakidarts_endpoint, [
                'relationship-composite',
                'relationship-healing-reading',
                'soul-connection-indicator',
                'parent-child-numerology',
                'horoscope-compat-love',
                'horoscope-compat-friendship',
                'horoscope-compat-career',
                'horoscope-compat-communication',
                'horoscope-compat-lifestyle',
                'horoscope-compat-financial',
                'horoscope-compat-growth'
            ], true );

            if ( $is_split ) :
                $dakidarts_person1 = array_filter( $dakidarts_params, function($p) {
                    return str_ends_with( $p, '_1' ) || str_ends_with( $p, '_a' ) || str_starts_with( $p, 'parent_' );
                });
                $dakidarts_person2 = array_filter( $dakidarts_params, function($p) {
                    return str_ends_with( $p, '_2' ) || str_ends_with( $p, '_b' ) || str_starts_with( $p, 'child_' );
                });
                $other_params = array_diff( $dakidarts_params, $dakidarts_person1, $dakidarts_person2 );
                ?>
                <div class="dakidarts-compatibility-grid">
                    <div class="dakidarts-compatibility-card">
                        <h3>✨ <?php 
                            if ( strpos( $dakidarts_endpoint, 'parent-child' ) !== false ) {
                                esc_html_e( 'Parent Profile', 'dakidarts-numerology-core' );
                            } else {
                                esc_html_e( 'First Soul', 'dakidarts-numerology-core' );
                            }
                        ?></h3>
                        <?php foreach ( $dakidarts_person1 as $dakidarts_param ) { $dakidarts_render_field( $dakidarts_param ); } ?>
                    </div>
                    <div class="dakidarts-compatibility-card">
                        <h3>✨ <?php 
                            if ( strpos( $dakidarts_endpoint, 'parent-child' ) !== false ) {
                                esc_html_e( 'Child Profile', 'dakidarts-numerology-core' );
                            } else {
                                esc_html_e( 'Second Soul', 'dakidarts-numerology-core' );
                            }
                        ?></h3>
                        <?php foreach ( $dakidarts_person2 as $dakidarts_param ) { $dakidarts_render_field( $dakidarts_param ); } ?>
                    </div>
                </div>
                <?php if ( ! empty( $other_params ) ) : ?>
                    <div class="dakidarts-fields-container">
                        <?php foreach ( $other_params as $dakidarts_param ) { $dakidarts_render_field( $dakidarts_param ); } ?>
                    </div>
                <?php endif; ?>
            <?php else : ?>
                <div class="dakidarts-fields-container">
                    <?php foreach ( $dakidarts_params as $dakidarts_param ) { $dakidarts_render_field( $dakidarts_param ); } ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>

        <input type="hidden" name="dakidarts_endpoint" value="<?php echo esc_attr( $dakidarts_endpoint ); ?>" />
        
        <div class="dakidarts-submit-wrapper" style="margin-top: 1.5rem;">
            <button type="submit" name="dakidarts_submit" class="dakidarts-submit" <?php echo $dakidarts_button_mode === 'custom' ? 'style="' . esc_attr( $dakidarts_button_inline_style ) . '"' : ''; ?>>
                <?php echo esc_html( $dakidarts_button_text ); ?>
            </button>
        </div>

        <div class="dakidarts-powered-by" style="text-align: center; margin-top: 1.25rem; font-size: 0.72rem; opacity: 0.5; font-family: var(--space-font, inherit); transition: opacity 0.25s ease; letter-spacing: 0.03em; text-transform: uppercase;">
            <a href="https://numerologyapi.com" target="_blank" rel="noopener" style="color: inherit; text-decoration: none; font-weight: 500; display: inline-flex; align-items: center; gap: 3px;">
                <span>powered by</span>
                <strong style="font-weight: 700; border-bottom: 1px dotted currentColor;">The Numerology API</strong>
            </a>
        </div>
    </form>

    <!-- Dynamic Target Result Container underneath the form, processed via AJAX -->
    <div class="dakidarts-result" style="display: none;"></div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const container = document.querySelector('.dakidarts-form-<?php echo esc_attr( $dakidarts_template ); ?>').closest('.dakidarts-form-wrapper');
    if (!container) return;

    // Toggle segment interactive bindings
    container.querySelectorAll('.dakidarts-toggle-wrapper').forEach(wrapper => {
        const hiddenInput = wrapper.querySelector('.dakidarts-toggle-hidden');
        const options = wrapper.querySelectorAll('.dakidarts-toggle-option');
        
        options.forEach(option => {
            option.addEventListener('click', function() {
                options.forEach(opt => opt.classList.remove('active'));
                this.classList.add('active');
                
                const val = this.getAttribute('data-value');
                hiddenInput.value = val;
                
                // Dispatch change event to update validation state
                hiddenInput.dispatchEvent(new Event('change', { bubbles: true }));
            });
        });
    });
});
</script>
