package wallet

import (
	"context"
	"hyapp/pkg/appcode"
	"hyapp/pkg/xerr"
	"hyapp/services/wallet-service/internal/domain/ledger"
	"strings"
)

// ListRechargeBills 返回充值账单只读列表；所有充值来源必须先在 wallet-service 落充值记录。
func (s *Service) ListRechargeBills(ctx context.Context, query ledger.ListRechargeBillsQuery) ([]ledger.RechargeBill, int64, error) {
	if s.repository == nil {
		return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	query.AppCode = appcode.Normalize(query.AppCode)
	ctx = appcode.WithContext(ctx, query.AppCode)

	return s.repository.ListRechargeBills(ctx, query)
}

// ListRechargeProducts 返回区域化充值档位；region_id 必须由 gateway 从 user-service 资料解析。
func (s *Service) ListRechargeProducts(ctx context.Context, userID int64, regionID int64, platform string, audienceType string) ([]ledger.RechargeProduct, []string, error) {
	if userID <= 0 || regionID <= 0 {
		return nil, nil, xerr.New(xerr.InvalidArgument, "user_id and region_id are required")
	}
	if strings.TrimSpace(platform) != "" {
		platform = ledger.NormalizeRechargeProductPlatform(platform)
		if platform == "" {
			return nil, nil, xerr.New(xerr.InvalidArgument, "platform is invalid")
		}
	}
	audienceType = ledger.NormalizeRechargeAudienceType(audienceType)
	if audienceType == "" {
		return nil, nil, xerr.New(xerr.InvalidArgument, "audience_type is invalid")
	}
	if s.repository == nil {
		return nil, nil, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	ctx = appcode.WithContext(ctx, appcode.FromContext(ctx))
	return s.repository.ListRechargeProducts(ctx, userID, regionID, platform, audienceType)
}

// ListAdminRechargeProducts 返回后台配置列表；后台入口负责 RBAC 和审计。
func (s *Service) ListAdminRechargeProducts(ctx context.Context, query ledger.ListRechargeProductsQuery) ([]ledger.RechargeProduct, int64, error) {
	if s.repository == nil {
		return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	query.AppCode = appcode.Normalize(query.AppCode)
	if strings.TrimSpace(query.Status) != "" {
		query.Status = ledger.NormalizeRechargeProductStatus(query.Status)
		if query.Status == "" {
			return nil, 0, xerr.New(xerr.InvalidArgument, "status is invalid")
		}
	}
	if strings.TrimSpace(query.Platform) != "" {
		query.Platform = ledger.NormalizeRechargeProductPlatform(query.Platform)
		if query.Platform == "" {
			return nil, 0, xerr.New(xerr.InvalidArgument, "platform is invalid")
		}
	}
	if strings.TrimSpace(query.AudienceType) != "" {
		query.AudienceType = ledger.NormalizeRechargeAudienceType(query.AudienceType)
		if query.AudienceType == "" {
			return nil, 0, xerr.New(xerr.InvalidArgument, "audience_type is invalid")
		}
	}
	ctx = appcode.WithContext(ctx, query.AppCode)
	return s.repository.ListAdminRechargeProducts(ctx, query)
}

// CreateRechargeProduct 创建内购商品配置；首版充值资源只允许发 COIN。
func (s *Service) CreateRechargeProduct(ctx context.Context, command ledger.RechargeProductCommand) (ledger.RechargeProduct, error) {
	if s.repository == nil {
		return ledger.RechargeProduct{}, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	command.AppCode = appcode.Normalize(command.AppCode)
	if err := validateRechargeProductCommand(command, false); err != nil {
		return ledger.RechargeProduct{}, err
	}
	ctx = appcode.WithContext(ctx, command.AppCode)
	return s.repository.CreateRechargeProduct(ctx, command)
}

// UpdateRechargeProduct 覆盖内购商品配置和支持区域，避免状态散落在后台库。
func (s *Service) UpdateRechargeProduct(ctx context.Context, command ledger.RechargeProductCommand) (ledger.RechargeProduct, error) {
	if s.repository == nil {
		return ledger.RechargeProduct{}, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	command.AppCode = appcode.Normalize(command.AppCode)
	if err := validateRechargeProductCommand(command, true); err != nil {
		return ledger.RechargeProduct{}, err
	}
	ctx = appcode.WithContext(ctx, command.AppCode)
	return s.repository.UpdateRechargeProduct(ctx, command)
}

// DeleteRechargeProduct 删除尚未接入 provider order 的配置事实；订单实现后这里需要先做引用检查。
func (s *Service) DeleteRechargeProduct(ctx context.Context, appCode string, productID int64) error {
	if productID <= 0 {
		return xerr.New(xerr.InvalidArgument, "product_id is required")
	}
	if s.repository == nil {
		return xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	ctx = appcode.WithContext(ctx, appcode.Normalize(appCode))
	return s.repository.DeleteRechargeProduct(ctx, appcode.FromContext(ctx), productID)
}

// ConfirmGooglePayment 校验 Google Play purchase token，原子入账并记录支付审计。
func (s *Service) ConfirmGooglePayment(ctx context.Context, command ledger.GooglePaymentCommand) (ledger.GooglePaymentReceipt, error) {
	command.AppCode = appcode.Normalize(command.AppCode)
	command.CommandID = strings.TrimSpace(command.CommandID)
	command.ProductCode = strings.TrimSpace(command.ProductCode)
	command.PackageName = strings.TrimSpace(command.PackageName)
	command.PurchaseToken = strings.TrimSpace(command.PurchaseToken)
	command.OrderID = strings.TrimSpace(command.OrderID)
	if command.CommandID == "" || command.UserID <= 0 || command.RegionID <= 0 || command.PackageName == "" || command.PurchaseToken == "" || (command.ProductID <= 0 && command.ProductCode == "") {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.InvalidArgument, "google payment command is incomplete")
	}
	if len(command.CommandID) > 128 || len(command.PackageName) > 256 || len(command.PurchaseToken) > 4096 || len(command.OrderID) > 128 {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.InvalidArgument, "google payment command is too long")
	}
	if s.repository == nil {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	if s.googlePlay == nil {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Unavailable, "google play client is not configured")
	}
	ctx = appcode.WithContext(ctx, command.AppCode)

	product, err := s.googleRechargeProductForPayment(ctx, command)
	if err != nil {
		return ledger.GooglePaymentReceipt{}, err
	}
	if product.Status != ledger.RechargeProductStatusActive || !product.Enabled {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Conflict, "recharge product is not active")
	}
	if product.Platform != ledger.RechargeProductPlatformAndroid || product.Channel != ledger.RechargeChannelGoogle {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.InvalidArgument, "recharge product is not google play product")
	}
	if command.ProductCode != "" && command.ProductCode != product.ProductName && command.ProductCode != product.ProductCode {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Conflict, "product_code does not match")
	}
	if !rechargeProductSupportsRegion(product, command.RegionID) {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Conflict, "recharge product is not available in user region")
	}

	purchase, err := s.googlePlay.GetProductPurchase(ctx, command.PackageName, command.PurchaseToken)
	if err != nil {
		return ledger.GooglePaymentReceipt{}, err
	}
	if purchase.PackageName == "" {
		purchase.PackageName = command.PackageName
	}
	if purchase.PackageName != command.PackageName {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Conflict, "package_name does not match")
	}
	if purchase.PurchaseState != ledger.GooglePurchaseStatePurchased {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Conflict, "google purchase is not purchased")
	}
	// 旧 App 首充曾把钱包内部 product_code 当成确认参数；上面的兼容只负责放行历史入参，
	// 真正的 Google 商品事实仍以 purchase.ProductID 对齐 product_name，避免内部编码绕过支付商品校验。
	if purchase.ProductID != "" && purchase.ProductID != product.ProductName {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Conflict, "google product_id does not match recharge product")
	}
	if command.OrderID != "" && purchase.OrderID != "" && command.OrderID != purchase.OrderID {
		return ledger.GooglePaymentReceipt{}, xerr.New(xerr.Conflict, "google order_id does not match")
	}

	receipt, err := s.repository.ConfirmGooglePayment(ctx, command, product, purchase)
	if err != nil {
		return ledger.GooglePaymentReceipt{}, err
	}
	if receipt.ConsumeState == ledger.PaymentConsumeStateConsumed {
		return receipt, nil
	}
	consumeProductID := product.ProductCode
	if purchase.ProductID != "" {
		consumeProductID = purchase.ProductID
	}
	if err := s.googlePlay.ConsumeProduct(ctx, command.PackageName, consumeProductID, command.PurchaseToken); err != nil {
		return receipt, nil
	}
	if err := s.repository.MarkGooglePaymentConsumed(ctx, command.AppCode, receipt.PaymentOrderID); err != nil {
		return receipt, nil
	}
	receipt.ConsumeState = ledger.PaymentConsumeStateConsumed
	return receipt, nil
}

