import type { ExecutionMethod, PaymentMethod, PaymentProvider } from "./enums.js"

export type PaymentProviderInputValue = string | number | boolean | null
export type PaymentProviderInputMap = Record<string, PaymentProviderInputValue>

/**
 * Déclare quels moyens de paiement un provider supporte,
 * et pour chacun, quelles méthodes d'exécution sont disponibles.
 */
export interface ProviderSupportedMethodDto {
    method: PaymentMethod
    executionMethods: ExecutionMethod[]
}

/**
 * @deprecated Utiliser supportedMethods à la place
 */
export interface PaymentProviderCapabilitiesDto {
    supportsPaymentSearch?: boolean
    supportsRefunds?: boolean
    supportsReaderFlow?: boolean
}

export interface PaymentProviderFieldOptionDto {
    label: string
    value: string
}

export interface PaymentProviderFieldDefinitionDto {
    key: string
    label: string
    type: "text" | "password" | "boolean" | "select"
    required?: boolean
    secret?: boolean
    placeholder?: string
    description?: string
    options?: PaymentProviderFieldOptionDto[]
}

export interface PaymentProviderDefinitionDto {
    provider: PaymentProvider
    label: string
    description?: string
    /** @deprecated Utiliser supportedMethods */
    capabilities?: PaymentProviderCapabilitiesDto
    /** Méthodes de paiement supportées avec leurs méthodes d'exécution */
    supportedMethods: ProviderSupportedMethodDto[]
    fields: PaymentProviderFieldDefinitionDto[]
}

/**
 * Moyens de paiement globaux ne nécessitant aucun provider.
 * Toujours disponibles.
 */
export const GLOBAL_PAYMENT_METHODS: ProviderSupportedMethodDto[] = [
    { method: "CASH",    executionMethods: ["MANUAL"] },
    { method: "CHECK",   executionMethods: ["MANUAL"] },
    { method: "VOUCHER", executionMethods: ["MANUAL"] },
    { method: "GIFT_CARD", executionMethods: ["MANUAL"] },
]

/**
 * Méthodes de paiement qui sont techniquement distinctes mais équivalentes
 * pour le vendeur/caissier. Le terminal (reader/tap to pay) gère tout automatiquement.
 *
 * - CARD = paiement bancaire (carte physique / sans contact)
 * - APPLE_PAY = via Apple Pay (NFC)
 * - GOOGLE_PAY = via Google Pay (NFC)
 *
 * Côté config et POS, on expose uniquement "CARD".
 * Les enums APPLE_PAY/GOOGLE_PAY restent pour le logging post-transaction
 * (le provider retourne le type réel après paiement).
 */
export const CARD_GROUP_METHODS: PaymentMethod[] = ["CARD", "APPLE_PAY", "GOOGLE_PAY"]

/**
 * Labels lisibles pour les ExecutionMethods
 */
export const EXECUTION_METHOD_LABELS: Record<ExecutionMethod, string> = {
    MANUAL: "Validation directe",
    READER: "Terminal de paiement",
    TAP_TO_PAY: "Tap To Pay (NFC)",
    HOSTED: "Lien de paiement",
    QR_CODE: "QR Code"
}

/**
 * Labels lisibles pour les PaymentMethods
 */
export const PAYMENT_METHOD_LABELS: Record<PaymentMethod, string> = {
    CASH: "Espèces",
    CARD: "Paiement bancaire",
    CHECK: "Chèque",
    APPLE_PAY: "Apple Pay",
    GOOGLE_PAY: "Google Pay",
    GIFT_CARD: "Carte cadeau",
    VOUCHER: "Bon / Voucher"
}

export const PAYMENT_PROVIDER_DEFINITIONS: Record<PaymentProvider, PaymentProviderDefinitionDto> = {
    SUMUP: {
        provider: "SUMUP",
        label: "SumUp",
        description: "Compte SumUp avec support reader (TPE Solo) ou Tap To Pay.",
        supportedMethods: [
            { method: "CARD", executionMethods: ["READER", "TAP_TO_PAY"] },
        ],
        fields: [
            {
                key: "merchantCode",
                label: "Merchant code",
                type: "text",
                required: true,
                placeholder: "MCXXXXXX"
            },
            {
                key: "apiSecretKey",
                label: "Api Secret Key",
                type: "password",
                required: true,
                secret: true,
                placeholder: "sumup_live_..."
            },
            {
                key: "webhookUrl",
                label: "Webhook URL (base)",
                type: "text",
                description: "URL de base publique du serveur (ex: ngrok). Le path /sumup/webhook sera ajoute automatiquement.",
                placeholder: "https://your-domain.com"
            }
        ]
    },
    STRIPE: {
        provider: "STRIPE",
        label: "Stripe",
        description: "Clés API Stripe (publishable + secret) avec support readers Terminal côté admin/diagnostic.",
        supportedMethods: [
            { method: "CARD", executionMethods: ["READER"] },
        ],
        fields: [
            {
                key: "secretKey",
                label: "Secret key",
                type: "password",
                required: true,
                secret: true,
                placeholder: "sk_live_..."
            },
            {
                key: "publishableKey",
                label: "Publishable key",
                type: "text",
                placeholder: "pk_live_..."
            },
            {
                key: "webhookSecret",
                label: "Webhook secret",
                type: "password",
                secret: true,
                placeholder: "whsec_..."
            }
        ]
    },
    SQUARE: {
        provider: "SQUARE",
        label: "Square",
        description: "Configuration Square pour paiements et terminaux.",
        supportedMethods: [
            { method: "CARD", executionMethods: ["READER"] },
        ],
        fields: [
            {
                key: "accessToken",
                label: "Access token",
                type: "password",
                required: true,
                secret: true,
                placeholder: "sq0atp-..."
            },
            {
                key: "locationId",
                label: "Location ID",
                type: "text",
                required: true,
                placeholder: "LXXXXXXXXXXXX"
            }
        ]
    },
    ADYEN: {
        provider: "ADYEN",
        label: "Adyen",
        description: "Compte Adyen avec merchant account et cle API.",
        supportedMethods: [
            { method: "CARD", executionMethods: ["READER"] },
        ],
        fields: [
            {
                key: "merchantAccount",
                label: "Merchant account",
                type: "text",
                required: true,
                placeholder: "MyMerchantAccount"
            },
            {
                key: "apiKey",
                label: "API key",
                type: "password",
                required: true,
                secret: true,
                placeholder: "AQE..."
            },
            {
                key: "clientKey",
                label: "Client key",
                type: "text",
                placeholder: "pub.v2..."
            }
        ]
    }
}
