package reserveac

import (
	"time"

	"gorm.io/gorm"

	flightinspection "wucher/internal/domain/flight_inspection"
	helicopter "wucher/internal/domain/helicopter"
	"wucher/internal/shared/pkg/reserveacutil"
	"wucher/internal/shared/pkg/uuidv7"
)

const (
	StatusPending    = reserveacutil.StatusPending
	StatusApproved   = reserveacutil.StatusApproved
	StatusOnProgress = reserveacutil.StatusOnProgress
)

type ReserveAc struct {
	ID           []byte  `gorm:"type:binary(16);primaryKey;column:id"`
	Status       string  `gorm:"type:varchar(20);not null;default:pending;index;check:chk_reserve_ac_status,status IN ('pending','approved','onprogress');column:status"`
	BaseID       []byte  `gorm:"type:binary(16);index;column:base_id"`
	AircraftID   []byte  `gorm:"type:binary(16);not null;index;column:helicopter_id"`
	InspectionID []byte  `gorm:"type:binary(16);not null;uniqueIndex:uidx_reserve_acs_inspection_id;column:inspection_id"`
	Notes        *string `gorm:"type:text;column:notes"`

	Aircraft   *helicopter.Helicopter             `gorm:"foreignKey:AircraftID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	Inspection *flightinspection.FlightInspection `gorm:"foreignKey:InspectionID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`

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

func (ReserveAc) TableName() string { return "reserve_acs" }

func (o *ReserveAc) BeforeCreate(_ *gorm.DB) error {
	if len(o.ID) == 0 {
		o.ID = uuidv7.MustBytes()
	}
	o.Status = reserveacutil.NormalizeStatus(o.Status)
	return nil
}
