package models

import (
	"time"

	"gorm.io/gorm"
)

// AccessLog 访问日志模型
type AccessLog struct {
	ID           uint   `json:"id" gorm:"primaryKey;column:id"`
	IP           string `json:"ip" gorm:"column:ip;index"`
	UserAgent    string `json:"user_agent" gorm:"column:user_agent"`
	Path         string `json:"path" gorm:"column:path;index"`
	Method       string `json:"method" gorm:"column:method"`
	StatusCode   int    `json:"status_code" gorm:"column:status_code"`
	ResponseTime int64  `json:"response_time" gorm:"column:response_time"` // 毫秒
	Region       string `json:"region" gorm:"column:region"`               // IP归属地
	CreatedAt    int64  `json:"created_at" gorm:"column:created_at"`
	DeletedAt    int64  `json:"deleted_at" gorm:"column:deleted_at;default:0"`
}

// TableName 指定表名
func (AccessLog) TableName() string {
	return "access_logs"
}

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