package dto

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
)

type CooperationContractItem struct {
	CooperationContractId      int               `json:"cooperationContractId,string,"`
	CooperationContractNumber  string            `json:"cooperationContractNumber"` //合约编号
	CooperationProjectNumber   string            `json:"cooperationProjectNumber"`  //项目编号
	CooperationContractName    string            `json:"CooperationContractName"`   //合约名称
	Status                     int               `json:"status"`                    //合约状态
	CreateTtime                int               `json:"createTtime"`
	Department                 domain.Department `json:"department"`
	IncentivesType             string            `json:"incentivesType"` //Incentives激励方式
	CooperationContractSponsor struct {
		UserId   int    `json:"userId,string,"`
		UserName string `json:"userName"`
	} `json:"cooperationContractSponsor"` //共创发起人
	CooperationMode domain.CooperationMode `json:"cooperationMode"` //共创模式
	Org             domain.Org             `json:"org"`             //组织结构
}

func ToCooperationContractItem(param *allied_creation_cooperation.CooperationContract) *CooperationContractItem {
	data := CooperationContractItem{
		CooperationContractId:     param.CooperationContractId,
		CooperationProjectNumber:  param.CooperationContractNumber,
		CooperationContractName:   param.CooperationContractName,
		CooperationContractNumber: param.CooperationContractNumber,
		Status:                    param.Status,
		CreateTtime:               int(param.CreatedAt.Unix()),
	}
	data.CooperationContractSponsor.UserId = param.CooperationContractSponsor.UserId
	data.CooperationContractSponsor.UserName = param.CooperationContractSponsor.UserInfo.UserName
	data.Department.DepartmentId = param.Department.DepartmentId
	data.Department.DepartmentName = param.Department.DepartmentName
	data.Org.OrgID = int(param.Org.OrgId)
	data.Org.OrgName = param.Org.OrgName
	data.CooperationMode.CooperationModeId = param.CooperationMode.CooperationModeId
	data.CooperationMode.CooperationModeName = param.CooperationMode.CooperationModeName
	data.CooperationMode.CooperationModeNumber = param.CooperationMode.CooperationModeNumber
	if len(param.DividendsIncentivesRules) > 0 {
		data.IncentivesType = "业绩分红"
	} else if len(param.MoneyIncentivesRules) > 0 {
		data.IncentivesType = "金额激励"
	}
	return &data
}

type MoneyIncentivesRule struct {
	MoneyIncentivesRuleId     int     `json:"moneyIncentivesRuleId"`
	CooperationContractNumber string  `json:"cooperationContractNumber"` // 关联的共创合约编号
	MoneyIncentivesAmount     float64 `json:"moneyIncentivesAmount"`     // 激励金额
	MoneyIncentivesStage      int     `json:"moneyIncentivesStage"`      // 金额激励阶段
	MoneyIncentivesTime       int64   `json:"moneyIncentivesTime"`       // 金额激励规则时间
	ReferrerPercentage        float64 `json:"referrerPercentage"`        // 推荐人抽成比例
	SalesmanPercentage        float64 `json:"salesmanPercentage"`        // 业务员抽成比例
}

type DividendsIncentivesRule struct {
	DividendsIncentivesRuleId     int     `json:"dividendsIncentivesRuleId"`
	CooperationContractNumber     string  `json:"cooperationContractNumber"`       // 关联的项目合约编号
	ReferrerPercentage            float64 `json:"referrerPercentage"`              // 推荐人抽成比例
	SalesmanPercentage            float64 `json:"salesmanPercentage"`              // 业务员抽成比例
	DividendsIncentivesPercentage float64 `json:"dividendsIncentivesPercentage"`   // 分红规则激励百分点
	DividendsIncentivesStage      int     `json:"dividendsIncentivesStage,string"` // 分红规则激励阶段,
	DividendsIncentivesStageEnd   int64   `json:"dividendsIncentivesStageEnd"`     // 分红规则激励阶段结束
	DividendsIncentivesStageStart int64   `json:"dividendsIncentivesStageStart"`   // 分红规则激励阶段开始
}

type Relevant struct {
	UserId     int               `json:"userId"`
	UserInfo   domain.UserInfo   `json:"userInfo"`
	Department domain.Department `json:"department"`
}

type ContractUndertaker struct {
	Attachment   []domain.Attachment `json:"attachment"`
	Department   domain.Department   `json:"department"`
	HasReferrer  bool                `json:"hasReferrer"`
	HasSalesman  bool                `json:"hasSalesman"`
	ReferrerUser domain.UserInfo     `json:"referrerUser"`
	SalesmanUser domain.UserInfo     `json:"salesmanUser"`
	UserInfo     domain.UserInfo     `json:"userInfo"`
}
type CooperationContractInfo struct {
	CooperationContract      CooperationContractItem   `json:"cooperationContract"`
	Relevant                 []Relevant                `json:"relevant"`
	DividendsIncentivesRules []DividendsIncentivesRule `json:"dividendsIncentivesRules"`
	MoneyIncentivesRules     []MoneyIncentivesRule     `json:"moneyIncentivesRules"`
	ContractUndertaker       []ContractUndertaker      `json:"contractUndertaker"`
}

