<?php

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

class Clielo_Admin {

    public static function init(): void {
        add_action( 'admin_init', [ __CLASS__, 'register_settings' ] );
    }

    public static function add_menu(): void {
        add_submenu_page(
            'clielo',
            __( 'Clielo - Settings', 'clielo' ),
            __( 'Settings', 'clielo' ),
            'manage_options',
            'clielo-settings',
            [ __CLASS__, 'render_settings_page' ]
        );

    }

    public static function register_settings(): void {
        // CPT
        register_setting( 'clielo_settings', 'clielo_post_type', [
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => 'annonce',
        ] );

        // Couleur principale
        register_setting( 'clielo_settings', 'clielo_color', [
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_hex_color',
            'default'           => '#0073aa',
        ] );

        // Position du bouton
        register_setting( 'clielo_settings', 'clielo_position', [
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => 'bottom-right',
        ] );

        // Section générale
        add_settings_section(
            'clielo_main_section',
            __( 'General settings', 'clielo' ),
            null,
            'clielo-settings'
        );

        add_settings_field(
            'clielo_post_type',
            __( 'Custom Post Type', 'clielo' ),
            [ __CLASS__, 'render_post_type_field' ],
            'clielo-settings',
            'clielo_main_section'
        );

        // Section apparence
        add_settings_section(
            'clielo_style_section',
            __( 'Appearance', 'clielo' ),
            null,
            'clielo-settings'
        );

        add_settings_field(
            'clielo_color',
            __( 'Couleur du chat', 'clielo' ),
            [ __CLASS__, 'render_color_field' ],
            'clielo-settings',
            'clielo_style_section'
        );

        add_settings_field(
            'clielo_position',
            __( 'Position du bouton', 'clielo' ),
            [ __CLASS__, 'render_position_field' ],
            'clielo-settings',
            'clielo_style_section'
        );

        // Section internationalisation
        add_settings_section(
            'clielo_i18n_section',
            __( 'Langue', 'clielo' ),
            null,
            'clielo-settings'
        );

        register_setting( 'clielo_settings', 'clielo_locale', [
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => '',
        ] );

        add_settings_field(
            'clielo_locale',
            __( 'Locale du plugin', 'clielo' ),
            [ __CLASS__, 'render_locale_field' ],
            'clielo-settings',
            'clielo_i18n_section'
        );

        // Section devise
        register_setting( 'clielo_settings', 'clielo_currency', [
            'type'              => 'string',
            'sanitize_callback' => function ( $v ) {
                return array_key_exists( $v, self::get_currencies() ) ? $v : 'EUR';
            },
            'default'           => 'EUR',
        ] );

        add_settings_section(
            'clielo_currency_section',
            __( 'Devise', 'clielo' ),
            null,
            'clielo-settings'
        );

        add_settings_field(
            'clielo_currency',
            __( 'Devise du plugin', 'clielo' ),
            [ __CLASS__, 'render_currency_field' ],
            'clielo-settings',
            'clielo_currency_section'
        );

    }

    public static function render_post_type_field(): void {
        $current    = get_option( 'clielo_post_type', 'annonce' );
        $post_types = get_post_types( [ 'public' => true ], 'objects' );

        echo '<select name="clielo_post_type" id="clielo_post_type">';
        foreach ( $post_types as $pt ) {
            printf(
                '<option value="%s" %s>%s (%s)</option>',
                esc_attr( $pt->name ),
                selected( $current, $pt->name, false ),
                esc_html( $pt->labels->singular_name ),
                esc_html( $pt->name )
            );
        }
        echo '</select>';
        echo '<p class="description">' . esc_html__( 'Sélectionnez le CPT sur lequel activer le chat.', 'clielo' ) . '</p>';
    }

    public static function render_color_field(): void {
        $color = get_option( 'clielo_color', '#0073aa' );
        printf(
            '<input type="color" name="clielo_color" id="clielo_color" value="%s" />',
            esc_attr( $color )
        );
        echo '<p class="description">' . esc_html__( 'Couleur principale du bouton et du header du chat.', 'clielo' ) . '</p>';
    }

    public static function render_position_field(): void {
        $current = get_option( 'clielo_position', 'bottom-right' );

        $positions = [
            'bottom-right' => __( 'Bas droite', 'clielo' ),
            'bottom-left'  => __( 'Bas gauche', 'clielo' ),
            'top-right'    => __( 'Haut droite', 'clielo' ),
            'top-left'     => __( 'Haut gauche', 'clielo' ),
        ];

        echo '<select name="clielo_position" id="clielo_position">';
        foreach ( $positions as $value => $label ) {
            printf(
                '<option value="%s" %s>%s</option>',
                esc_attr( $value ),
                selected( $current, $value, false ),
                esc_html( $label )
            );
        }
        echo '</select>';
    }

