package walletapiimp

import (
	"encoding/json"
	"net/http"
	"strings"

	"github.com/google/uuid"
	"github.com/tech/sendico/pkg/api/http/response"
	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/model"
	"github.com/tech/sendico/pkg/mservice"
	"github.com/tech/sendico/pkg/mutil/mzap"
	describablev1 "github.com/tech/sendico/pkg/proto/common/describable/v1"
	chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
	"github.com/tech/sendico/server/interface/api/srequest"
	"github.com/tech/sendico/server/interface/api/sresponse"
	mutil "github.com/tech/sendico/server/internal/mutil/param"
	ast "github.com/tech/sendico/server/internal/mutil/proto"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.uber.org/zap"
)

func (a *WalletAPI) create(r *http.Request, account *model.Account, token *sresponse.TokenData) http.HandlerFunc {
	orgRef, err := a.oph.GetRef(r)
	if err != nil {
		a.logger.Warn("Failed to parse organization reference for wallet list", zap.Error(err), zap.String(a.oph.Name(), a.oph.GetID(r)))
		return response.BadReference(a.logger, a.Name(), a.oph.Name(), a.oph.GetID(r), err)
	}

	var sr srequest.CreateWallet
	if err := json.NewDecoder(r.Body).Decode(&sr); err != nil {
		a.logger.Warn("Failed to decode wallet creation request request", zap.Error(err), mzap.StorableRef(account))
		return response.BadPayload(a.logger, a.Name(), err)
	}

	ctx := r.Context()
	res, err := a.enf.Enforce(ctx, a.walletsPermissionRef, account.ID, orgRef, primitive.NilObjectID, model.ActionCreate)
	if err != nil {
		a.logger.Warn("Failed to check chain wallet access permissions", zap.Error(err), mutil.PLog(a.oph, r), mzap.StorableRef(account))
		return response.Auto(a.logger, a.Name(), err)
	}
	if !res {
		a.logger.Debug("Access denied when listing organization wallets", mutil.PLog(a.oph, r), mzap.StorableRef(account))
		return response.AccessDenied(a.logger, a.Name(), "wallets creation permission denied")
	}

	asset, err := a.assets.Resolve(ctx, sr.Asset)
	if err != nil {
		a.logger.Warn("Failed to resolve asset", zap.Error(err), mzap.StorableRef(account),
			zap.String("chain", string(sr.Asset.Chain)), zap.String("token", sr.Asset.TokenSymbol))
		return response.Auto(a.logger, a.Name(), err)
	}

	if a.chainGateway == nil {
		return response.Internal(a.logger, mservice.ChainGateway, merrors.Internal("chain gateway client is not configured"))
	}

	var ownerRef string
	if !sr.IsOrgWallet {
		ownerRef = account.ID.Hex()
	}
	passet, err := ast.Asset2Proto(&asset.Asset)
	if err != nil {
		a.logger.Warn("Failed to convert asset to proto asset", zap.Error(err),
			mzap.StorableRef(asset), mzap.StorableRef(account))
		return response.Auto(a.logger, a.Name(), err)
	}

	req := &chainv1.CreateManagedWalletRequest{
		IdempotencyKey:  uuid.NewString(),
		OrganizationRef: orgRef.Hex(),
		OwnerRef:        ownerRef,
		Describable: &describablev1.Describable{
			Name:        sr.Description.Name,
			Description: sr.Description.Description,
		},
		Asset: passet,
		Metadata: map[string]string{
			"source": "create",
			"login":  account.Login,
		},
	}

	resp, err := a.chainGateway.CreateManagedWallet(ctx, req)
	if err != nil {
		a.logger.Warn("Failed to create managed wallet", zap.Error(err), mzap.ObjRef("organization_ref", orgRef), mzap.StorableRef(account))
		return response.Auto(a.logger, a.Name(), err)
	}
	if resp == nil || resp.Wallet == nil || strings.TrimSpace(resp.Wallet.WalletRef) == "" {
		return response.Auto(a.logger, a.Name(), merrors.Internal("chain gateway returned empty wallet reference"))
	}

	a.logger.Info("Managed wallet created for organization", mzap.ObjRef("organization_ref", orgRef),
		zap.String("wallet_ref", resp.Wallet.WalletRef), mzap.StorableRef(account))

	return sresponse.Success(a.logger, token)
}