func (s *Service) googleRechargeProductForPayment(ctx context.Context, command ledger.GooglePaymentCommand) (ledger.RechargeProduct, error) {
	if command.ProductID > 0 {
		return s.repository.GetRechargeProduct(ctx, command.AppCode, command.ProductID)
	}
	// 首充弹窗只持有 Google 商品 ID，不再依赖 App 充值商品列表返回 product_id；
	// 这里仍然回到钱包内部商品配置取币数、金额和区域，避免客户端自报充值规格。
	return s.repository.GetGoogleRechargeProductByCode(ctx, command.AppCode, command.ProductCode)
}

func validateRechargeProductCommand(command ledger.RechargeProductCommand, requireProductID bool) error {
	if requireProductID && command.ProductID <= 0 {
		return xerr.New(xerr.InvalidArgument, "product_id is required")
	}
	if command.AmountMicro <= 0 || command.CoinAmount <= 0 || command.OperatorUserID <= 0 {
		return xerr.New(xerr.InvalidArgument, "recharge product amount and operator are required")
	}
	if strings.TrimSpace(command.ProductName) == "" || len([]rune(strings.TrimSpace(command.ProductName))) > 128 {
		return xerr.New(xerr.InvalidArgument, "product_name is invalid")
	}
	if len([]rune(strings.TrimSpace(command.Description))) > 512 {
		return xerr.New(xerr.InvalidArgument, "description is too long")
	}
	if ledger.NormalizeRechargeProductPlatform(command.Platform) == "" {
		return xerr.New(xerr.InvalidArgument, "platform is invalid")
	}
	if ledger.NormalizeRechargeAudienceType(command.AudienceType) == "" {
		return xerr.New(xerr.InvalidArgument, "audience_type is invalid")
	}
	if len(command.RegionIDs) == 0 {
		return xerr.New(xerr.InvalidArgument, "region_ids are required")
	}
	seen := make(map[int64]struct{}, len(command.RegionIDs))
	for _, regionID := range command.RegionIDs {
		if regionID <= 0 {
			return xerr.New(xerr.InvalidArgument, "region_id is invalid")
		}
		if _, ok := seen[regionID]; ok {
			return xerr.New(xerr.InvalidArgument, "region_id is duplicated")
		}
		seen[regionID] = struct{}{}
	}
	return nil
}

func rechargeProductSupportsRegion(product ledger.RechargeProduct, regionID int64) bool {
	for _, productRegionID := range product.RegionIDs {
		if productRegionID == regionID {
			return true
		}
	}
	return false
}
