package apple

import (
	"context"
	"encoding/json"
	"strings"

	"github.com/perfect-panel/server/internal/model/payment"
	"github.com/perfect-panel/server/internal/model/user"
	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/internal/types"
	"github.com/perfect-panel/server/pkg/constant"
	iapapple "github.com/perfect-panel/server/pkg/iap/apple"
	"github.com/perfect-panel/server/pkg/logger"
	"github.com/perfect-panel/server/pkg/xerr"
	"github.com/pkg/errors"
)

type AttachTransactionByIdLogic struct {
	logger.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAttachTransactionByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AttachTransactionByIdLogic {
	return &AttachTransactionByIdLogic{
		Logger: logger.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AttachTransactionByIdLogic) AttachById(req *types.AttachAppleTransactionByIdRequest) (*types.AttachAppleTransactionResponse, error) {
	l.Infow("attach by transaction id start", logger.Field("orderNo", req.OrderNo), logger.Field("transactionId", req.TransactionId))
	u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
	if !ok || u == nil {
		l.Errorw("attach by id invalid access")
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "invalid access")
	}
	ord, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
	if err != nil {
		l.Errorw("attach by id order not exist", logger.Field("orderNo", req.OrderNo))
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.OrderNotExist), "order not exist")
	}
	pay, err := l.svcCtx.PaymentModel.FindOne(l.ctx, ord.PaymentId)
	if err != nil {
		l.Errorw("attach by id payment not found", logger.Field("paymentId", ord.PaymentId))
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.PaymentMethodNotFound), "payment not found")
	}
	hasKey := false
	if pay.Config != "" && (strings.Contains(pay.Config, "-----BEGIN PRIVATE KEY-----") || strings.Contains(pay.Config, "BEGIN PRIVATE KEY")) {
		hasKey = true
	}
	l.Infow("attach by id payment config meta", logger.Field("paymentId", pay.Id), logger.Field("platform", pay.Platform), logger.Field("config_len", len(pay.Config)), logger.Field("has_private_key", hasKey))
	if pay.Config == "" {
		l.Errorw("attach by id iap config empty", logger.Field("paymentId", pay.Id))
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "iap config is empty")
	}
	var cfg payment.AppleIAPConfig
	if err := cfg.Unmarshal([]byte(pay.Config)); err != nil {
		l.Errorw("attach by id iap config error", logger.Field("error", err.Error()), logger.Field("paymentId", pay.Id), logger.Field("platform", pay.Platform), logger.Field("config_len", len(pay.Config)), logger.Field("has_private_key", hasKey))
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "iap config error")
	}
	apiCfg := iapapple.ServerAPIConfig{
		KeyID:      cfg.KeyID,
		IssuerID:   cfg.IssuerID,
		PrivateKey: cfg.PrivateKey,
		Sandbox:    cfg.Sandbox,
	}

	// Try to extract BundleID from productIds (if available in config) or custom data
	// For now, we leave it empty unless we find it in config, but we can try to parse from payment config if needed.
	// However, ServerAPIConfig update allows optional BundleID.

	if req.Sandbox != nil {
		apiCfg.Sandbox = *req.Sandbox
	}

	// Try to fix PEM format if it is missing newlines (common issue)
	if !strings.Contains(apiCfg.PrivateKey, "\n") && strings.Contains(apiCfg.PrivateKey, "BEGIN PRIVATE KEY") {
		apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, " ", "\n")
		apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, "-----BEGIN\nPRIVATE\nKEY-----", "-----BEGIN PRIVATE KEY-----")
		apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, "-----END\nPRIVATE\nKEY-----", "-----END PRIVATE KEY-----")
	}

	if apiCfg.KeyID == "" || apiCfg.IssuerID == "" || apiCfg.PrivateKey == "" {
		l.Errorw("attach by id credential missing", logger.Field("keyID", apiCfg.KeyID), logger.Field("issuerID", apiCfg.IssuerID), logger.Field("hasPrivateKey", apiCfg.PrivateKey != ""))
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "apple server api credential missing")
	}

	// Try to get BundleID from Site CustomData if not set
	if apiCfg.BundleID == "" {
		var customData struct {
			IapBundleId string `json:"iapBundleId"`
		}
		if l.svcCtx.Config.Site.CustomData != "" {
			_ = json.Unmarshal([]byte(l.svcCtx.Config.Site.CustomData), &customData)
			apiCfg.BundleID = customData.IapBundleId
		}
	}

	jws, err := iapapple.GetTransactionInfo(apiCfg, req.TransactionId)
	if err != nil {
		l.Errorw("fetch transaction info error", logger.Field("error", err.Error()))
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "fetch transaction info error")
	}
	// reuse existing attach logic with JWS
	attach := NewAttachTransactionLogic(l.ctx, l.svcCtx)
	resp, e := attach.Attach(&types.AttachAppleTransactionRequest{
		SignedTransactionJWS: jws,
		SubscribeId:          0,
		DurationDays:         0,
		Tier:                 "",
		OrderNo:              req.OrderNo,
	})
	if e != nil {
		l.Errorw("attach by id commit error", logger.Field("error", e.Error()))
		return nil, e
	}
	l.Infow("attach by transaction id ok", logger.Field("orderNo", req.OrderNo), logger.Field("transactionId", req.TransactionId), logger.Field("expiresAt", resp.ExpiresAt))
	return resp, nil
}
