package controller

import (
	"narasi-ahli-be/app/module/knowledge_base/request"
	"narasi-ahli-be/app/module/knowledge_base/service"
	"narasi-ahli-be/utils/paginator"
	utilRes "narasi-ahli-be/utils/response"
	utilVal "narasi-ahli-be/utils/validator"
	"strconv"

	"github.com/gofiber/fiber/v2"
)

type KnowledgeBaseController struct {
	KnowledgeBaseService service.KnowledgeBaseServiceInterface
}

type KnowledgeBaseControllerInterface interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Create(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
	Viewer(c *fiber.Ctx) error 
	UpdateStatus(c *fiber.Ctx) error
	UpdateDocumentId(c *fiber.Ctx) error


}

func NewKnowledgeBaseController(svc service.KnowledgeBaseServiceInterface) KnowledgeBaseControllerInterface {
	return &KnowledgeBaseController{
		KnowledgeBaseService: svc,
	}
}

// @Summary      Get all KnowledgeBase
// @Description  API for getting all KnowledgeBase
// @Tags         Knowledge Base
// @Security     Bearer
// @Param        agentId query string false "Agent ID"
// @Param        title query string false "Search title"
// @Param        status query int false "Status (0=waitingdraft,1=approved,2=rejected)"
// @Param        createdById query int false "Created By ID"
// @Param        isActive query bool false "Is active"
// @Param        page query int false "Page"
// @Param        limit query int false "Limit"
// @Success      200  {object}  response.Response
// @Router       /knowledge-base [get]
func (ctl *KnowledgeBaseController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.KnowledgeBaseQueryRequestContext{
		AgentId:     c.Query("agentId"),
		Title:       c.Query("title"),
		Status:      c.Query("status"),
		IsActive:    c.Query("isActive"),
		CreatedById: c.Query("createdById"),
	}

	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	data, paging, err := ctl.KnowledgeBaseService.All(req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"KnowledgeBase list successfully retrieved"},
		Data:     data,
		Meta:     paging,
	})
}

// @Summary      Get one KnowledgeBase
// @Description  API for getting one KnowledgeBase
// @Tags         Knowledge Base
// @Security     Bearer
// @Param        id path int true "KnowledgeBase ID"
// @Success      200  {object}  response.Response
// @Router       /knowledge-base/{id} [get]
func (ctl *KnowledgeBaseController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	data, err := ctl.KnowledgeBaseService.Show(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"KnowledgeBase successfully retrieved"},
		Data:     data,
	})
}

// @Summary      Create KnowledgeBase
// @Description  API for creating KnowledgeBase with upload file
// @Tags         Knowledge Base
// @Security     Bearer
// @Accept       multipart/form-data
// @Produce      json
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
//
// @Param        agentId formData string true "Agent ID"
// @Param        agentName formData string true "Agent Name"
// @Param        createdById formData int true "Created By ID"
// @Param        status formData int true "Status (integer)"
// @Param        title formData string true "Title"
//
// @Param        fileJournal formData file false "Upload Journal File"
// @Param        fileAudio formData file false "Upload Audio File"
// @Param        fileVideo formData file false "Upload Video File"
//
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /knowledge-base [post]
func (ctl *KnowledgeBaseController) Create(c *fiber.Ctx) error {
	data, err := ctl.KnowledgeBaseService.Create(c)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"KnowledgeBase successfully created"},
		Data:     data,
	})
}

// Viewer KnowledgeBase
// @Summary      Viewer KnowledgeBase
// @Description  API for Viewer KnowledgeBase
// @Tags         Knowledge Base
// @Security     Bearer
// @Param        filename path string true "KnowledgeBase File Name"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /knowledge-base/viewer/{filename} [get]
func (ctl *KnowledgeBaseController) Viewer(c *fiber.Ctx) error {
	return ctl.KnowledgeBaseService.Viewer(c)
}

// @Summary      Update KnowledgeBase
// @Description  API for updating KnowledgeBase
// @Tags         Knowledge Base
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        payload body request.KnowledgeBaseUpdateRequest true "Required payload"
// @Param        id path int true "KnowledgeBase ID"
// @Success      200  {object}  response.Response
// @Router       /knowledge-base/{id} [put]
func (ctl *KnowledgeBaseController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	req := new(request.KnowledgeBaseUpdateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	data, err := ctl.KnowledgeBaseService.Update(uint(id), *req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"KnowledgeBase successfully updated"},
		Data:     data,
	})
}

// @Summary      Delete KnowledgeBase
// @Description  API for deleting KnowledgeBase (soft delete)
// @Tags         Knowledge Base
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        id path int true "KnowledgeBase ID"
// @Success      200  {object}  response.Response
// @Router       /knowledge-base/{id} [delete]
func (ctl *KnowledgeBaseController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	err = ctl.KnowledgeBaseService.Delete(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"KnowledgeBase successfully deleted"},
	})
}

// @Summary      Update KnowledgeBase Status
// @Description  API untuk update status KnowledgeBase
// @Tags         Knowledge Base
// @Security     Bearer
// @Param        id path int true "KnowledgeBase ID"
// @Param        payload body request.KnowledgeBaseUpdateStatusRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Router       /knowledge-base/{id}/status [patch]
func (ctl *KnowledgeBaseController) UpdateStatus(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	req := new(request.KnowledgeBaseUpdateStatusRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	data, err := ctl.KnowledgeBaseService.UpdateStatus(uint(id), req.Status)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"KnowledgeBase status successfully updated"},
		Data:     data,
	})
}

// @Summary      Update KnowledgeBase DocumentId
// @Description  API untuk menambahkan/mengubah documentId ke KnowledgeBase
// @Tags         Knowledge Base
// @Security     Bearer
// @Param        id path int true "KnowledgeBase ID"
// @Param        payload body request.KnowledgeBaseUpdateDocumentRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Router       /knowledge-base/{id}/document [patch]
func (ctl *KnowledgeBaseController) UpdateDocumentId(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	req := new(request.KnowledgeBaseUpdateDocumentRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	data, err := ctl.KnowledgeBaseService.UpdateDocumentId(uint(id), req.DocumentId)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"KnowledgeBase documentId successfully updated"},
		Data:     data,
	})
}