package models

import (
	"time"

	"gorm.io/gorm"
)

// Testimonial 客户评价模型
type Testimonial struct {
	ID        uint   `json:"id" gorm:"primaryKey;column:id"`
	Name      string `json:"name" gorm:"column:name"`
	Role      string `json:"role" gorm:"column:role"`
	Content   string `json:"content" gorm:"column:content;type:text"`
	Avatar    string `json:"avatar" gorm:"column:avatar"`
	Rating    uint8  `json:"rating" gorm:"column:rating;default:5"`
	SortOrder uint   `json:"sortOrder" gorm:"column:sort_order;default:0"`
	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 (Testimonial) TableName() string {
	return "testimonials"
}

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

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

// Partner 合作伙伴模型
type Partner struct {
	ID          uint   `json:"id" gorm:"primaryKey;column:id"`
	Name        string `json:"name" gorm:"column:name"`
	Logo        string `json:"logo" gorm:"column:logo"`
	Description string `json:"description" gorm:"column:description;type:text"`
	URL         string `json:"url" gorm:"column:url"`
	SortOrder   uint   `json:"sortOrder" gorm:"column:sort_order;default:0"`
	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 (Partner) TableName() string {
	return "partners"
}

// BeforeCreate 创建前钩子
func (p *Partner) 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 *Partner) BeforeUpdate(tx *gorm.DB) error {
	p.UpdatedAt = time.Now().Unix()
	return nil
}
