package main

import (
	"encoding/json"
	"log"
)

type crmdContact struct {
	crmdEntityBase
	SalutationName   string `json:"salutationName,omitempty"`
	FirstName        string `json:"firstName,omitempty"`
	LastName         string `json:"lastName,omitempty"`
	AccountID        string `json:"accountID,omitempty"`
	Title            string `json:"title,omitempty"`
	EmailAddress     string `json:"emailAddress,omitempty"`
	PhoneNumber      string `json:"phoneNumber,omitempty"`
	DoNotCall        bool   `json:"doNotCall,omitempty"`
	AddressStreet    string `json:"addressStreet,omitempty"`
	AddressCity      string `json:"addressCity,omitempty"`
	AddressState     string `json:"addressState,omitempty"`
	AddressCountry   string `json:"addressCountry,omitempty"`
	AddresPostalCode string `json:"addressPostalcode,omitempty"`
	AccountType      string `json:"accountType,omitempty"`
	EmailAddressData []struct {
		EmailAddress string `json:"emailAddress,omitempty"`
		Lower        string `json:"lower,omitempty"`
		Primary      bool   `json:"primary,omitempty"`
		OptOut       bool   `json:"optOut,omitempty"`
		Invalid      bool   `json:"invalid,omitempty"`
	} `json:"emailAddressData,omitempty"`
	PhoneNumberData []struct {
		PhoneNumber string `json:"phoneNumber,omitempty"`
		Primary     bool   `json:"primary,omitempty"`
		Type        string `json:"type,omitempty"`
	} `json:"phoneNumberData,omitempty"`
	AccountName     string            `json:"accountName,omitempty"`
	AccountIDs      []string          `json:"accountsIds,omitempty"`
	AccountNames    map[string]string `json:"accountsNames,omitempty"`
	AccountsColumns map[string]struct {
		Role string `json:"role,omitempty"`
	} `json:"accountsColumns,omitempty"`
	CampaignID   string `json:"campaignId,omitempty"`
	CampaignName string `json:"campaignName,omitempty"`

	PortalUserID     string            `json:"portalUserId,omitempty"`
	PortalUserName   string            `json:"portalUserName,omitempty"`
	OriginalLeadID   string            `json:"originalLeadId,omitempty"`
	OriginalLeadName string            `json:"originalLeadName,omitempty"`
	IsFollowed       bool              `json:"isFollowed,omitempty"`
	FollwersIDs      []string          `json:"followersIds,omitempty"`
	FollowersNames   map[string]string `json:"followersNames,omitempty"`
}

type crmdConvert struct {
	ID      string `json:"id,omitempty"`
	Records struct {
		Contact crmdContact `json:"Contact,omitempty"`
	} `json:"records,omitempty"`
}

func (m *crmdContact) convertFromLead(leadID string) (err error) {
	url := CRMConfig.apiConvertLeadURL()
	lead, err := crmGetLead(leadID)
	if err != nil {
		log.Println(err)
		return
	}

	convert := crmdConvert{}
	convert.ID = leadID
	convert.Records.Contact.copyFromLead(lead)

	jsonB, err := json.Marshal(convert)
	if err != nil {
		return
	}

	jsonStr, err := postRAW(jsonB, url, crmBuildCommonAPIHeader())
	//log.Println(jsonStr)
	if err != nil {
		return
	}

	entity, err := crmJSON2Entity("Lead", jsonStr)
	if err != nil {
		return
	}
	convertedLead := entity.(crmdLead)
	entity, err = crmGetEntity("Contact", convertedLead.CreatedContactID)
	if err != nil {
		return
	}
	*m = entity.(crmdContact)
	return
}

func (m *crmdContact) copyFromLead(lead crmdLead) {
	m.SalutationName = lead.SalutationName
	m.FirstName = lead.FirstName
	m.LastName = lead.LastName
	m.EmailAddressData = lead.EmailAddressData
	m.PhoneNumberData = lead.PhoneNumberData
}