    public static function render_locale_field(): void {
        $current = get_option( 'clielo_locale', '' );
        $locales = [
            ''      => __( 'Locale WordPress (automatique)', 'clielo' ),
            'fr_FR' => 'Français (fr_FR)',
            'en_US' => 'English (en_US)',
            'es_ES' => 'Español (es_ES)',
            'de_DE' => 'Deutsch (de_DE)',
            'it_IT' => 'Italiano (it_IT)',
            'pt_PT' => 'Português (pt_PT)',
        ];
        echo '<select name="clielo_locale" id="clielo_locale">';
        foreach ( $locales as $value => $label ) {
            printf(
                '<option value="%s" %s>%s</option>',
                esc_attr( $value ),
                selected( $current, $value, false ),
                esc_html( $label )
            );
        }
        echo '</select>';
        echo '<p class="description">' . esc_html__( 'Forcer une langue spécifique pour le plugin, indépendamment de la locale WordPress. Nécessite un fichier .mo dans le dossier /languages/.', 'clielo' ) . '</p>';
    }

    public static function render_settings_page(): void {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        ?>
        <div class="wrap">
            <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
            <form action="options.php" method="post">
                <?php
                settings_fields( 'clielo_settings' );
                do_settings_sections( 'clielo-settings' );
                submit_button( __( 'Enregistrer', 'clielo' ) );
                ?>
            </form>
        </div>
        <?php
    }

    public static function is_extra_pages_enabled( int $post_id = 0 ): bool {
        if ( ! $post_id ) {
            return false;
        }
        return get_post_meta( $post_id, '_clielo_extra_pages_enabled', true ) === '1';
    }

    public static function is_maintenance_enabled( int $post_id = 0 ): bool {
        if ( ! $post_id ) {
            return false;
        }
        return get_post_meta( $post_id, '_clielo_maintenance_enabled', true ) === '1';
    }

    public static function is_express_enabled( int $post_id = 0 ): bool {
        if ( ! $post_id ) {
            return false;
        }
        return get_post_meta( $post_id, '_clielo_express_enabled', true ) === '1';
    }

    public static function get_extra_pages_label( int $post_id = 0 ): string {
        $label = $post_id ? get_post_meta( $post_id, '_clielo_extra_pages_label', true ) : '';
        return $label ?: __( 'Pages supplémentaires', 'clielo' );
    }

    public static function get_maintenance_label( int $post_id = 0 ): string {
        $label = $post_id ? get_post_meta( $post_id, '_clielo_maintenance_label', true ) : '';
        return $label ?: __( 'Maintenance mensuelle', 'clielo' );
    }

    public static function get_express_label( int $post_id = 0 ): string {
        $label = $post_id ? get_post_meta( $post_id, '_clielo_express_label', true ) : '';
        return $label ?: __( 'Livraison express', 'clielo' );
    }

    public static function render_currency_field(): void {
        $current    = self::get_currency();
        $currencies = self::get_currencies();
        echo '<select name="clielo_currency" id="clielo_currency" style="min-width:320px">';
        foreach ( $currencies as $code => $data ) {
            printf(
                '<option value="%s" %s>%s</option>',
                esc_attr( $code ),
                selected( $current, $code, false ),
                esc_html( $data['label'] )
            );
        }
        echo '</select>';
        echo '<p class="description">' . esc_html__( 'Devise utilisée pour l\'affichage des prix et les factures. Doit correspondre à la devise configurée dans vos passerelles de paiement.', 'clielo' ) . '</p>';
    }

    public static function get_post_type(): string {
        return get_option( 'clielo_post_type', 'annonce' );
    }

    public static function get_color(): string {
        return get_option( 'clielo_color', '#0073aa' );
    }

    public static function get_position(): string {
        return get_option( 'clielo_position', 'bottom-right' );
    }

    public static function get_locale(): string {
        return get_option( 'clielo_locale', '' );
    }

