<?php
/**
 * Dakidarts Numerology Core - Admin Style Customizations
 *
 * Allows admins to customize frontend form and button styles.
 *
 * @package Dakidarts_Numerology_Core
 */

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

class Dakidarts_Admin_Style {

    private $option_key = 'dakidarts_numerology_style_settings';

    public function __construct() {
        add_action( 'admin_menu', [ $this, 'add_admin_submenu' ] );
        add_action( 'admin_init', [ $this, 'register_settings' ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_color_picker' ] );
        add_action( 'admin_notices', [ $this, 'render_notices' ] );
    }

    /**
     * Add submenu page under main plugin menu
     */
    public function add_admin_submenu() {
        add_submenu_page(
            'dakidarts-numerology-core',
            __( 'Style & Customization', 'dakidarts-numerology-core' ),
            __( 'Style', 'dakidarts-numerology-core' ),
            'manage_options',
            'dakidarts-admin-style',
            [ $this, 'render' ]
        );
    }

    /**
     * Enqueue WP color picker assets
     */
    public function enqueue_color_picker( $hook_suffix ) {
        // Only enqueue on our plugin style page
        if ( $hook_suffix !== 'dakidarts-numerology-core_page_dakidarts-admin-style' ) {
            return;
        }

        wp_enqueue_style( 'wp-color-picker' );
        wp_enqueue_script( 'wp-color-picker' );
    }

    /**
     * Register settings group
     */
    public function register_settings() {
        register_setting(
            'dakidarts_style_group',
            $this->option_key,
            [ $this, 'sanitize_settings' ]
        );

        add_action( 'update_option_' . $this->option_key, [ $this, 'settings_saved' ], 10, 2 );
    }

    public function settings_saved( $old_value, $new_value ) {
        set_transient( 'dakidarts_admin_notice', [
            'message' => __( 'Style settings saved successfully!', 'dakidarts-numerology-core' ),
            'type'    => 'success'
        ], 30 );
    }

    /**
 * Sanitize admin inputs
 */
public function sanitize_settings( $input ) {
    $sanitized = [];

    // Colors
    $color_keys = ['form_bg', 'button_bg', 'button_color', 'button_border_color'];
    foreach ( $color_keys as $key ) {
        $sanitized[ $key ] = isset( $input[ $key ] ) ? sanitize_hex_color( $input[ $key ] ) : '';
    }

    // Sizes & Spacing + Texts
    $text_keys = [
        'form_padding', 'button_padding', 'form_margin', 'button_margin',
        'button_radius', 'button_border_width',
        'calculate_text', 'try_again_text' // ✅ NEW
    ];
    foreach ( $text_keys as $key ) {
        $sanitized[ $key ] = isset( $input[ $key ] ) ? sanitize_text_field( $input[ $key ] ) : '';
    }

    // Button border style (must be valid CSS border style)
    $valid_styles = ['solid','dashed','dotted','double','groove','ridge','none'];
    $sanitized['button_border_style'] = isset( $input['button_border_style'] ) && in_array( $input['button_border_style'], $valid_styles, true )
        ? $input['button_border_style']
        : 'solid';

    // Button position
    $valid_positions = ['inherit','left','center','right'];
    $sanitized['button_position'] = isset( $input['button_position'] ) && in_array( $input['button_position'], $valid_positions, true )
        ? $input['button_position']
        : 'inherit';

    return $sanitized;
}

/**
 * Render admin page
 */
public function render() {
    $options = get_option( $this->option_key, [] );
    ?>
    <div class="dakidarts-admin-page dakidarts-admin-style">
        <div class="dakidarts-hero dakidarts-hero--compact">
            <div class="dakidarts-hero__content">
                <div class="dakidarts-hero__eyebrow">
                    <span class="dakidarts-pill"><i class="fa-solid fa-palette"></i> <?php esc_html_e( 'Style Studio', 'dakidarts-numerology-core' ); ?></span>
                </div>
                <h1><?php esc_html_e( 'Style & Customization', 'dakidarts-numerology-core' ); ?></h1>
                <p><?php esc_html_e( 'Control the look and feel of your numerology forms and call-to-action buttons.', 'dakidarts-numerology-core' ); ?></p>
            </div>
        </div>

        <form method="post" action="options.php" class="dakidarts-form">
            <?php settings_fields( 'dakidarts_style_group' ); ?>
            <div class="dakidarts-grid">
                <div class="dakidarts-card">
                    <div class="dakidarts-card__header">
                        <h2><i class="fa-solid fa-square"></i> <?php esc_html_e( 'Form Appearance', 'dakidarts-numerology-core' ); ?></h2>
                        <p><?php esc_html_e( 'Define the background, padding, and spacing for your forms.', 'dakidarts-numerology-core' ); ?></p>
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Form Background Color', 'dakidarts-numerology-core' ); ?></label>
                        <input type="text"
                               class="dakidarts-color-picker"
                               name="<?php echo esc_attr($this->option_key); ?>[form_bg]"
                               value="<?php echo esc_attr($options['form_bg'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Form Padding (px)', 'dakidarts-numerology-core' ); ?></label>
                        <input type="number"
                               name="<?php echo esc_attr($this->option_key); ?>[form_padding]"
                               value="<?php echo esc_attr($options['form_padding'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Form Margin (px)', 'dakidarts-numerology-core' ); ?></label>
                        <input type="number"
                               name="<?php echo esc_attr($this->option_key); ?>[form_margin]"
                               value="<?php echo esc_attr($options['form_margin'] ?? ''); ?>" />
                    </div>
                </div>

                <div class="dakidarts-card">
                    <div class="dakidarts-card__header">
                        <h2><i class="fa-solid fa-bolt"></i> <?php esc_html_e( 'Button Styling', 'dakidarts-numerology-core' ); ?></h2>
                        <p><?php esc_html_e( 'Fine-tune button colors, borders, and alignment.', 'dakidarts-numerology-core' ); ?></p>
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Background Color', 'dakidarts-numerology-core' ); ?></label>
                        <input type="text"
                               class="dakidarts-color-picker"
                               name="<?php echo esc_attr($this->option_key); ?>[button_bg]"
                               value="<?php echo esc_attr($options['button_bg'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Text Color', 'dakidarts-numerology-core' ); ?></label>
                        <input type="text"
                               class="dakidarts-color-picker"
                               name="<?php echo esc_attr($this->option_key); ?>[button_color]"
                               value="<?php echo esc_attr($options['button_color'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Border Color', 'dakidarts-numerology-core' ); ?></label>
                        <input type="text"
                               class="dakidarts-color-picker"
                               name="<?php echo esc_attr($this->option_key); ?>[button_border_color]"
                               value="<?php echo esc_attr($options['button_border_color'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Padding (px)', 'dakidarts-numerology-core' ); ?></label>
                        <input type="number"
                               name="<?php echo esc_attr($this->option_key); ?>[button_padding]"
                               value="<?php echo esc_attr($options['button_padding'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Margin (px)', 'dakidarts-numerology-core' ); ?></label>
                        <input type="number"
                               name="<?php echo esc_attr($this->option_key); ?>[button_margin]"
                               value="<?php echo esc_attr($options['button_margin'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Radius (px)', 'dakidarts-numerology-core' ); ?></label>
                        <input type="number"
                               name="<?php echo esc_attr($this->option_key); ?>[button_radius]"
                               value="<?php echo esc_attr($options['button_radius'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Border Width (px)', 'dakidarts-numerology-core' ); ?></label>
                        <input type="number"
                               name="<?php echo esc_attr($this->option_key); ?>[button_border_width]"
                               value="<?php echo esc_attr($options['button_border_width'] ?? ''); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Border Style', 'dakidarts-numerology-core' ); ?></label>
                        <select name="<?php echo esc_attr($this->option_key); ?>[button_border_style]">
                            <?php
                            $styles = ['solid','dashed','dotted','double','groove','ridge','none'];
                            foreach ( $styles as $style ) {
                                printf(
                                    '<option value="%s" %s>%s</option>',
                                    esc_attr($style),
                                    selected( $options['button_border_style'] ?? 'solid', $style, false ),
                                    esc_html( ucfirst($style) )
                                );
                            }
                            ?>
                        </select>
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Button Position', 'dakidarts-numerology-core' ); ?></label>
                        <select name="<?php echo esc_attr($this->option_key); ?>[button_position]">
                            <?php
                            $positions = ['inherit','left','center','right'];
                            foreach ( $positions as $pos ) {
                                printf(
                                    '<option value="%s" %s>%s</option>',
                                    esc_attr($pos),
                                    selected( $options['button_position'] ?? 'inherit', $pos, false ),
                                    esc_html( ucfirst($pos) )
                                );
                            }
                            ?>
                        </select>
                    </div>
                </div>

                <div class="dakidarts-card">
                    <div class="dakidarts-card__header">
                        <h2><i class="fa-solid fa-pen-nib"></i> <?php esc_html_e( 'Button Text', 'dakidarts-numerology-core' ); ?></h2>
                        <p><?php esc_html_e( 'Customize the text shown on your primary actions.', 'dakidarts-numerology-core' ); ?></p>
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Calculate Button Text', 'dakidarts-numerology-core' ); ?></label>
                        <input type="text"
                               name="<?php echo esc_attr($this->option_key); ?>[calculate_text]"
                               value="<?php echo esc_attr($options['calculate_text'] ?? 'Calculate'); ?>"
                               placeholder="<?php esc_attr_e('e.g. Calculate', 'dakidarts-numerology-core'); ?>" />
                    </div>

                    <div class="dakidarts-field">
                        <label class="dakidarts-label"><?php esc_html_e( 'Try Again Button Text', 'dakidarts-numerology-core' ); ?></label>
                        <input type="text"
                               name="<?php echo esc_attr($this->option_key); ?>[try_again_text]"
                               value="<?php echo esc_attr($options['try_again_text'] ?? 'Try Again'); ?>"
                               placeholder="<?php esc_attr_e('e.g. Try Again', 'dakidarts-numerology-core'); ?>" />
                    </div>
                </div>
            </div>

            <div class="dakidarts-card dakidarts-card--footer">
                <button type="submit" class="button button-primary dakidarts-btn dakidarts-btn--primary">
                    <i class="fa-solid fa-floppy-disk"></i> <?php esc_html_e( 'Save Styles', 'dakidarts-numerology-core' ); ?>
                </button>
            </div>
        </form>
    </div>

    <script>
        jQuery(document).ready(function($){
            $('.dakidarts-color-picker').wpColorPicker();
        });
    </script>
    <?php
}

public function render_notices() {
    $notice = get_transient( 'dakidarts_admin_notice' );
    if ( ! $notice ) {
        return;
    }

    delete_transient( 'dakidarts_admin_notice' );

    $type = $notice['type'] === 'error' ? 'error' : 'success';
    $icon = $type === 'success' ? 'fa-circle-check' : 'fa-circle-xmark';
    ?>
    <div class="dakidarts-notice dakidarts-notice--<?php echo esc_attr( $type ); ?>">
        <span class="dakidarts-notice__icon"><i class="fa-solid <?php echo esc_attr( $icon ); ?>"></i></span>
        <span class="dakidarts-notice__message"><?php echo esc_html( $notice['message'] ); ?></span>
    </div>
    <?php
}

}
