package common

import (
	"github.com/fiskerinc/cloud-services/pkg/common/dbbasemodel"
)

type ECCKeys struct {
	ECU      string     `json:"ecu,omitempty" pg:",pk"`
	Env      string     `json:"env,omitempty" pg:",pk"`
	PubKey1  *BinaryHex `json:"pub_key_level_1,omitempty" pg:"pub_key_level_1,type:bytea" swaggertype:"string" format:"hex" example:"9a1a6949d7f8a511df6e2e2771e444dbd6de97e7d98bdecbb5adc4b8965ce3bef353f523dbea123d7882dc043d415cda02810bad1b6f1b8c6202234a424b7d5b"`
	PrivKey1 *BinaryHex `json:"level_1,omitempty" pg:"priv_key_level_1,type:bytea" swaggertype:"string" format:"hex" example:"407f59557fb64ae98bc30b5370fab138f4827e14784d79bcf707dbe35ba2b85d"`
	PubKey2  *BinaryHex `json:"pub_key_level_2,omitempty" pg:"pub_key_level_2,type:bytea" swaggertype:"string" format:"hex" example:"9a1a6949d7f8a511df6e2e2771e444dbd6de97e7d98bdecbb5adc4b8965ce3bef353f523dbea123d7882dc043d415cda02810bad1b6f1b8c6202234a424b7d5b"`
	PrivKey2 *BinaryHex `json:"level_2,omitempty" pg:"priv_key_level_2,type:bytea" swaggertype:"string" format:"hex" example:"407f59557fb64ae98bc30b5370fab138f4827e14784d79bcf707dbe35ba2b85d"`
	PubKey3  *BinaryHex `json:"pub_key_level_3,omitempty" pg:"pub_key_level_3,type:bytea" swaggertype:"string" format:"hex" example:"9a1a6949d7f8a511df6e2e2771e444dbd6de97e7d98bdecbb5adc4b8965ce3bef353f523dbea123d7882dc043d415cda02810bad1b6f1b8c6202234a424b7d5b"`
	PrivKey3 *BinaryHex `json:"level_3,omitempty" pg:"priv_key_level_3,type:bytea" swaggertype:"string" format:"hex" example:"407f59557fb64ae98bc30b5370fab138f4827e14784d79bcf707dbe35ba2b85d"`
	dbbasemodel.DBModelBase
}

func (ek *ECCKeys) Init(pub1 *BinaryHex, pub2 *BinaryHex, pub3 *BinaryHex, priv1 *BinaryHex, priv2 *BinaryHex, priv3 *BinaryHex) {
	ek.PubKey1 = pub1
	ek.PubKey2 = pub2
	ek.PubKey3 = pub3
	ek.PrivKey1 = priv1
	ek.PrivKey2 = priv2
	ek.PrivKey3 = priv3
}

// Use this to remove unneeded values when adding to update manifest's ecc_keys
func (ek *ECCKeys) ScrubForManifest() {
	ek.ScrubForAftersales()
	ek.PubKey1 = nil
	ek.PubKey2 = nil
	ek.PubKey3 = nil
}

func (ek *ECCKeys) ScrubForAftersales() {
	ek.ECU = ""
	ek.Env = ""
	ek.CreatedAt = nil
	ek.UpdatedAt = nil
}

// This transforming nonsense is getting pretty crazy here
// Unsure if this will actually work
func (ek *ECCKeys) TransformECUName() {
	replacement, ok := ECUReplacement()[ek.ECU]
	if ok {
		ek.ECU = replacement
	}
}
