<?php

namespace fluXtore\Core\Settings;

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

if ( ! class_exists( 'fluXtore_Gateway_Settings' ) ) {
        class fluXtore_Gateway_Settings {
                const OPTION_KLARNA   = 'gateway_klarna_enabled';
                const OPTION_AFTERPAY = 'gateway_afterpay_enabled';
                const OPTION_MOLLIE   = 'gateway_mollie_enabled';
                const OPTION_RAZORPAY = 'gateway_razorpay_enabled';

                /**
                 * @var self|null
                 */
                private static $instance = null;

                /**
                 * Cached gateway definitions.
                 *
                 * @var array<string,array<string,mixed>>
                 */
                private static $definitions = [];

                private function __construct() {}

                public static function init() {
                        if ( null !== self::$instance ) {
                                return;
                        }

                        self::$instance = new self();

                        add_action( 'fluxtore_register_options', [ self::$instance, 'add_options' ] );

                        add_filter( 'fluxtore_settings_' . self::OPTION_KLARNA . '_save', [ self::$instance, 'sanitize_toggle' ] );
                        add_filter( 'fluxtore_settings_' . self::OPTION_AFTERPAY . '_save', [ self::$instance, 'sanitize_toggle' ] );
                        add_filter( 'fluxtore_settings_' . self::OPTION_MOLLIE . '_save', [ self::$instance, 'sanitize_toggle' ] );
                        add_filter( 'fluxtore_settings_' . self::OPTION_RAZORPAY . '_save', [ self::$instance, 'sanitize_toggle' ] );
                }

                public function add_options() {
                        $definitions = self::get_gateway_definitions();

                        foreach ( $definitions as $definition ) {
                                $section = isset( $definition['section'] ) ? $definition['section'] : 'gateways_priority';

                                fluXtore_Settings::register_option(
                                        'gateways',
                                        $definition['option'],
                                        [
                                                'type'        => 'bool',
                                                'label'       => $definition['label'],
                                                'description' => isset( $definition['description'] ) ? $definition['description'] : '',
                                                'section'     => $section,
                                                'default'     => $this->get_bool_default( $definition['option'], false ),
                                        ]
                                );
                        }
                }

                private function get_bool_default( $option, $default = false ) {
                        $raw_default = fluXtore_Settings::get( $option, $default ? '1' : '0' );

                        if ( is_bool( $raw_default ) ) {
                                return $raw_default;
                        }

                        if ( is_numeric( $raw_default ) ) {
                                return (bool) intval( $raw_default );
                        }

                        if ( is_string( $raw_default ) ) {
                                $filtered = filter_var( $raw_default, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );

                                if ( null !== $filtered ) {
                                        return $filtered;
                                }
                        }

                        return (bool) $default;
                }

                public function sanitize_toggle( $value ) {
                        if ( is_string( $value ) ) {
                                $value = trim( $value );
                        }

                        return (bool) filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
                }

                /**
                 * Retrieve gateway definitions used across admin, checkout, and upsell flows.
                 *
                 * @return array<string,array<string,mixed>>
                 */
                public static function get_gateway_definitions() {
                        if ( ! empty( self::$definitions ) ) {
                                return self::$definitions;
                        }

                        $gateways = [
                                'klarna'   => [
                                        'id'          => 'klarna',
                                        'option'      => self::OPTION_KLARNA,
                                        'label'       => __( 'Enable Klarna Pay Later buttons', 'fluxtore' ),
                                        'description' => __( 'Surface Klarna Pay Later / Slice It on funnel checkouts when the shopper is eligible.', 'fluxtore' ),
                                        'plugin'      => 'klarna-payments-for-woocommerce/klarna-payments-for-woocommerce.php',
                                        'method_ids'  => [ 'klarna_payments', 'klarna_pay_now', 'klarna_pay_later', 'klarna_slice_it' ],
                                        'countries'   => [ 'AT', 'BE', 'DE', 'DK', 'ES', 'FI', 'FR', 'GB', 'IE', 'IT', 'NL', 'NO', 'SE', 'US' ],
                                        'currencies'  => [ 'AUD', 'CAD', 'CHF', 'DKK', 'EUR', 'GBP', 'NOK', 'SEK', 'USD' ],
                                        'min_total'   => 10,
                                        'max_total'   => 1500,
                                        'type'        => 'bnpl',
                                        'section'     => 'gateways_bnpl',
                                ],
                                'afterpay' => [
                                        'id'          => 'afterpay',
                                        'option'      => self::OPTION_AFTERPAY,
                                        'label'       => __( 'Enable Afterpay/Clearpay messaging', 'fluxtore' ),
                                        'description' => __( 'Highlights Afterpay (Clearpay) installments on supported currencies to encourage higher conversions.', 'fluxtore' ),
                                        'plugin'      => 'afterpay-gateway-for-woocommerce/afterpay-gateway-for-woocommerce.php',
                                        'method_ids'  => [ 'afterpay', 'afterpay_up', 'clearpay' ],
                                        'countries'   => [ 'AU', 'CA', 'FR', 'GB', 'IT', 'ES', 'NZ', 'US' ],
                                        'currencies'  => [ 'AUD', 'CAD', 'EUR', 'GBP', 'NZD', 'USD' ],
                                        'min_total'   => 1,
                                        'max_total'   => 2000,
                                        'type'        => 'bnpl',
                                        'section'     => 'gateways_bnpl',
                                ],
                                'mollie'   => [
                                        'id'          => 'mollie',
                                        'option'      => self::OPTION_MOLLIE,
                                        'label'       => __( 'Enable Mollie components (including Klarna & in3)', 'fluxtore' ),
                                        'description' => __( 'Shows Mollie wallet buttons such as Klarna Pay Later, in3 or Apple Pay when the Mollie gateway is available.', 'fluxtore' ),
                                        'plugin'      => 'mollie-payments-for-woocommerce/mollie-payments-for-woocommerce.php',
                                        'method_ids'  => [ 'mollie_wc_gateway_applepay', 'mollie_wc_gateway_klarna', 'mollie_wc_gateway_in3', 'mollie_wc_gateway_przelewy24' ],
                                        'countries'   => [ 'AT', 'BE', 'DE', 'DK', 'FI', 'FR', 'IE', 'IT', 'LU', 'NL', 'NO', 'PT', 'ES', 'SE' ],
                                        'currencies'  => [ 'EUR' ],
                                        'min_total'   => 1,
                                        'max_total'   => 3000,
                                        'type'        => 'wallet',
                                        'section'     => 'gateways_wallets',
                                ],
                                'razorpay' => [
                                        'id'          => 'razorpay',
                                        'option'      => self::OPTION_RAZORPAY,
                                        'label'       => __( 'Enable Razorpay UPI & PayLater prompts', 'fluxtore' ),
                                        'description' => __( 'Expose Razorpay UPI, PayLater and Card EMI options for Indian customers directly inside the funnel checkout.', 'fluxtore' ),
                                        'plugin'      => 'woo-razorpay/razorpay.php',
                                        'method_ids'  => [ 'razorpay', 'razorpay_upi', 'razorpay_paylater', 'razorpay_emipayment' ],
                                        'countries'   => [ 'IN' ],
                                        'currencies'  => [ 'INR' ],
                                        'min_total'   => 1,
                                        'max_total'   => 200000,
                                        'type'        => 'wallet',
                                        'section'     => 'gateways_wallets',
                                ],
                        ];

                        if ( function_exists( 'apply_filters' ) ) {
                                $gateways = apply_filters( 'fluxtore_gateway_definitions', $gateways );
                        }

                        self::$definitions = $gateways;

                        return self::$definitions;
                }
        }
}

fluXtore_Gateway_Settings::init();
