package fleethistory

import (
	"time"

	"gorm.io/gorm"

	"wucher/internal/shared/pkg/uuidv7"
)

// Entity types a history event can refer to.
const (
	EntityComplaint   = "complaint"
	EntityEASARelease = "easa_release"
	EntityMCF         = "mcf"
	EntityHelicopter  = "helicopter"
)

// Action kinds recorded in the fleet history timeline.
const (
	ActionComplaintFiled = "complaint_filed"
	ActionMELClassified  = "mel_classified"
	ActionNSRDone        = "nsr_done"
	ActionNSRCancelled   = "nsr_cancelled"
	ActionActionTaken    = "action_taken"
	ActionMCFResult      = "mcf_result"
	ActionEASACreated    = "easa_created"
	ActionEASASigned     = "easa_signed"
	ActionAOGSet         = "aog_set"
	ActionAOGCleared     = "aog_cleared"
)

// FleetHistory is an append-only event in a helicopter's timeline. It records who
// did what and when across the complaint / MEL / NSR / action / MCF / EASA / AOG
// flows, forming the per-aircraft history (servicing events live in their own
// service-log table and are merged at read time).
type FleetHistory struct {
	ID           []byte    `gorm:"type:binary(16);primaryKey;column:id"`
	HelicopterID []byte    `gorm:"type:binary(16);not null;index;column:helicopter_id"`
	EntityType   string    `gorm:"type:varchar(32);not null;column:entity_type"`
	EntityID     []byte    `gorm:"type:binary(16);column:entity_id"`
	Action       string    `gorm:"type:varchar(48);not null;column:action"`
	Message      string    `gorm:"type:text;column:message"`
	Actor        []byte    `gorm:"type:binary(16);column:actor"`
	CreatedAt    time.Time `gorm:"type:datetime(3);index;column:created_at"`
}

func (FleetHistory) TableName() string { return "fleet_histories" }

func (f *FleetHistory) BeforeCreate(_ *gorm.DB) error {
	if len(f.ID) == 0 {
		f.ID = uuidv7.MustBytes()
	}
	return nil
}
