package models

import (
	"time"

	"gorm.io/gorm"
)

// OperationLog 操作日志模型
type OperationLog struct {
	ID        uint   `json:"id" gorm:"primaryKey;column:id"`
	UserID    uint   `json:"userId" gorm:"column:user_id;index"`
	Username  string `json:"username" gorm:"column:username"`
	IP        string `json:"ip" gorm:"column:ip"`
	Region    string `json:"region" gorm:"column:region"` // IP归属地
	Path      string `json:"path" gorm:"column:path;index"`
	Method    string `json:"method" gorm:"column:method"`
	Action    string `json:"action" gorm:"column:action;index"`
	Params    string `json:"params" gorm:"column:params;type:text"`
	Status    int    `json:"status" gorm:"column:status"`
	Duration  int    `json:"duration" gorm:"column:duration"`
	CreatedAt int64  `json:"createdAt" gorm:"column:created_at"`
	DeletedAt int64  `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}

// TableName 指定表名
func (OperationLog) TableName() string {
	return "operation_logs"
}

// BeforeCreate 创建前钩子
func (o *OperationLog) BeforeCreate(tx *gorm.DB) error {
	if o.CreatedAt == 0 {
		o.CreatedAt = time.Now().Unix()
	}
	if o.DeletedAt == 0 {
		o.DeletedAt = 0
	}
	return nil
}

// OperationLogResponse 操作日志响应模型
type OperationLogResponse struct {
	ID        uint   `json:"id"`
	UserID    uint   `json:"userId"`
	Username  string `json:"username"`
	IP        string `json:"ip"`
	Region    string `json:"region"` // IP归属地
	Path      string `json:"path"`
	Method    string `json:"method"`
	Action    string `json:"action"` // 操作描述
	Params    string `json:"params"`
	Status    int    `json:"status"`
	Duration  int    `json:"duration"`
	CreatedAt string `json:"createdAt"`
}
