package payment

import (
	"strings"

	"github.com/perfect-panel/server/internal/types"
)

type Platform int

const (
	Stripe Platform = iota
	AlipayF2F
	EPay
	Balance
	CryptoSaaS
	AppleIAP
	UNSUPPORTED Platform = -1
)

const (
	platformNameStripe      = "Stripe"
	platformNameAlipayF2F   = "AlipayF2F"
	platformNameEPay        = "EPay"
	platformNameBalance     = "balance"
	platformNameCryptoSaaS  = "CryptoSaaS"
	platformNameAppleIAP    = "AppleIAP"
	platformNameUnsupported = "unsupported"
)

var platformAliasToType = map[string]Platform{
	"stripe":      Stripe,
	"alipayf2f":   AlipayF2F,
	"alipay_f2f":  AlipayF2F,
	"epay":        EPay,
	"balance":     Balance,
	"cryptosaas":  CryptoSaaS,
	"crypto_saas": CryptoSaaS,
	"appleiap":    AppleIAP,
	"apple_iap":   AppleIAP,
}

func (p Platform) String() string {
	switch p {
	case Stripe:
		return platformNameStripe
	case AlipayF2F:
		return platformNameAlipayF2F
	case EPay:
		return platformNameEPay
	case Balance:
		return platformNameBalance
	case CryptoSaaS:
		return platformNameCryptoSaaS
	case AppleIAP:
		return platformNameAppleIAP
	default:
		return platformNameUnsupported
	}
}

func ParsePlatform(s string) Platform {
	normalized := normalizePlatformAlias(s)
	if p, ok := platformAliasToType[normalized]; ok {
		return p
	}
	compact := strings.ReplaceAll(normalized, "_", "")
	if p, ok := platformAliasToType[compact]; ok {
		return p
	}
	return UNSUPPORTED
}

func CanonicalPlatformName(input string) (string, bool) {
	platform := ParsePlatform(input)
	if platform == UNSUPPORTED {
		return "", false
	}
	return platform.String(), true
}

func normalizePlatformAlias(input string) string {
	normalized := strings.ToLower(strings.TrimSpace(input))
	normalized = strings.ReplaceAll(normalized, "-", "_")
	normalized = strings.ReplaceAll(normalized, " ", "")
	return normalized
}

func GetSupportedPlatforms() []types.PlatformInfo {
	return []types.PlatformInfo{
		{
			Platform:    Stripe.String(),
			PlatformUrl: "https://stripe.com",
			PlatformFieldDescription: map[string]string{
				"public_key":     "Publishable key",
				"secret_key":     "Secret key",
				"webhook_secret": "Webhook secret",
				"payment":        "Payment Method, only supported card/alipay/wechat_pay",
			},
		},
		{
			Platform:    AlipayF2F.String(),
			PlatformUrl: "https://alipay.com",
			PlatformFieldDescription: map[string]string{
				"app_id":       "App ID",
				"private_key":  "Private Key",
				"public_key":   "Public Key",
				"invoice_name": "Invoice Name",
				"sandbox":      "Sandbox Mode",
			},
		},
		{
			Platform:    EPay.String(),
			PlatformUrl: "",
			PlatformFieldDescription: map[string]string{
				"pid":  "PID",
				"url":  "URL",
				"key":  "Key",
				"type": "Type",
			},
		},
		{
			Platform:    CryptoSaaS.String(),
			PlatformUrl: "https://t.me/CryptoSaaSBot",
			PlatformFieldDescription: map[string]string{
				"endpoint":   "API Endpoint",
				"account_id": "Account ID",
				"secret_key": "Secret Key",
			},
		},
		{
			Platform:    AppleIAP.String(),
			PlatformUrl: "https://developer.apple.com/in-app-purchase/",
			PlatformFieldDescription: map[string]string{
				"product_ids": "Product IDs (comma separated)",
				"key_id":      "Key ID",
				"issuer_id":   "Issuer ID",
				"private_key": "Private Key (.p8 content)",
				"sandbox":     "Sandbox Mode",
			},
		},
	}
}
