package medicine

import (
	"time"

	"gorm.io/gorm"

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

type Medicine struct {
	ID              []byte          `gorm:"type:binary(16);primaryKey;column:id"`
	Name            string          `gorm:"type:varchar(128);not null;column:name"`
	SortKey         *int            `gorm:"column:sortkey"`
	Note            string          `gorm:"type:varchar(255);column:note"`
	Pack            string          `gorm:"type:varchar(128);column:pack"`
	Unit            string          `gorm:"type:varchar(50);column:unit"`
	Weight          *int64          `gorm:"type:bigint;column:weight"`
	Column          int64           `gorm:"type:bigint;column:column"`
	MedicineGroupID []byte          `gorm:"type:binary(16);index;column:medicine_group_id"`
	MotorReactionID []byte          `gorm:"type:binary(16);index;column:motor_reaction_id"`
	IsActive        bool            `gorm:"type:tinyint(1);index;not null;default:1;column:is_active"`
	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"`
	DeletedBy       []byte          `gorm:"type:binary(16);column:deleted_by"`
	MedicineGroup   *MedicineGroup  `gorm:"foreignKey:MedicineGroupID;references:ID"`
	MotorReaction   *MotorReaction  `gorm:"foreignKey:MotorReactionID;references:ID"`
	MotorReactions  []MotorReaction `gorm:"many2many:medicine_motor_reactions;joinForeignKey:MedicineID;joinReferences:MotorReactionID"`
}

func (Medicine) TableName() string { return "medicines" }

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

type MedicineGroup struct {
	ID        []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	Name      string     `gorm:"type:varchar(255);not null;column:name"`
	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"`
	DeletedBy []byte     `gorm:"type:binary(16);column:deleted_by"`
}

func (MedicineGroup) TableName() string { return "medicine_groups" }

func (g *MedicineGroup) BeforeCreate(tx *gorm.DB) error {
	if len(g.ID) == 0 {
		g.ID = uuidv7.MustBytes()
	}
	return nil
}

type MotorReaction struct {
	ID        []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	Name      string     `gorm:"type:varchar(255);not null;column:name"`
	Score     *int64     `gorm:"type:bigint;column:score"`
	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"`
	DeletedBy []byte     `gorm:"type:binary(16);column:deleted_by"`
}

func (MotorReaction) TableName() string { return "motor_reactions" }

func (r *MotorReaction) BeforeCreate(tx *gorm.DB) error {
	if len(r.ID) == 0 {
		r.ID = uuidv7.MustBytes()
	}
	return nil
}

type MedicineMotorReaction struct {
	MedicineID      []byte `gorm:"type:binary(16);primaryKey;column:medicine_id"`
	MotorReactionID []byte `gorm:"type:binary(16);primaryKey;column:motor_reaction_id"`
}

func (MedicineMotorReaction) TableName() string { return "medicine_motor_reactions" }
