package helicopterfile

import (
	"time"

	"gorm.io/gorm"

	filemanager "wucher/internal/domain/file_manager"
	"wucher/internal/domain/helicopter"
	"wucher/internal/shared/pkg/uuidv7"
)

const (
	SectionBeforeFirstFlightInspection = "bfi"
	SectionFlightPreparationAndWB      = "fpwb"
	SectionAfterLastFlightInspection   = "afi"
)

type HelicopterFile struct {
	ID          []byte `gorm:"type:binary(16);primaryKey;column:id"`
	Section     string `gorm:"type:varchar(64);not null;index;check:chk_helicopter_files_section,section IN ('bfi','fpwb','afi');column:section"`
	Position    int    `gorm:"column:position;not null;default:1"`
	IsMandatory bool   `gorm:"column:is_mandatory;not null;default:false"`

	HelicopterID     []byte `gorm:"type:binary(16);not null;index;column:helicopter_id"`
	FileAttachmentID []byte `gorm:"type:binary(16);index;column:file_attachment_id"`
	SourceFileID     []byte `gorm:"type:binary(16);index;column:source_file_id"`

	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"`

	Helicopter     *helicopter.Helicopter  `gorm:"foreignKey:HelicopterID;references:ID"`
	FileAttachment *filemanager.Attachment `gorm:"foreignKey:FileAttachmentID;references:ID"`
	SourceFile     *filemanager.File       `gorm:"foreignKey:SourceFileID;references:ID"`
}

func (HelicopterFile) TableName() string { return "helicopter_files" }

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