package merchant

import (
	"context"
	"fmt"
	"kami/api/commonApi"
	"kami/api/merchant/v1"
	"kami/internal/model"
	"kami/internal/service"
	"kami/utility/config"

	"github.com/duke-git/lancet/v2/pointer"
	"github.com/duke-git/lancet/v2/slice"
)

func (c *ControllerV1) MerchantDeployList(ctx context.Context, req *v1.MerchantDeployListReq) (res *v1.MerchantDeployListRes, err error) {
	list, _ := service.MerchantDeployInfo().GetListByUid(ctx, req.MerchantUid)
	resList := make([]*v1.MerchantDeployRecord, 0)
	merchantInfo, _ := service.Merchant().GetMerchantByUId(ctx, req.MerchantUid)
	addrCfg := config.NewConfig(ctx).GetAddrCfg()

	slice.ForEach(list, func(index int, item *model.MerchantDeployRecord) {
		//查询通道信息
		roadInfo, _ := service.Road().GetDetailByUid(ctx, item.SingleRoadUid)
		item.MerchantDeployRecord.RoadSetting = fmt.Sprintf(
			"对接信息\n\n"+
				"%s\n"+
				"账户：%s\n"+
				"密码：\n\n"+
				"AppKey：%s\n"+
				"AppSecret：%s\n\n"+
				"key = thisis32bitlongpassphraseimusing\n"+
				"iv = 1234567890123456\n\n"+
				"%s通道编码：【%s】\n\n"+
				"文档在商户端登录右上角\n\n"+
				"请求支付页面：%s/?sign=xxx", addrCfg.MerchantAddr, merchantInfo.LoginAccount, merchantInfo.MerchantKey,
			merchantInfo.MerchantSecret, merchantInfo.SinglePayForRoadName, roadInfo.ProductCode, addrCfg.ShopAddr,
		)
		if !pointer.IsNil(item) && item.Id > 0 {
			resList = append(resList, &item.MerchantDeployRecord)
		}
	})
	res = &v1.MerchantDeployListRes{
		CommonDataRes: commonApi.CommonDataRes[*v1.MerchantDeployRecord]{
			List: resList,
		},
	}
	return
}
