package flight

import (
	"time"

	"gorm.io/gorm"

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

type Flight struct {
	ID                 []byte               `gorm:"type:binary(16);primaryKey;column:id"`
	Status             string               `gorm:"type:varchar(20);not null;default:draft;column:status"`
	Date               time.Time            `gorm:"type:date;not null;index;column:date"`
	MissionCode        *string              `gorm:"type:varchar(64);index;column:mission_code"`
	DutyRosterID       []byte               `gorm:"type:binary(16);->;column:duty_roster_id"`
	ReserveAcID        []byte               `gorm:"type:binary(16);->;column:reserve_ac_id"`
	TakeoverAcID       []byte               `gorm:"type:binary(16);index;column:takeover_ac_id"`
	Takeover           *takeover.TakeoverAc `gorm:"foreignKey:TakeoverAcID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`

	CreatedAt     time.Time  `gorm:"column:created_at"`
	CreatedBy     []byte     `gorm:"type:binary(16);column:created_by"`
	CreatedByName string     `gorm:"->;column:created_by_name"`
	UpdatedAt     time.Time  `gorm:"column:updated_at"`
	UpdatedBy     []byte     `gorm:"type:binary(16);column:updated_by"`
	UpdatedByName string     `gorm:"->;column:updated_by_name"`
	DeletedAt     *time.Time `gorm:"column:deleted_at;index"`
	DeletedBy     []byte     `gorm:"type:binary(16);column:deleted_by"`
}

func (Flight) TableName() string { return "flights" }

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