package models

import (
	"time"

	"gorm.io/gorm"
)

// Work 作品模型
type Work struct {
	ID          string `json:"id" gorm:"primaryKey;column:id"`
	Title       string `json:"title" gorm:"column:title"`
	Category    string `json:"category" gorm:"column:category"`
	Year        string `json:"year" gorm:"column:year"`
	HeroImg     string `json:"heroImg" gorm:"column:hero_img"`
	HeroVideo   string `json:"heroVideo" gorm:"column:hero_video"`
	VideoID     string `json:"videoId" gorm:"column:video_id"`
	Description string `json:"desc" gorm:"column:description;type:text"`
	Links       string `json:"links" gorm:"column:links;type:text"` // JSON格式存储链接
	IsFeatured  int    `json:"isFeatured" gorm:"column:is_featured;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 (Work) TableName() string {
	return "works"
}

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

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

// WorkTechStack 作品技术栈模型
type WorkTechStack struct {
	ID        uint   `json:"id" gorm:"primaryKey;column:id"`
	WorkID    string `json:"workId" gorm:"column:work_id;index"`
	Category  string `json:"category" gorm:"column:category"`
	Item      string `json:"item" gorm:"column:item"`
	CreatedAt int64  `json:"createdAt" gorm:"column:created_at"`
	DeletedAt int64  `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}

// TableName 指定表名
func (WorkTechStack) TableName() string {
	return "work_tech_stack"
}

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

// WorkGallery 作品图库模型
type WorkGallery struct {
	ID          uint   `json:"id" gorm:"primaryKey;column:id"`
	WorkID      string `json:"workId" gorm:"column:work_id;index"`
	ImageURL    string `json:"imageUrl" gorm:"column:image_url"`
	SortOrder   uint   `json:"sortOrder" gorm:"column:sort_order;default:0"`
	Description string `json:"description" gorm:"column:description"`
	CreatedAt   int64  `json:"createdAt" gorm:"column:created_at"`
	DeletedAt   int64  `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}

// TableName 指定表名
func (WorkGallery) TableName() string {
	return "work_gallery"
}

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

// WorkResponse 作品响应模型，包含关联数据
type WorkResponse struct {
	ID        string                   `json:"id"`
	Title     string                   `json:"title"`
	Category  string                   `json:"category"`
	Year      string                   `json:"year"`
	HeroImg   string                   `json:"heroImg"`
	HeroVideo string                   `json:"heroVideo,omitempty"`
	VideoID   string                   `json:"videoId,omitempty"`
	VideoURL  string                   `json:"videoUrl,omitempty"`
	Desc      string                   `json:"desc"`
	TechStack []map[string]interface{} `json:"techStack"`
	Gallery   []string                 `json:"gallery"`
	Links     map[string]interface{}   `json:"links"`
	Next      string                   `json:"next"`
}
