package fleetstatus

import (
	"time"

	"gorm.io/gorm"

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

type FleetStatus struct {
	ID           []byte                 `gorm:"type:binary(16);primaryKey;column:id"`
	HelicopterID []byte                 `gorm:"type:binary(16);not null;index;column:helicopter_id"`
	Helicopter   *helicopter.Helicopter `gorm:"foreignKey:HelicopterID;references:ID"`
	Status       string                 `gorm:"type:varchar(16);not null;default:active;index;column:status"`
	ServicedAt   *time.Time             `gorm:"column:serviced_at;index"`

	MaintenanceSchedules []MaintenanceSchedule `gorm:"foreignKey:FleetStatusID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	Files                []FleetStatusFile     `gorm:"foreignKey:FleetStatusID;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 (FleetStatus) TableName() string { return "fleet_statuses" }

func (f *FleetStatus) BeforeCreate(_ *gorm.DB) error {
	if len(f.ID) == 0 {
		f.ID = uuidv7.MustBytes()
	}
	if f.Status == "" {
		f.Status = StatusActive
	}
	return nil
}

type MaintenanceSchedule struct {
	ID             []byte `gorm:"type:binary(16);primaryKey;column:id"`
	FleetStatusID  []byte `gorm:"type:binary(16);not null;index;column:fleet_status_id"`
	InspectionType string `gorm:"type:varchar(16);not null;column:inspection_type"`
	Type           string `gorm:"type:varchar(255);not null;column:type"`
	Due            string `gorm:"type:varchar(255);column:due"`
	Ext            string `gorm:"type:varchar(255);column:ext"`

	FleetStatus *FleetStatus `gorm:"foreignKey:FleetStatusID;references:ID"`
}

func (MaintenanceSchedule) TableName() string { return "maintenance_schedules" }

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

type FleetStatusFile struct {
	ID               []byte `gorm:"type:binary(16);primaryKey;column:id"`
	IsMandatory      bool   `gorm:"not null;default:false;column:is_mandatory"`
	FleetStatusID    []byte `gorm:"type:binary(16);not null;index;column:fleet_status_id"`
	FileAttachmentID []byte `gorm:"type:binary(16);not null;index;column:file_attachment_id"`
	Category         string `gorm:"type:varchar(64);index;column:category"`

	FileID []byte `gorm:"-"`

	FleetStatus *FleetStatus            `gorm:"foreignKey:FleetStatusID;references:ID"`
	Attachment  *filemanager.Attachment `gorm:"foreignKey:FileAttachmentID;references:ID"`
}

func (FleetStatusFile) TableName() string { return "fleet_status_files" }

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

type FleetStatusServiceLog struct {
	ID             []byte                 `gorm:"type:binary(16);primaryKey;column:id"`
	FleetStatusID  []byte                 `gorm:"type:binary(16);not null;index;column:fleet_status_id"`
	HelicopterID   []byte                 `gorm:"type:binary(16);not null;index;column:helicopter_id"`
	Helicopter     *helicopter.Helicopter `gorm:"foreignKey:HelicopterID;references:ID"`
	InspectionType string                 `gorm:"type:varchar(16);not null;column:inspection_type"`
	Type           string                 `gorm:"type:varchar(255);not null;column:type"`
	Due            string                 `gorm:"type:varchar(255);column:due"`
	Ext            string                 `gorm:"type:varchar(255);column:ext"`
	ServicedAt     time.Time              `gorm:"column:serviced_at;index"`
	CreatedAt      time.Time              `gorm:"column:created_at"`
	CreatedBy      []byte                 `gorm:"type:binary(16);column:created_by"`
}

func (FleetStatusServiceLog) TableName() string { return "fleet_status_service_logs" }

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

