package models

import (
	"time"

	"gorm.io/gorm"
)

// Inquiry 合作咨询
type Inquiry struct {
	ID            uint   `json:"id" gorm:"primaryKey;column:id"`
	Name          string `json:"name" gorm:"column:name"`
	Company       string `json:"company" gorm:"column:company"`
	ContactMethod string `json:"contactMethod" gorm:"column:contact_method"` // email, wechat, phone
	ContactValue  string `json:"contactValue" gorm:"column:contact_value"`
	Budget        string `json:"budget" gorm:"column:budget"`
	Description   string `json:"description" gorm:"column:description;type:text"`
	Status        int    `json:"status" gorm:"column:status;default:0"` // 0-Unread, 1-Read, 2-Contacted
	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 (Inquiry) TableName() string {
	return "inquiries"
}

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

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

// EmailSuffix 邮箱后缀配置
type EmailSuffix struct {
	ID        uint   `json:"id" gorm:"primaryKey;column:id"`
	Suffix    string `json:"suffix" gorm:"column:suffix;uniqueIndex"`
	IsActive  bool   `json:"isActive" gorm:"column:is_active;default:1"`
	SortOrder int    `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 (EmailSuffix) TableName() string {
	return "email_suffixes"
}

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

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