package audit

import (
	"time"

	"gorm.io/gorm"

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

type AuditLog struct {
	ID          []byte `gorm:"type:binary(16);primaryKey;column:id"`
	RequestID   string `gorm:"type:varchar(64);index;column:request_id"`
	ActorUserID []byte `gorm:"type:binary(16);index;column:actor_user_id"`
	Layer       string `gorm:"type:varchar(32);index;not null;column:layer"`
	Module      string `gorm:"type:varchar(64);index;column:module"`
	Action      string `gorm:"type:varchar(128);index;not null;column:action"`
	Method      string `gorm:"type:varchar(12);index;column:method"`
	Path        string `gorm:"type:varchar(255);index;column:path"`
	StatusCode  int    `gorm:"index;column:status_code"`
	Success     bool   `gorm:"type:tinyint(1);index;not null;default:0;column:success"`
	IP          string `gorm:"type:varchar(128);column:ip"`
	UserAgent   string `gorm:"type:varchar(255);column:user_agent"`
	Message     string `gorm:"type:varchar(512);column:message"`
	Metadata    []byte `gorm:"type:json;column:metadata"`
	CreatedAt   time.Time
}

func (AuditLog) TableName() string { return "audit_logs" }

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