package common

import (
	"fmt"

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

const (
	InfoSourceAutoCreated = "autocreated"
	CarSoldStatusRetailed = "Retailed"
)

type RegionCode string

const (
	US RegionCode = "US"
	EU RegionCode = "EU"
)

// Car schema
type Car struct {
	VIN         string           `pg:",pk" json:"vin" validate:"required,vin"`
	Region      RegionCode       `json:"region,omitempty"`
	Country     string           `json:"country,omitempty" validate:"max=256"`
	Year        int              `json:"year,omitempty" validate:"required,gte=1000,lte=9999"`
	Model       string           `json:"model,omitempty" validate:"required,max=256"`
	Trim        string           `json:"trim,omitempty" validate:"required,max=256"`
	Powertrain  string           `json:"powertrain,omitempty" validate:"max=256"`
	Restraint   string           `json:"restraint,omitempty" validate:"max=256"`
	BodyType    string           `json:"body_type,omitempty" validate:"max=256"`
	ECUList     string           `json:"ecu_list,omitempty"`
	ICCID       string           `json:"iccid,omitempty" validate:"omitempty,max=50"`
	InfoSource  string           `pg:"info_source" json:"-"`
	Blocked     bool             `pg:"blocked" json:"-"`
	Tags        []string         `json:"tags,omitempty" pg:"tags,array" validate:"omitempty,max=50"`
	Drivers     []CarToDriver    `pg:"-" json:"drivers,omitempty"`
	Manifests   []StatusManifest `pg:"-" json:"manifests,omitempty"`
	Document    string           `pg:"-" json:"document,omitempty"`
	SoldStatus  string           `pg:"sold_status" json:"sold_status,omitempty"`
	SUMSVersion string           `pg:"sums_version" json:"sums_version,omitempty"`
	OSVersion   string           `pg:"-" json:"os_version,omitempty"`
	Flashpack   string           `json:"flashpack,omitempty"`
	dbbasemodel.DBModelBase
}

// CarToDriver table storing cars-to-drivers
type CarToDriver struct {
	ID            int64          `json:"id"`
	VIN           string         `pg:",unique:carid_driverid" json:"vin,omitempty" validate:"required,vin"`
	DriverID      string         `pg:",unique:carid_driverid" json:"driverid,omitempty" validate:"required,max=256"`
	DriverRole    string         `json:"role" validate:"required,max=100"`
	BLEKey        string         `pg:"ble_key" json:"ble_key,omitempty" validation:"hexdecimal,max=32"`
	Settings      []CarSetting   `pg:"-" json:"settings,omitempty"`
	Subscriptions []Subscription `pg:"rel:has-many" json:"subscriptions,omitempty"`
	dbbasemodel.DBModelBase
}

func (c Car) String() string {
	return fmt.Sprintf("Car<%s %s %d>", c.VIN, c.Model, c.Year)
}
