package account

import (
	"encoding/json"

	"github.com/pingcap/crm-bot/crm"
	"github.com/pingcap/crm-bot/model"
	"github.com/pingcap/crm-bot/salesforce"
)

func CrmAccounts(tpl *salesforce.SFTpl, marshal bool) interface{} {
	// fxxk
	source := crm.CrmDataSource{
		ApiName:           "AccountObj",
		FieldDescFileName: "./etc/account-fielddesc.json",
		NeedFieldFileName: "./etc/account-crm.csv",
		Topic:             "",
	}
	d := crm.CrmData(&source, nil)
	for _, item := range d.Data {
		item["DataSource"] = "纷享销客"
		if !marshal {
			item["所属大区"] = "CHN"
		}
	}

	sd := salesforce.CrmData(tpl)
	for _, item := range sd {
		item["DataSource"] = "salesforce"
	}
	sd = append(sd, d.Data...)
	if marshal {
		dataBytes, err := json.Marshal(sd)
		if err != nil {
			return nil
		}

		accounts := []model.CRMAccount{}
		err = json.Unmarshal(dataBytes, &accounts)
		if err != nil {
			return nil
		}
		return accounts
	} else {
		return sd
	}
}
