package flightinspectionfilechecklist

import (
	"time"

	"gorm.io/gorm"

	flightinspection "wucher/internal/domain/flight_inspection"
	helicopterfile "wucher/internal/domain/helicopter_file"
	"wucher/internal/shared/pkg/uuidv7"
)

type FlightInspectionFileChecklist struct {
	ID []byte `gorm:"type:binary(16);primaryKey;column:id"`

	IsDone bool `gorm:"type:tinyint(1);not null;default:0;index:idx_flight_inspection_file_checklists_inspection_sorted;column:is_done"`

	HelicopterFileID   []byte `gorm:"type:binary(16);not null;index:idx_flight_inspection_file_checklists_file;uniqueIndex:uidx_flight_inspection_file_checklists_inspection_file,priority:2;column:helicopter_file_id"`
	FlightInspectionID []byte `gorm:"type:binary(16);not null;index:idx_flight_inspection_file_checklists_inspection;index:idx_flight_inspection_file_checklists_inspection_sorted;uniqueIndex:uidx_flight_inspection_file_checklists_inspection_file,priority:1;column:flight_inspection_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"`

	HelicopterFile   *helicopterfile.HelicopterFile     `gorm:"foreignKey:HelicopterFileID;references:ID"`
	FlightInspection *flightinspection.FlightInspection `gorm:"foreignKey:FlightInspectionID;references:ID"`
}

func (FlightInspectionFileChecklist) TableName() string { return "flight_inspection_file_checklists" }

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