package helicopter

import (
	"gorm.io/gorm"
	"time"
	filemanager "wucher/internal/domain/file_manager"
	"wucher/internal/shared/pkg/uuidv7"
)

const (
	StatusAvailable = "available"
	StatusBooked    = "booked"
	StatusAOG       = "aog"
	StatusMCF       = "mcf"
)

const (
	AOGSourceUnknown   = ""
	AOGSourceManual    = "manual"
	AOGSourceAuto      = "auto"
	AOGSourceComplaint = "complaint"
)

// StatusRank is the display order for fleet and hold/defect listings — most critical
// first: MCF, then AOG, then Booked, then Available (any unknown status sorts last).
func StatusRank(status string) int {
	switch status {
	case StatusMCF:
		return 0
	case StatusAOG:
		return 1
	case StatusBooked:
		return 2
	case StatusAvailable:
		return 3
	default:
		return 4
	}
}

func IsValidStatus(s string) bool {
	switch s {
	case StatusAvailable, StatusBooked, StatusAOG, StatusMCF:
		return true
	default:
		return false
	}
}

type StatusInput struct {
	IsBook            bool
	IsMCF             bool
	ComplaintGrounded bool
}

func (h *Helicopter) DeriveStatus(in StatusInput) string {
	switch {
	case in.IsMCF:
		return StatusMCF
	case h.AirOnGround || in.ComplaintGrounded:
		return StatusAOG
	case in.IsBook:
		return StatusBooked
	default:
		return StatusAvailable
	}
}

func (h *Helicopter) EnforceAOGInactive() {
	if h.AirOnGround {
		h.IsActive = false
	}
}

func (h *Helicopter) EnforceMCFInactive(mcf bool) {
	if mcf {
		h.IsActive = false
	}
}

type SortKeyPatch struct {
	Set   bool
	Value *int
}

type Helicopter struct {
	ID               []byte                  `gorm:"type:binary(16);primaryKey;column:id"`
	Designation      string                  `gorm:"type:varchar(100);not null;column:designation"`
	Identifier       string                  `gorm:"type:varchar(100);uniqueIndex;not null;column:identifier"`
	ReportSequence   int                     `gorm:"not null;default:0;column:report_sequence"`
	Type             string                  `gorm:"type:varchar(100);not null;column:type"`
	SortKey          *int                    `gorm:"column:sortkey"`
	Engine1          string                  `gorm:"type:varchar(100);column:engine_1"`
	Engine2          string                  `gorm:"type:varchar(100);column:engine_2"`
	Consumption      float64                 `gorm:"type:decimal(10,2);not null;default:0;column:consumption_lt_min"`
	NR1              bool                    `gorm:"type:tinyint(1);not null;default:0;column:nr1"`
	NR2              bool                    `gorm:"type:tinyint(1);not null;default:0;column:nr2"`
	LH               bool                    `gorm:"type:tinyint(1);not null;default:0;column:lh"`
	RH               bool                    `gorm:"type:tinyint(1);not null;default:0;column:rh"`
	MGB              bool                    `gorm:"type:tinyint(1);not null;default:0;column:mgb"`
	IGB              bool                    `gorm:"type:tinyint(1);not null;default:0;column:igb"`
	TGB              bool                    `gorm:"type:tinyint(1);not null;default:0;column:tgb"`
	UTCOffset        int                     `gorm:"not null;default:0;column:utc_offset"`
	FotoAttachmentID []byte                  `gorm:"type:binary(16);index;column:foto_attachment_id"`
	Dry              bool                    `gorm:"type:tinyint(1);not null;default:0;column:dry"`
	AirOnGround      bool                    `gorm:"type:tinyint(1);not null;default:0;column:air_on_ground"`
	AOGReason        string                  `gorm:"type:text;column:aog_reason"`
	AOGSource        string                  `gorm:"type:varchar(16);not null;default:'';column:aog_source"`
	Note             string                  `gorm:"type:text;column:note"`
	IsActive         bool                    `gorm:"type:tinyint(1);index;not null;default:1;column:is_active"`
	CreatedBy        []byte                  `gorm:"type:binary(16);column:created_by"`
	UpdatedBy        []byte                  `gorm:"type:binary(16);column:updated_by"`
	DeletedAt        *time.Time              `gorm:"column:deleted_at"`
	CreatedByName    string                  `gorm:"->;column:created_by_name"`
	UpdatedByName    string                  `gorm:"->;column:updated_by_name"`
	FotoAttachment   *filemanager.Attachment `gorm:"foreignKey:FotoAttachmentID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	CreatedAt        time.Time
	UpdatedAt        time.Time
}

func (Helicopter) TableName() string { return "helicopters" }

func (h *Helicopter) BeforeCreate(tx *gorm.DB) error {
	if len(h.ID) == 0 {
		h.ID = uuidv7.MustBytes()
	}
	return nil
}
