package controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"youngee_b_api/app/dao"
	"youngee_b_api/app/entity"
	"youngee_b_api/app/service/review_service"
	"youngee_b_api/app/vo"
)

type AccountController struct{}

// 营业执照OCR识别
func (t AccountController) OCRIdentify(c *gin.Context) {
	param := &vo.IdentifyParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reviewService := review_service.GetConfig()
	resultMap, err := reviewService.CheckBusinessLicense(param)
	if err != nil {
		type ErrorResponse struct {
			StatusCode                  int    `json:"status_code"`
			RequestID                   string `json:"request_id"`
			ErrorCode                   string `json:"error_code"`
			ErrorMessage                string `json:"error_message"`
			EncodedAuthorizationMessage string `json:"encoded_authorization_message"`
		}
		var errorResp ErrorResponse
		// 解析 JSON 字符串
		err1 := json.Unmarshal([]byte(err.Error()), &errorResp)
		if err1 != nil {
			fmt.Printf("Error parsing JSON: %v\n", err)
			returnError(c, 40000, err.Error())
			return
		}
		logrus.Errorf("[OCRIdentify] call Show err:%+v\n", err)
		returnError(c, 40000, errorResp.ErrorMessage)
		return
	}
	returnSuccess(c, 20000, resultMap)
}

