package models

import (
	"time"

	"gorm.io/gorm"
)

// Permission 权限模型
type Permission struct {
	ID        uint   `json:"id" gorm:"primaryKey;column:id"`
	Name      string `json:"name" gorm:"column:name"`
	Resource  string `json:"resource" gorm:"column:resource"`
	Action    string `json:"action" gorm:"column:action"`
	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 (Permission) TableName() string {
	return "permissions"
}

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

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

// PermissionResponse 权限响应模型
type PermissionResponse struct {
	ID        uint   `json:"id"`
	Name      string `json:"name"`
	Resource  string `json:"resource"`
	Action    string `json:"action"`
	CreatedAt string `json:"createdAt"`
	UpdatedAt string `json:"updatedAt"`
}