    public static function get_currencies(): array {
        return [
            // Europe
            'EUR' => [ 'label' => 'EUR — Euro (€)',                                'symbol' => '€',    'decimals' => 2, 'position' => 'after'  ],
            'GBP' => [ 'label' => 'GBP — Livre sterling (£)',                      'symbol' => '£',    'decimals' => 2, 'position' => 'before' ],
            'CHF' => [ 'label' => 'CHF — Franc suisse',                            'symbol' => 'CHF',  'decimals' => 2, 'position' => 'after'  ],
            // Amériques
            'USD' => [ 'label' => 'USD — Dollar américain ($)',                    'symbol' => '$',    'decimals' => 2, 'position' => 'before' ],
            'CAD' => [ 'label' => 'CAD — Dollar canadien (CA$)',                   'symbol' => 'CA$',  'decimals' => 2, 'position' => 'before' ],
            'BRL' => [ 'label' => 'BRL — Réal brésilien (R$)',                     'symbol' => 'R$',   'decimals' => 2, 'position' => 'before' ],
            'MXN' => [ 'label' => 'MXN — Peso mexicain',                           'symbol' => 'MX$',  'decimals' => 2, 'position' => 'before' ],
            // Afrique
            'XOF' => [ 'label' => 'XOF — Franc CFA UEMOA (CI, SN, ML, BF, TG…)', 'symbol' => 'FCFA', 'decimals' => 0, 'position' => 'after'  ],
            'XAF' => [ 'label' => 'XAF — Franc CFA CEMAC (CM, CG, GA, TD…)',      'symbol' => 'FCFA', 'decimals' => 0, 'position' => 'after'  ],
            'GNF' => [ 'label' => 'GNF — Franc guinéen (FG)',                      'symbol' => 'FG',   'decimals' => 0, 'position' => 'after'  ],
            'CDF' => [ 'label' => 'CDF — Franc congolais (FC)',                    'symbol' => 'FC',   'decimals' => 2, 'position' => 'after'  ],
            'NGN' => [ 'label' => 'NGN — Naira nigérian (₦)',                      'symbol' => '₦',    'decimals' => 2, 'position' => 'before' ],
            'GHS' => [ 'label' => 'GHS — Cedi ghanéen (GH₵)',                     'symbol' => 'GH₵',  'decimals' => 2, 'position' => 'before' ],
            'KES' => [ 'label' => 'KES — Shilling kenyan (KSh)',                   'symbol' => 'KSh',  'decimals' => 2, 'position' => 'before' ],
            'ZAR' => [ 'label' => 'ZAR — Rand sud-africain (R)',                   'symbol' => 'R',    'decimals' => 2, 'position' => 'before' ],
            'MAD' => [ 'label' => 'MAD — Dirham marocain (DH)',                    'symbol' => 'DH',   'decimals' => 2, 'position' => 'after'  ],
            'TND' => [ 'label' => 'TND — Dinar tunisien (DT)',                     'symbol' => 'DT',   'decimals' => 3, 'position' => 'after'  ],
            'DZD' => [ 'label' => 'DZD — Dinar algérien (DA)',                     'symbol' => 'DA',   'decimals' => 2, 'position' => 'after'  ],
            'EGP' => [ 'label' => 'EGP — Livre égyptienne (E£)',                   'symbol' => 'E£',   'decimals' => 2, 'position' => 'before' ],
            'MGA' => [ 'label' => 'MGA — Ariary malgache (Ar)',                    'symbol' => 'Ar',   'decimals' => 2, 'position' => 'after'  ],
            // Moyen-Orient / Asie
            'AED' => [ 'label' => 'AED — Dirham émirati',                          'symbol' => 'AED',  'decimals' => 2, 'position' => 'after'  ],
            'SAR' => [ 'label' => 'SAR — Riyal saoudien',                          'symbol' => 'SAR',  'decimals' => 2, 'position' => 'after'  ],
            'INR' => [ 'label' => 'INR — Roupie indienne (₹)',                     'symbol' => '₹',    'decimals' => 2, 'position' => 'before' ],
            'JPY' => [ 'label' => 'JPY — Yen japonais (¥)',                        'symbol' => '¥',    'decimals' => 0, 'position' => 'before' ],
            'CNY' => [ 'label' => 'CNY — Yuan chinois (¥)',                        'symbol' => '¥',    'decimals' => 2, 'position' => 'before' ],
            'AUD' => [ 'label' => 'AUD — Dollar australien (A$)',                  'symbol' => 'A$',   'decimals' => 2, 'position' => 'before' ],
            'SGD' => [ 'label' => 'SGD — Dollar de Singapour (S$)',                'symbol' => 'S$',   'decimals' => 2, 'position' => 'before' ],
        ];
    }

    public static function get_currency(): string {
        return get_option( 'clielo_currency', 'EUR' );
    }

    public static function get_currency_data(): array {
        $currencies = self::get_currencies();
        return $currencies[ self::get_currency() ] ?? $currencies['EUR'];
    }

    public static function format_price( float $amount ): string {
        $curr      = self::get_currency_data();
        $formatted = number_format( $amount, $curr['decimals'], ',', ' ' );
        return 'before' === $curr['position']
            ? esc_html( $curr['symbol'] ) . $formatted
            : $formatted . ' ' . esc_html( $curr['symbol'] );
    }
}
