package models

import (
	"time"

	"gorm.io/gorm"
)

// Setting 系统配置模型
type Setting struct {
	ID          uint   `json:"id" gorm:"primaryKey;column:id"`
	KeyName     string `json:"keyName" gorm:"column:key_name;uniqueIndex"`
	Value       string `json:"value" gorm:"column:value;type:text"`
	Description string `json:"description" gorm:"column:description"`
	CreatedAt   int64  `json:"createdAt" gorm:"column:created_at"`
	UpdatedAt   int64  `json:"updatedAt" gorm:"column:updated_at"`
	DeletedAt   int64  `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}

// TableName 指定表名
func (Setting) TableName() string {
	return "settings"
}

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

// BeforeUpdate 更新前钩子
func (s *Setting) BeforeUpdate(tx *gorm.DB) error {
	s.UpdatedAt = time.Now().Unix()
	return nil
}

// SettingResponse 系统配置响应模型
type SettingResponse struct {
	ID          uint   `json:"id"`
	KeyName     string `json:"keyName"`
	Value       string `json:"value"`
	Description string `json:"description"`
	CreatedAt   string `json:"createdAt"`
	UpdatedAt   string `json:"updatedAt"`
}
