package operation

import (
	"strings"
	"time"

	"gorm.io/gorm"

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

type HEMSOperation struct {
	ID                      []byte    `json:"id" gorm:"type:binary(16);primaryKey;column:id"`
	Date                    time.Time `json:"date" gorm:"column:date;not null" example:"2026-03-12T03:04:05Z"`
	HEMSOperationCategoryID []byte    `json:"hems_operation_category_id,omitempty" gorm:"type:binary(16);column:hems_operation_category_id;index" example:"018f3a5c-7a6f-7c2a-8d4a-4b5b1f6c8e9d"`
	MissionID               []byte    `json:"mission_id,omitempty" gorm:"type:binary(16);column:mission_id;index;uniqueIndex:uidx_hems_operations_mission_id" example:"018f3a5c-7a6f-7c2a-8d4a-4b5b1f6c8e9d"`
	OperationalDataID       []byte    `json:"operational_data_id,omitempty" gorm:"type:binary(16);column:operational_data_id;index" example:"018f3a5c-7a6f-7c2a-8d4a-4b5b1f6c8e9d"`

	OperationalData       *HEMSOperationalData   `json:"operational_data,omitempty" gorm:"foreignKey:OperationalDataID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Mission               *mission.Mission       `json:"mission,omitempty" gorm:"foreignKey:MissionID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	HEMSOperationCategory *HEMSOperationCategory `json:"hems_operation_category,omitempty" gorm:"foreignKey:HEMSOperationCategoryID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`

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

func (HEMSOperation) TableName() string { return "hems_operations" }

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

type HEMSOperationCategory struct {
	ID        []byte     `json:"id" gorm:"type:binary(16);primaryKey;column:id"`
	Name      string     `json:"name" gorm:"type:varchar(128);not null;column:name" example:"firefighting"`
	Type      string     `json:"type" gorm:"type:varchar(32);not null;column:type" enum:"first, secondary, misuse"`
	CreatedAt time.Time  `json:"created_at,omitempty" gorm:"column:created_at"`
	CreatedBy []byte     `json:"created_by,omitempty" gorm:"type:binary(16);column:created_by"`
	UpdatedAt time.Time  `json:"updated_at,omitempty" gorm:"column:updated_at"`
	UpdatedBy []byte     `json:"updated_by,omitempty" gorm:"type:binary(16);column:updated_by"`
	DeletedAt *time.Time `json:"deleted_at,omitempty" gorm:"column:deleted_at"`
	DeletedBy []byte     `json:"deleted_by,omitempty" gorm:"type:binary(16);column:deleted_by"`
}

func (HEMSOperationCategory) TableName() string { return "hems_operation_categories" }

func (c *HEMSOperationCategory) BeforeCreate(_ *gorm.DB) error {
	if len(c.ID) == 0 {
		c.ID = uuidv7.MustBytes()
	}
	c.Type = strings.ToLower(strings.TrimSpace(c.Type))
	return nil
}

func IsValidHEMSOperationCategoryType(value string) bool {
	switch strings.ToLower(strings.TrimSpace(value)) {
	case "first", "secondary", "misuse":
		return true
	default:
		return false
	}
}
