package models

import (
	"time"

	"gorm.io/gorm"
)

// VideoCategory 视频分类模型
type VideoCategory struct {
	ID          uint   `json:"id" gorm:"primaryKey;column:id"`
	Name        string `json:"name" gorm:"column:name"`
	Slug        string `json:"slug" gorm:"column:slug;uniqueIndex"`
	Description string `json:"description" gorm:"column:description;type:text"`
	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"`
}

func (VideoCategory) TableName() string { return "video_categories" }

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

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

// VideoCategoryResponse 视频分类 API 响应
type VideoCategoryResponse struct {
	ID          uint   `json:"id"`
	Name        string `json:"name"`
	Slug        string `json:"slug"`
	Description string `json:"description"`
	SortOrder   uint   `json:"sortOrder"`
	CreatedAt   string `json:"createdAt"`
	UpdatedAt   string `json:"updatedAt"`
	VideoCount  int64  `json:"videoCount,omitempty"`
	AlbumCount  int64  `json:"albumCount,omitempty"`
}

// VideoAlbum 视频专辑模型
type VideoAlbum struct {
	ID          uint   `json:"id" gorm:"primaryKey;column:id"`
	Name        string `json:"name" gorm:"column:name"`
	Description string `json:"description" gorm:"column:description;type:text"`
	Cover       string `json:"cover" gorm:"column:cover"`
	CategoryID  uint   `json:"categoryId" gorm:"column:category_id;index"`
	IsActive    int    `json:"isActive" gorm:"column:is_active;default:1"`
	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"`
}

func (VideoAlbum) TableName() string { return "video_albums" }

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

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

// VideoAlbumResponse 视频专辑 API 响应
type VideoAlbumResponse struct {
	ID                   uint            `json:"id"`
	Name                 string          `json:"name"`
	Description          string          `json:"description"`
	Cover                string          `json:"cover"`
	CategoryID           uint            `json:"categoryId"`
	CategoryName         string          `json:"categoryName,omitempty"`
	IsActive             int             `json:"isActive"`
	SortOrder            uint            `json:"sortOrder"`
	CreatedAt            string          `json:"createdAt"`
	UpdatedAt            string          `json:"updatedAt"`
	VideoCount           int64           `json:"videoCount,omitempty"`
	LatestVideoUpdatedAt string          `json:"latestVideoUpdatedAt,omitempty"`
	PreviewVideos        []VideoResponse `json:"previewVideos,omitempty"`
}

// VideoAlbumNavContext 视频详情页专辑内前后导航
type VideoAlbumNavContext struct {
	AlbumID    uint            `json:"albumId"`
	AlbumName  string          `json:"albumName"`
	PrevVideos []VideoResponse `json:"prevVideos"`
	NextVideos []VideoResponse `json:"nextVideos"`
}

// VideoDetailResponse 视频详情 API 响应（含专辑导航）
type VideoDetailResponse struct {
	VideoResponse
	AlbumContext *VideoAlbumNavContext `json:"albumContext,omitempty"`
}

// AdminVideoResponse 管理端视频响应（含所属专辑 ID）
type AdminVideoResponse struct {
	VideoResponse
	AlbumIDs []uint `json:"albumIds,omitempty"`
}

// Video 视频模型
type Video struct {
	ID          string `json:"id" gorm:"primaryKey;column:id"`
	Title       string `json:"title" gorm:"column:title"`
	Description string `json:"description" gorm:"column:description;type:text"`
	VideoURL    string `json:"videoUrl" gorm:"column:video_url"`
	Cover       string `json:"cover" gorm:"column:cover"`
	Poster      string `json:"poster" gorm:"column:poster"`
	CategoryID  uint   `json:"categoryId" gorm:"column:category_id;index"`
	Duration    int    `json:"duration" gorm:"column:duration;default:0"`
	IsPublished int    `json:"isPublished" gorm:"column:is_published;default:1"`
	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 (Video) TableName() string { return "videos" }

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

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

// VideoResponse 视频 API 响应
type VideoResponse struct {
	ID           string `json:"id"`
	Title        string `json:"title"`
	Description  string `json:"description"`
	VideoURL     string `json:"videoUrl"`
	Cover        string `json:"cover"`
	Poster       string `json:"poster"`
	CategoryID   uint   `json:"categoryId"`
	CategoryName string `json:"categoryName,omitempty"`
	Duration     int    `json:"duration"`
	IsPublished  int    `json:"isPublished"`
	CreatedAt    string `json:"createdAt"`
	UpdatedAt    string `json:"updatedAt"`
}

// AlbumVideo 专辑视频关联
type AlbumVideo struct {
	AlbumID   uint   `json:"albumId" gorm:"primaryKey;column:album_id"`
	VideoID   string `json:"videoId" gorm:"primaryKey;column:video_id"`
	SortOrder uint   `json:"sortOrder" gorm:"column:sort_order;default:0"`
	CreatedAt int64  `json:"createdAt" gorm:"column:created_at"`
}

func (AlbumVideo) TableName() string { return "album_videos" }

func (av *AlbumVideo) BeforeCreate(tx *gorm.DB) error {
	if av.CreatedAt == 0 {
		av.CreatedAt = time.Now().Unix()
	}
	return nil
}
