package models

import (
	"time"

	"gorm.io/gorm"
)

// CodeType 代码类型
type CodeType struct {
	ID        uint   `json:"id" gorm:"primaryKey;column:id"`
	Name      string `json:"name" gorm:"column:name"`
	Category  int    `json:"category" gorm:"column:category"` // 0前端 1后端 2其他
	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"`
}

func (CodeType) TableName() string {
	return "code_types"
}

func (c *CodeType) BeforeCreate(tx *gorm.DB) error {
	now := time.Now().Unix()
	if c.CreatedAt == 0 {
		c.CreatedAt = now
	}
	if c.UpdatedAt == 0 {
		c.UpdatedAt = now
	}
	return nil
}

func (c *CodeType) BeforeUpdate(tx *gorm.DB) error {
	c.UpdatedAt = time.Now().Unix()
	return nil
}

type CodeTypeResponse struct {
	ID       uint   `json:"id"`
	Name     string `json:"name"`
	Category int    `json:"category"`
}
