package model

import "time"

// AssetOperationLog 资产操作审计日志模型。
// 记录资产域（卡/设备）敏感写操作，不建立外键约束，不使用 GORM 关联标签。
type AssetOperationLog struct {
	ID        uint      `gorm:"column:id;primaryKey;comment:主键ID" json:"id"`
	CreatedAt time.Time `gorm:"column:created_at;not null;comment:创建时间" json:"created_at"`

	OperatorID   *uint  `gorm:"column:operator_id;type:bigint;index:idx_asset_log_operator_created,priority:1;comment:操作人ID（系统触发为空）" json:"operator_id,omitempty"`
	OperatorType string `gorm:"column:operator_type;type:varchar(32);not null;comment:操作人类型(system/admin_user/agent_user/enterprise_user/personal_customer)" json:"operator_type"`
	OperatorName string `gorm:"column:operator_name;type:varchar(255);not null;default:'';comment:操作人名称快照" json:"operator_name"`

	AssetType       string `gorm:"column:asset_type;type:varchar(32);not null;index:idx_asset_log_asset_created,priority:1;comment:资产类型(iot_card/device)" json:"asset_type"`
	AssetID         uint   `gorm:"column:asset_id;type:bigint;not null;index:idx_asset_log_asset_created,priority:2;comment:资产ID" json:"asset_id"`
	AssetIdentifier string `gorm:"column:asset_identifier;type:varchar(128);not null;default:'';index:idx_asset_log_identifier_created,priority:1;comment:资产标识(ICCID/虚拟号)" json:"asset_identifier"`

	OperationType string `gorm:"column:operation_type;type:varchar(64);not null;index:idx_asset_log_operation_created,priority:1;comment:操作类型" json:"operation_type"`
	OperationDesc string `gorm:"column:operation_desc;type:text;not null;comment:操作描述（中文）" json:"operation_desc"`

	BeforeData JSONB `gorm:"column:before_data;type:jsonb;comment:变更前数据" json:"before_data,omitempty"`
	AfterData  JSONB `gorm:"column:after_data;type:jsonb;comment:变更后数据" json:"after_data,omitempty"`

	RequestID     *string `gorm:"column:request_id;type:varchar(255);comment:请求ID" json:"request_id,omitempty"`
	IPAddress     *string `gorm:"column:ip_address;type:varchar(64);comment:请求IP" json:"ip_address,omitempty"`
	UserAgent     *string `gorm:"column:user_agent;type:text;comment:用户代理" json:"user_agent,omitempty"`
	RequestPath   *string `gorm:"column:request_path;type:varchar(255);comment:请求路径" json:"request_path,omitempty"`
	RequestMethod *string `gorm:"column:request_method;type:varchar(16);comment:请求方法" json:"request_method,omitempty"`

	ResultStatus string  `gorm:"column:result_status;type:varchar(16);not null;index:idx_asset_log_result_created,priority:1;comment:执行结果(success/failed/denied)" json:"result_status"`
	ErrorCode    *string `gorm:"column:error_code;type:varchar(64);comment:错误码" json:"error_code,omitempty"`
	ErrorMsg     *string `gorm:"column:error_msg;type:text;comment:错误摘要" json:"error_msg,omitempty"`

	BatchTotal   int `gorm:"column:batch_total;type:int;not null;default:0;comment:批量总数" json:"batch_total"`
	SuccessCount int `gorm:"column:success_count;type:int;not null;default:0;comment:批量成功数" json:"success_count"`
	FailCount    int `gorm:"column:fail_count;type:int;not null;default:0;comment:批量失败数" json:"fail_count"`
}

// TableName 指定资产操作日志表名。
func (AssetOperationLog) TableName() string {
	return "tb_asset_operation_log"
}