// 策略配置-自动执行&违约规则
func (t AccountController) StrategyAuto(c *gin.Context) {
	param := &vo.InfoAutoParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	autoTaskInfoLast := dao.InfoAutoTaskDao{}.GetAutoTaskLast(param.EnterpriseId)
	autoTaskInfoNew := entity.InfoAutoTask{
		EnterpriseID:                 param.EnterpriseId,
		ReviewAuto:                   autoTaskInfoLast.ReviewAuto,
		LinkReviewAuto:               autoTaskInfoLast.LinkReviewAuto,
		TaskFinishAuto:               autoTaskInfoLast.TaskFinishAuto,
		Invalid:                      autoTaskInfoLast.Invalid,
		SelectionInvalid:             autoTaskInfoLast.SelectionInvalid,
		DraftDefault:                 autoTaskInfoLast.DraftDefault,
		DraftCooperateProjectDefault: autoTaskInfoLast.DraftCooperateProjectDefault,
		DraftStoreDefault:            autoTaskInfoLast.DraftStoreDefault,
		DraftCooperateLocalDefault:   autoTaskInfoLast.DraftCooperateLocalDefault,
		LinkBreach:                   autoTaskInfoLast.LinkBreach,
		CaseCloseDefault:             autoTaskInfoLast.CaseCloseDefault,

		ReviewAutoOperator:                   autoTaskInfoLast.ReviewAutoOperator,
		LinkReviewAutoOperator:               autoTaskInfoLast.LinkReviewAutoOperator,
		TaskFinishAutoOperator:               autoTaskInfoLast.TaskFinishAutoOperator,
		InvalidOperator:                      autoTaskInfoLast.InvalidOperator,
		SelectionInvalidOperator:             autoTaskInfoLast.SelectionInvalidOperator,
		DraftDefaultOperator:                 autoTaskInfoLast.DraftDefaultOperator,
		DraftCooperateProjectDefaultOperator: autoTaskInfoLast.DraftCooperateProjectDefaultOperator,
		DraftStoreDefaultOperator:            autoTaskInfoLast.DraftStoreDefaultOperator,
		DraftCooperateLocalDefaultOperator:   autoTaskInfoLast.DraftCooperateLocalDefaultOperator,
		LinkBreachOperator:                   autoTaskInfoLast.LinkBreachOperator,
		CaseCloseDefaultOperator:             autoTaskInfoLast.CaseCloseDefaultOperator,
	}
	if param.ReviewAuto != 0 {
		autoTaskInfoNew.ReviewAuto = param.ReviewAuto
		autoTaskInfoNew.ReviewAutoOperator = param.EnterpriseId
	}
	if param.LinkReviewAuto != 0 {
		autoTaskInfoNew.LinkReviewAuto = param.LinkReviewAuto
		autoTaskInfoNew.LinkReviewAutoOperator = param.EnterpriseId
	}
	if param.TaskFinishAuto != 0 {
		autoTaskInfoNew.TaskFinishAuto = param.TaskFinishAuto
		autoTaskInfoNew.TaskFinishAutoOperator = param.EnterpriseId
	}
	if param.Invalid != 0 {
		autoTaskInfoNew.Invalid = param.Invalid
		autoTaskInfoNew.InvalidOperator = param.EnterpriseId
	}
	if param.SelectionInvalid != 0 {
		autoTaskInfoNew.SelectionInvalid = param.SelectionInvalid
		autoTaskInfoNew.SelectionInvalidOperator = param.EnterpriseId
	}
	if param.DraftDefault != 0 {
		autoTaskInfoNew.DraftDefault = param.DraftDefault
		autoTaskInfoNew.DraftDefaultOperator = param.EnterpriseId
	}
	if param.DraftCooperateProjectDefault != 0 {
		autoTaskInfoNew.DraftCooperateProjectDefault = param.DraftCooperateProjectDefault
		autoTaskInfoNew.DraftCooperateProjectDefaultOperator = param.EnterpriseId
	}
	if param.DraftStoreDefault != 0 {
		autoTaskInfoNew.DraftStoreDefault = param.DraftStoreDefault
		autoTaskInfoNew.DraftStoreDefaultOperator = param.EnterpriseId
	}
	if param.DraftCooperateLocalDefault != 0 {
		autoTaskInfoNew.DraftCooperateLocalDefault = param.DraftCooperateLocalDefault
		autoTaskInfoNew.DraftCooperateLocalDefaultOperator = param.EnterpriseId
	}
	if param.LinkBreach != 0 {
		autoTaskInfoNew.LinkBreach = param.LinkBreach
		autoTaskInfoNew.LinkBreachOperator = param.EnterpriseId
	}
	if param.CaseCloseDefault != 0 {
		autoTaskInfoNew.CaseCloseDefault = param.CaseCloseDefault
		autoTaskInfoNew.CaseCloseDefaultOperator = param.EnterpriseId
	}
	err1 := dao.InfoAutoTaskDao{}.Insert(autoTaskInfoNew)
	if err1 != nil {
		logrus.Errorf("[StrategyAuto] call Insert err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}

	returnSuccess(c, 20000, nil)
}

// 策略配置-违约扣款
func (t AccountController) StrategyDefaultPayment(c *gin.Context) {
	param := &vo.InfoAutoDefaultParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	autoDefaultInfoLast := dao.InfoAutoDefaultDao{}.GetAutoDefaultLast(param.EnterpriseId)
	infoAutoDefaultNew := entity.InfoAutoDefault{
		EnterpriseID:         param.EnterpriseId,
		SketchOtherTimeOut:   autoDefaultInfoLast.SketchOtherTimeOut,
		SketchOtherNotUpload: autoDefaultInfoLast.SketchOtherNotUpload,
		LinkReplaceTimeOut:   autoDefaultInfoLast.LinkReplaceTimeOut,
		LinkReplaceNotUpload: autoDefaultInfoLast.LinkReplaceNotUpload,
		DataReplaceTimeOut:   autoDefaultInfoLast.DataReplaceTimeOut,
		DataReplaceNotUpload: autoDefaultInfoLast.DataReplaceNotUpload,

		SketchOtherTimeOutOperator:   autoDefaultInfoLast.SketchOtherTimeOutOperator,
		SketchOtherNotUploadOperator: autoDefaultInfoLast.SketchOtherNotUploadOperator,
		LinkReplaceTimeOutOperator:   autoDefaultInfoLast.LinkReplaceTimeOutOperator,
		LinkReplaceNotUploadOperator: autoDefaultInfoLast.LinkReplaceNotUploadOperator,
		DataReplaceTimeOutOperator:   autoDefaultInfoLast.DataReplaceTimeOutOperator,
		DataReplaceNotUploadOperator: autoDefaultInfoLast.DataReplaceNotUploadOperator,
	}
	if param.SketchOtherTimeOut != 0 {
		infoAutoDefaultNew.SketchOtherTimeOut = param.SketchOtherTimeOut
		infoAutoDefaultNew.SketchOtherNotUploadOperator = param.EnterpriseId
	}
	if param.SketchOtherNotUpload != 0 {
		infoAutoDefaultNew.SketchOtherNotUpload = param.SketchOtherNotUpload
		infoAutoDefaultNew.SketchOtherNotUploadOperator = param.EnterpriseId
	}
	if param.LinkReplaceTimeOut != 0 {
		infoAutoDefaultNew.LinkReplaceTimeOut = param.LinkReplaceTimeOut
		infoAutoDefaultNew.LinkReplaceTimeOutOperator = param.EnterpriseId
	}
	if param.LinkReplaceNotUpload != 0 {
		infoAutoDefaultNew.LinkReplaceNotUpload = param.LinkReplaceNotUpload
		infoAutoDefaultNew.LinkReplaceNotUploadOperator = param.EnterpriseId
	}
	if param.DataReplaceTimeOut != 0 {
		infoAutoDefaultNew.DataReplaceTimeOut = param.DataReplaceTimeOut
		infoAutoDefaultNew.DataReplaceTimeOutOperator = param.EnterpriseId
	}
	if param.DataReplaceNotUpload != 0 {
		infoAutoDefaultNew.DataReplaceNotUpload = param.DataReplaceNotUpload
		infoAutoDefaultNew.DataReplaceNotUploadOperator = param.EnterpriseId
	}
	err1 := dao.InfoAutoDefaultDao{}.Insert(infoAutoDefaultNew)
	if err1 != nil {
		logrus.Errorf("[StrategyDefaultPayment] call Insert err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}

	returnSuccess(c, 20000, nil)
}

// 策略配置-自动执行&违约规则 详情
func (t AccountController) StrategyAutoDetail(c *gin.Context) {
	param := &vo.InfoAutoParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	autoTaskInfo := dao.InfoAutoTaskDao{}.GetAutoTaskLast(param.EnterpriseId)
	reInfoAuto := vo.ReInfoAuto{
		ReviewAuto:                           autoTaskInfo.ReviewAuto,
		LinkReviewAuto:                       autoTaskInfo.LinkReviewAuto,
		TaskFinishAuto:                       autoTaskInfo.TaskFinishAuto,
		Invalid:                              autoTaskInfo.Invalid,
		SelectionInvalid:                     autoTaskInfo.SelectionInvalid,
		DraftDefault:                         autoTaskInfo.DraftDefault,
		DraftCooperateProjectDefault:         autoTaskInfo.DraftCooperateProjectDefault,
		DraftStoreDefault:                    autoTaskInfo.DraftStoreDefault,
		DraftCooperateLocalDefault:           autoTaskInfo.DraftCooperateLocalDefault,
		LinkBreach:                           autoTaskInfo.LinkBreach,
		CaseCloseDefault:                     autoTaskInfo.CaseCloseDefault,
		ReviewAutoOperator:                   "样叽系统",
		LinkReviewAutoOperator:               "样叽系统",
		TaskFinishAutoOperator:               "样叽系统",
		InvalidOperator:                      "样叽系统",
		SelectionInvalidOperator:             "样叽系统",
		DraftDefaultOperator:                 "样叽系统",
		DraftCooperateProjectDefaultOperator: "样叽系统",
		DraftStoreDefaultOperator:            "样叽系统",
		DraftCooperateLocalDefaultOperator:   "样叽系统",
		LinkBreachOperator:                   "样叽系统",
		CaseCloseDefaultOperator:             "样叽系统",
	}
	var businessName string
	enterprise, err1 := dao.EnterpriseDao{}.GetEnterprise(autoTaskInfo.EnterpriseID)
	if err1 != nil && enterprise != nil {
		businessName = enterprise.BusinessName
	}
	if autoTaskInfo.ReviewAutoOperator != "0" {
		reInfoAuto.ReviewAutoOperator = businessName
	}
	if autoTaskInfo.LinkReviewAutoOperator != "0" {
		reInfoAuto.LinkReviewAutoOperator = businessName
	}
	if autoTaskInfo.TaskFinishAutoOperator != "0" {
		reInfoAuto.TaskFinishAutoOperator = businessName
	}
	if autoTaskInfo.InvalidOperator != "0" {
		reInfoAuto.InvalidOperator = businessName
	}
	if autoTaskInfo.SelectionInvalidOperator != "0" {
		reInfoAuto.SelectionInvalidOperator = businessName
	}
	if autoTaskInfo.DraftDefaultOperator != "0" {
		reInfoAuto.DraftDefaultOperator = businessName
	}
	if autoTaskInfo.DraftCooperateProjectDefaultOperator != "0" {
		reInfoAuto.DraftCooperateProjectDefaultOperator = businessName
	}
	if autoTaskInfo.DraftStoreDefaultOperator != "0" {
		reInfoAuto.DraftStoreDefaultOperator = businessName
	}
	if autoTaskInfo.DraftCooperateLocalDefaultOperator != "0" {
		reInfoAuto.DraftCooperateLocalDefaultOperator = businessName
	}
	if autoTaskInfo.LinkBreachOperator != "0" {
		reInfoAuto.LinkBreachOperator = businessName
	}
	if autoTaskInfo.CaseCloseDefaultOperator != "0" {
		reInfoAuto.CaseCloseDefaultOperator = businessName
	}

	returnSuccess(c, 20000, reInfoAuto)
}

// 策略配置-违约扣款 详情
func (t AccountController) StrategyDefaultPaymentPaymentDetail(c *gin.Context) {
	param := &vo.InfoAutoDefaultParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	autoDefaultInfo := dao.InfoAutoDefaultDao{}.GetAutoDefaultLast(param.EnterpriseId)
	reInfoAutoDefault := vo.ReInfoAutoDefault{
		SketchOtherTimeOut:           autoDefaultInfo.SketchOtherTimeOut,
		SketchOtherNotUpload:         autoDefaultInfo.SketchOtherNotUpload,
		LinkReplaceTimeOut:           autoDefaultInfo.LinkReplaceTimeOut,
		LinkReplaceNotUpload:         autoDefaultInfo.LinkReplaceNotUpload,
		DataReplaceTimeOut:           autoDefaultInfo.DataReplaceTimeOut,
		DataReplaceNotUpload:         autoDefaultInfo.DataReplaceNotUpload,
		SketchOtherTimeOutOperator:   "样叽系统",
		SketchOtherNotUploadOperator: "样叽系统",
		LinkReplaceTimeOutOperator:   "样叽系统",
		LinkReplaceNotUploadOperator: "样叽系统",
		DataReplaceTimeOutOperator:   "样叽系统",
		DataReplaceNotUploadOperator: "样叽系统",
	}
	var businessName string
	enterprise, err1 := dao.EnterpriseDao{}.GetEnterprise(autoDefaultInfo.EnterpriseID)
	if err1 != nil && enterprise != nil {
		businessName = enterprise.BusinessName
	}
	if autoDefaultInfo.SketchOtherTimeOutOperator != "0" {
		reInfoAutoDefault.SketchOtherTimeOutOperator = businessName
	}
	if autoDefaultInfo.SketchOtherNotUploadOperator != "0" {
		reInfoAutoDefault.SketchOtherNotUploadOperator = businessName
	}
	if autoDefaultInfo.LinkReplaceTimeOutOperator != "0" {
		reInfoAutoDefault.LinkReplaceTimeOutOperator = businessName
	}
	if autoDefaultInfo.LinkReplaceNotUploadOperator != "0" {
		reInfoAutoDefault.LinkReplaceNotUploadOperator = businessName
	}
	if autoDefaultInfo.DataReplaceTimeOutOperator != "0" {
		reInfoAutoDefault.DataReplaceTimeOutOperator = businessName
	}
	if autoDefaultInfo.DataReplaceNotUploadOperator != "0" {
		reInfoAutoDefault.DataReplaceNotUploadOperator = businessName
	}

	returnSuccess(c, 20000, reInfoAutoDefault)
}
