package request

import (
	"narasi-ahli-be/app/database/entity"
	"narasi-ahli-be/utils/paginator"
	"strconv"
	"time"
)

// Chat Session Requests
type ChatSessionQueryRequest struct {
	Type       *string               `json:"type"` // 'personal' or 'group'
	Pagination *paginator.Pagination `json:"pagination"`
}

type ChatSessionCreateRequest struct {
	Name    *string `json:"name" validate:"omitempty,min=2,max=255"` // null for personal chat
	Type    string  `json:"type" validate:"required,oneof=personal group"`
	UserIDs []uint  `json:"userIds" validate:"required,min=1"` // participants (excluding creator)
}

func (req ChatSessionCreateRequest) ToEntity(createdBy uint) *entity.ChatSessions {
	return &entity.ChatSessions{
		Name:      req.Name,
		Type:      req.Type,
		CreatedBy: createdBy,
	}
}

type ChatSessionUpdateRequest struct {
	Name *string `json:"name" validate:"omitempty,min=2,max=255"`
}

func (req ChatSessionUpdateRequest) ToEntity() *entity.ChatSessions {
	return &entity.ChatSessions{
		Name: req.Name,
	}
}

type ChatSessionQueryRequestContext struct {
	Type string `json:"type"`
}

func (req ChatSessionQueryRequestContext) ToParamRequest() ChatSessionQueryRequest {
	var request ChatSessionQueryRequest

	if chatType := req.Type; chatType != "" {
		request.Type = &chatType
	}

	return request
}

// Chat Message Requests
type ChatMessageQueryRequest struct {
	ChatSessionID uint                  `json:"chatSessionId" validate:"required"`
	UserID        uint                  `json:"userId"` // Will be set in service layer
	Pagination    *paginator.Pagination `json:"pagination"`
}

type ChatMessageCreateRequest struct {
	ChatSessionID uint   `json:"chatSessionId" validate:"required"`
	Message       string `json:"message" validate:"required,min=1,max=1000"`
	MessageType   string `json:"messageType" validate:"omitempty,oneof=text image file user assistant"`
}

func (req ChatMessageCreateRequest) ToEntity(senderID uint) *entity.ChatMessages {
	messageType := req.MessageType
	if messageType == "" {
		messageType = "text"
	}

	return &entity.ChatMessages{
		ChatSessionID: req.ChatSessionID,
		SenderID:      senderID,
		Message:       req.Message,
		MessageType:   messageType,
	}
}

type ChatMessageUpdateRequest struct {
	Message string `json:"message" validate:"required,min=1,max=1000"`
}

func (req ChatMessageUpdateRequest) ToEntity() *entity.ChatMessages {
	return &entity.ChatMessages{
		Message:  req.Message,
		IsEdited: true,
		EditedAt: &time.Time{},
	}
}

type ChatMessageQueryRequestContext struct {
	ChatSessionID string `json:"chatSessionId"`
}

func (req ChatMessageQueryRequestContext) ToParamRequest() ChatMessageQueryRequest {
	var request ChatMessageQueryRequest

	if chatSessionId := req.ChatSessionID; chatSessionId != "" {
		chatSessionIdUint, err := strconv.ParseUint(chatSessionId, 10, 0)
		if err == nil {
			request.ChatSessionID = uint(chatSessionIdUint)
		}
	}

	return request
}

// Chat Participant Requests
type ChatParticipantCreateRequest struct {
	ChatSessionID uint `json:"chatSessionId" validate:"required"`
	UserID        uint `json:"userId" validate:"required"`
}

func (req ChatParticipantCreateRequest) ToEntity() *entity.ChatParticipants {
	return &entity.ChatParticipants{
		ChatSessionID: req.ChatSessionID,
		UserID:        req.UserID,
		IsActive:      true,
	}
}
