package patient_data

import (
	"time"

	"gorm.io/gorm"

	insurancepatientdata "wucher/internal/domain/insurance_patient_data"
	"wucher/internal/domain/opc"
	"wucher/internal/shared/pkg/uuidv7"
)

const (
	GenderMale   = "male"
	GenderFemale = "female"
)

func IsValidGender(g string) bool {
	return g == GenderMale || g == GenderFemale
}

type PatientData struct {
	ID                     []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	FamilyName             string     `gorm:"type:varchar(128);not null;column:family_name"`
	FirstName              string     `gorm:"type:varchar(128);not null;column:first_name"`
	OPCId                  []byte     `gorm:"type:binary(16);index;column:opc_id"`
	BirthDate              *time.Time `gorm:"type:date;column:birth_date"`
	Gender                 string     `gorm:"type:varchar(16);not null;column:gender"`
	Street                 string     `gorm:"type:varchar(255);column:street"`
	SVNR                   string     `gorm:"type:varchar(64);column:svnr"`
	Email                  string     `gorm:"type:varchar(191);column:email"`
	Phone                  string     `gorm:"type:varchar(64);column:phone"`
	CoInsured              bool       `gorm:"type:tinyint(1);not null;default:0;column:co_insured"`
	InsurancePatientDataId []byte     `gorm:"type:binary(16);index;column:insurance_patient_data_id"`

	OPC                  *opc.Opc                                   `gorm:"foreignKey:OPCId;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	InsurancePatientData *insurancepatientdata.InsurancePatientData `gorm:"foreignKey:InsurancePatientDataId;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`

	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 (PatientData) TableName() string { return "patient_data" }

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