package request

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

type KnowledgeBaseGeneric interface {
	ToEntity() *entity.KnowledgeBase
}

type KnowledgeBaseQueryRequest struct {
	AgentId     *string               `json:"agentId"`
	Title       *string               `json:"title"`
	Status      *int                  `json:"status"`
	IsActive    *bool                 `json:"isActive"`
	CreatedById *uint                 `json:"createdById"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type KnowledgeBaseUpdateStatusRequest struct {
	Status int `json:"status" validate:"required"`
}

type KnowledgeBaseUpdateDocumentRequest struct {
	DocumentId int `json:"documentId" validate:"required"`
}

type KnowledgeBaseCreateRequest struct {
	AgentId     *string `form:"agentId" validate:"required"`
	AgentName   *string `form:"agentName" validate:"required"`
	CreatedById uint    `form:"createdById" validate:"required"`
	Title       *string `form:"title" validate:"required"`
	Status      int     `form:"status" validate:"required"`
}

func (req KnowledgeBaseCreateRequest) ToEntity() *entity.KnowledgeBase {
	kb := &entity.KnowledgeBase{
		AgentId:       req.AgentId,
		AgentName:     req.AgentName,
		CreatedById:   req.CreatedById,
		Title:         req.Title,
		Status:        req.Status,
		
	}

	return kb
}

type KnowledgeBaseUpdateRequest struct {
	ID uint `json:"id" validate:"required"`

	AgentId     *string `json:"agentId" validate:"required"`
	AgentName   *string `json:"agentName" validate:"required"`
	Title       *string `json:"title" validate:"required"`
	Status      int     `json:"status" validate:"required"`

	FileJournalUrl *string `json:"fileJournalUrl"`
	FileAudioUrl   *string `json:"fileAudioUrl"`
	FileVideoUrl   *string `json:"fileVideoUrl"`

	IsActive *bool `json:"isActive"`
}

func (req KnowledgeBaseUpdateRequest) ToEntity() *entity.KnowledgeBase {
	kb := &entity.KnowledgeBase{
		ID:            req.ID,
		AgentId:       req.AgentId,
		AgentName:     req.AgentName,
		Title:         req.Title,
		Status:        req.Status,
		FileJournalUrl: req.FileJournalUrl,
		FileAudioUrl:   req.FileAudioUrl,
		FileVideoUrl:   req.FileVideoUrl,
		UpdatedAt:     time.Now(),
	}

	if req.IsActive != nil {
		kb.IsActive = *req.IsActive
	}

	return kb
}

type KnowledgeBaseQueryRequestContext struct {
	AgentId     string `json:"agentId"`
	Title       string `json:"title"`
	Status      string `json:"status"`
	IsActive    string `json:"isActive"`
	CreatedById string `json:"createdById"`
}

func (req KnowledgeBaseQueryRequestContext) ToParamRequest() KnowledgeBaseQueryRequest {
	var request KnowledgeBaseQueryRequest

	if agentId := req.AgentId; agentId != "" {
		request.AgentId = &agentId
	}

	if title := req.Title; title != "" {
		request.Title = &title
	}

	if statusStr := req.Status; statusStr != "" {
		status, err := strconv.Atoi(statusStr)
		if err == nil {
			request.Status = &status
		}
	}

	if isActiveStr := req.IsActive; isActiveStr != "" {
		isActive, err := strconv.ParseBool(isActiveStr)
		if err == nil {
			request.IsActive = &isActive
		}
	}

	if createdByIdStr := req.CreatedById; createdByIdStr != "" {
		id64, err := strconv.ParseUint(createdByIdStr, 10, 32)
		if err == nil {
			uid := uint(id64)
			request.CreatedById = &uid
		}
	}

	return request
}
