package model

import (
	"encoding/json"
	"time"

	"gorm.io/gorm"
)

// Audit represents an audit log entry for enterprise compliance.
// Reference: Chatwoot enterprise Audit model + P2B M11 spec
type Audit struct {
	ID              uint           `gorm:"primaryKey" json:"id"`
	AccountID       *uint          `gorm:"index" json:"account_id,omitempty"`
	UserID          *uint          `gorm:"index" json:"user_id,omitempty"`
	AuditableType   string         `gorm:"size:100;not null;index" json:"auditable_type"` // Conversation/Message/Contact/User/Inbox/Account
	AuditableID     uint           `gorm:"not null;index" json:"auditable_id"`
	Action          string         `gorm:"size:100;not null;index" json:"action"` // create/update/delete/assign/unassign/...
	Changes         json.RawMessage `gorm:"type:jsonb" json:"changes"`
	CreatedAt       time.Time      `gorm:"autoCreateTime;index" json:"created_at"`

	Account *Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
	User    *User    `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

func (Audit) TableName() string { return "audits" }