package common

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

type ManifestFileType string

const (
	Bootloader  ManifestFileType = "bootloader"
	Software    ManifestFileType = "software"
	Calibration ManifestFileType = "calibration"
	Other       ManifestFileType = "other"
)

var fileSortOrder = map[ManifestFileType]int{
	Bootloader:  0,
	Software:    1,
	Calibration: 2,
	Other:       3,
}

type UpdateManifestFile struct {
	FileID              string           `json:"file_id" pg:",pk" validate:"omitempty,hexadecimal"`
	UpdateManifestECUID int64            `json:"manifest_ecu_id,omitempty" pg:"update_manifest_ecu_id,unique:updatemanifestecuid_filename" validate:"gte=0"`
	Filename            string           `json:"filename,omitempty" pg:",unique:updatemanifestecuid_filename" validate:"max=512"`
	URL                 string           `json:"url" validate:"omitempty,max=32768,url"`
	FileSize            uint64           `json:"file_size,omitempty"`
	Checksum            string           `json:"checksum,omitempty" validate:"omitempty,max=8,hexadecimal"`
	FileType            ManifestFileType `json:"type,omitempty" validate:"max=255"`
	FileOrder           int              `json:"order,omitempty"`
	WriteRegionID       int64            `json:"-"`
	WriteRegion         MemoryRegion     `json:"write_region" pg:"rel:has-one"`
	EraseRegionID       int64            `json:"-"`
	EraseRegion         *MemoryRegion    `json:"erase_region,omitempty" pg:"rel:has-one"`
	FileKey             *FileKeyResponse `json:"file_key,omitempty" pg:"rel:belongs-to"`
	Parsed              *bool            `json:"parsed_file,omitempty" pg:"parsed_file"`
	Signature           string           `json:"signature,omitempty" validate:"omitempty,max=129,hexadecimal"`
  	CompatibleTrims      []CompatibleTrim      `json:"compatible_trims,omitempty" pg:",array"`
	CompatibleDriveSides []CompatibleDriveSide `json:"compatible_drive_sides,omitempty" pg:",array"`
	dbbasemodel.DBModelBase
}

func (umf *UpdateManifestFile) Scrub() {
	umf.UpdateManifestECUID = 0
	umf.Filename = ""
	umf.FileOrder = 0
	umf.CreatedAt = nil
	umf.UpdatedAt = nil
	umf.Parsed = nil
	umf.CompatibleTrims = nil
	umf.CompatibleDriveSides = nil
}

// I think this code is kind of messy, and could definitely be cleaner
func (umf *UpdateManifestFile) IsFileCompatible(compatibleFilter ECUFileFilter) (compatible bool) {
	compatible = true
	hasTrim := false
	for _, trim := range umf.CompatibleTrims {
		if trim == compatibleFilter.Trim {
			hasTrim = true
			break
		}
	}
	if !hasTrim {
		compatible = false
		return
	}

	hasDriveSide := false
	for _, driveSide := range umf.CompatibleDriveSides{
		if driveSide == compatibleFilter.DriveSide {
			hasDriveSide = true
			break
		}
	}
	if !hasDriveSide {
		compatible = false
		return
	}
	return
}

type CompatibleDriveSide string

const (
	LEFT_HAND_DRIVE  CompatibleDriveSide = "LHD"
	RIGHT_HAND_DRIVE CompatibleDriveSide = "RHD"
)

// May want to make a dynamic table for this, as new cars will have different trims
type CompatibleTrim string

const (
	EXTREME CompatibleTrim = "EXT"
	ULTRA   CompatibleTrim = "ULT"
	SPORT   CompatibleTrim = "SPT"
)

// Unused, but I imagine we will need this field in the future
type CompatibleYear string

const (
	YEAR_2023 CompatibleYear = "2023"
)