func ToCooperationContractInfo(param *allied_creation_cooperation.CooperationContract) *CooperationContractInfo {

	relevants := []Relevant{}
	dividendsIncentivesRules := []DividendsIncentivesRule{}
	moneyIncentivesRules := []MoneyIncentivesRule{}
	contractUndertaker := []ContractUndertaker{}
	for _, v := range param.RelevantPeople {
		r := Relevant{
			UserId: int(v.UserId),
			UserInfo: domain.UserInfo{
				UsersName: v.UserInfo.UsersName,
				UsersId:   v.UserInfo.UsersId,
				UserCode:  v.UserInfo.UserCode,
			},
			Department: domain.Department{
				DepartmentNumber: v.Department.DepartmentNumber,
				DepartmentId:     int(v.Department.DepartmentId),
				DepartmentName:   v.Department.DepartmentName,
			}}
		relevants = append(relevants, r)
	}

	for _, v := range param.DividendsIncentivesRules {
		r := DividendsIncentivesRule{
			DividendsIncentivesRuleId:     v.DividendsIncentivesRuleId,
			CooperationContractNumber:     v.CooperationContractNumber,
			ReferrerPercentage:            v.ReferrerPercentage,
			SalesmanPercentage:            v.SalesmanPercentage,
			DividendsIncentivesPercentage: v.DividendsIncentivesPercentage,
			DividendsIncentivesStage:      v.DividendsIncentivesStage,
			DividendsIncentivesStageEnd:   v.DividendsIncentivesStageEnd.Unix(),
			DividendsIncentivesStageStart: v.DividendsIncentivesStageStart.Unix(),
		}
		dividendsIncentivesRules = append(dividendsIncentivesRules, r)
	}

	for _, v := range param.MoneyIncentivesRules {
		r := MoneyIncentivesRule{
			MoneyIncentivesRuleId:     v.MoneyIncentivesRuleId,
			CooperationContractNumber: v.CooperationContractNumber,
			MoneyIncentivesAmount:     v.MoneyIncentivesAmount,
			MoneyIncentivesStage:      v.MoneyIncentivesStage,
			MoneyIncentivesTime:       v.MoneyIncentivesTime.Unix(),
			ReferrerPercentage:        v.ReferrerPercentage,
			SalesmanPercentage:        v.SalesmanPercentage,
		}
		moneyIncentivesRules = append(moneyIncentivesRules, r)
	}

	for _, v := range param.Undertakers {
		u := ContractUndertaker{
			Attachment: v.ContractAttachment,
			Department: domain.Department{
				DepartmentNumber: v.Department.DepartmentNumber,
				DepartmentId:     int(v.Department.DepartmentId),
				DepartmentName:   v.Department.DepartmentName,
			},
			HasReferrer: v.Referrer.UserId > 0,
			HasSalesman: v.Salesman.UserId > 0,
			ReferrerUser: domain.UserInfo{
				UsersName: v.Referrer.UserInfo.UserName,
				Phone:     v.Referrer.UserInfo.UserPhone,
				UsersId:   v.Referrer.UserId,
				// UserCode :v.
			},
			SalesmanUser: domain.UserInfo{
				UsersName: v.Salesman.UserInfo.UserName,
				UsersId:   v.Salesman.UserId,
				Phone:     v.Salesman.UserInfo.UserPhone,
			},
			UserInfo: domain.UserInfo{
				UsersName: v.UserInfo.UserName,
				UsersId:   v.UserId,
				Phone:     v.UserInfo.UserPhone,
			},
		}
		contractUndertaker = append(contractUndertaker, u)
	}
	data := CooperationContractInfo{
		CooperationContract:      *ToCooperationContractItem(param),
		Relevant:                 relevants,
		DividendsIncentivesRules: dividendsIncentivesRules,
		MoneyIncentivesRules:     moneyIncentivesRules,
		ContractUndertaker:       contractUndertaker,
	}
	return &data

}

type CooperationContractUndertake struct {
	Undertaker struct {
		UsersName string `json:"userName"`
		Phone     string `json:"phone"`
		UserId    int    `json:"userId,string,"`
		UserCode  string `json:"userCode"`
	} `json:"undertaker"`
	CooperationContractDescription string `json:"cooperationContractDescription"`
	CooperationContractId          int    `json:"cooperationContractId,string,"`
	CooperationContractName        string `json:"cooperationContractName"`
	CooperationContractNumber      string `json:"cooperationContractNumber"`
	CooperationContractSponsor     struct {
		UsersName string `json:"userName"`
		Phone     string `json:"phone"`
		UserId    int    `json:"userId,string,"`
		UserCode  string `json:"userCode"`
	} `json:"cooperationContractSponsor"`
	CooperationMode struct {
		CooperationModeId     int    `json:"cooperationModeId,string"`
		CooperationModeName   string `json:"cooperationModeName"`
		CooperationModeNumber string `json:"cooperationModeNumber"`
	} `json:"cooperationMode"`
	Org struct {
		OrgName string `json:"orgName"`
	} `json:"org"`
	Attachment []domain.Attachment `json:"attachment"`
}

func ToCooperationContractUndertake(allied_creation_cooperation.DataCooperationContractSearchByUndertaker) *CooperationContractUndertake {
	data := CooperationContractUndertake{}
	return &data
}