package model

import (
	"time"

	"gorm.io/datatypes"
)

type ChatConversation struct {
	ID                 uint64     `gorm:"primaryKey" json:"id"`
	OrderID            *uint64    `gorm:"uniqueIndex" json:"order_id"`
	ListingID          *uint64    `gorm:"index" json:"listing_id"`
	Type               string     `gorm:"size:32;not null;default:'order_group'" json:"type"`
	Title              string     `gorm:"size:128;not null" json:"title"`
	Status             string     `gorm:"size:32;not null;default:'active'" json:"status"`
	LastMessageID      *uint64    `json:"last_message_id"`
	LastMessagePreview string     `gorm:"size:255;not null;default:''" json:"last_message_preview"`
	LastMessageAt      *time.Time `json:"last_message_at"`
	CreatedAt          time.Time  `json:"created_at"`
	UpdatedAt          time.Time  `json:"updated_at"`
}

func (ChatConversation) TableName() string {
	return "chat_conversations"
}

type ChatParticipant struct {
	ID              uint64     `gorm:"primaryKey" json:"id"`
	ConversationID  uint64     `gorm:"not null;index" json:"conversation_id"`
	ParticipantType string     `gorm:"size:16;not null;index" json:"participant_type"`
	ParticipantID   uint64     `gorm:"not null;index" json:"participant_id"`
	Role            string     `gorm:"size:32;not null" json:"role"`
	Remark          string     `gorm:"size:128;not null;default:''" json:"remark"`
	LastReadAt      *time.Time `json:"last_read_at"`
	JoinedAt        time.Time  `json:"joined_at"`
	CreatedAt       time.Time  `json:"created_at"`
	UpdatedAt       time.Time  `json:"updated_at"`
}

func (ChatParticipant) TableName() string {
	return "chat_participants"
}

type ChatMessage struct {
	ID             uint64         `gorm:"primaryKey" json:"id"`
	ConversationID uint64         `gorm:"not null;index" json:"conversation_id"`
	SenderType     string         `gorm:"size:16;not null" json:"sender_type"`
	SenderID       uint64         `gorm:"not null;default:0" json:"sender_id"`
	SenderRole     string         `gorm:"size:32;not null;default:''" json:"sender_role"`
	ContentType    string         `gorm:"size:32;not null;default:'text'" json:"content_type"`
	Content        string         `json:"content"`
	AttachmentURLS datatypes.JSON `gorm:"column:attachment_urls" json:"attachment_urls"`
	CreatedAt      time.Time      `json:"created_at"`
}

func (ChatMessage) TableName() string {
	return "chat_messages"
}

type ChatQrCode struct {
	ID             uint64     `gorm:"primaryKey" json:"id"`
	ImageURL       string     `gorm:"size:512;not null" json:"image_url"`
	GroupName      string     `gorm:"size:128;not null;default:''" json:"group_name"`
	Status         string     `gorm:"size:16;not null;default:'unused'" json:"status"`
	ConversationID *uint64    `gorm:"index" json:"conversation_id"`
	UsedAt         *time.Time `json:"used_at"`
	ExpiresAt      *time.Time `json:"expires_at"`
	CreatedBy      uint64     `gorm:"not null" json:"created_by"`
	Note           string     `gorm:"size:255;not null;default:''" json:"note"`
	WecomRenamed   bool       `gorm:"not null;default:false" json:"wecom_renamed"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
}

func (ChatQrCode) TableName() string {
	return "chat_qrcode_pool"
}

type ChatQrCodeDeliveryTask struct {
	ID             uint64     `gorm:"primaryKey" json:"id"`
	ConversationID uint64     `gorm:"column:conversation_id;not null;uniqueIndex" json:"conversation_id"`
	Status         string     `gorm:"size:16;not null;default:'pending';index" json:"status"`
	QrCodeID       *uint64    `gorm:"column:qrcode_id;index" json:"qrcode_id"`
	ErrorMessage   string     `gorm:"column:error_message;size:255;not null;default:''" json:"error_message"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
	SentAt         *time.Time `gorm:"column:sent_at" json:"sent_at"`
}

func (ChatQrCodeDeliveryTask) TableName() string {
	return "chat_qrcode_delivery_tasks"
}

type ChatSupportGroup struct {
	ID          uint64    `gorm:"primaryKey" json:"id"`
	Code        string    `gorm:"size:64;not null;uniqueIndex" json:"code"`
	Name        string    `gorm:"size:64;not null" json:"name"`
	Description string    `gorm:"size:255;not null;default:''" json:"description"`
	Status      string    `gorm:"size:16;not null;default:'active';index" json:"status"`
	SortOrder   int       `gorm:"not null;default:0" json:"sort_order"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

func (ChatSupportGroup) TableName() string {
	return "chat_support_groups"
}

type ChatSupportGroupMember struct {
	ID        uint64    `gorm:"primaryKey" json:"id"`
	GroupID   uint64    `gorm:"not null;uniqueIndex:uk_support_group_member" json:"group_id"`
	AdminID   uint64    `gorm:"column:admin_user_id;not null;uniqueIndex:uk_support_group_member;index" json:"admin_id"`
	CreatedAt time.Time `json:"created_at"`
}

func (ChatSupportGroupMember) TableName() string {
	return "chat_support_group_members"
}
