package controller

import (
	"narasi-ahli-be/app/module/work_history/request"
	"narasi-ahli-be/app/module/work_history/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 workHistoryController struct {
	workHistoryService service.WorkHistoryService
}

type WorkHistoryController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewWorkHistoryController(workHistoryService service.WorkHistoryService) WorkHistoryController {
	return &workHistoryController{
		workHistoryService: workHistoryService,
	}
}

// All Work History
// @Summary      Get all Work History
// @Description  API for getting all Work History for specific user
// @Tags         Work History
// @Param        req query request.WorkHistoryQueryRequest false "query parameters"
// @Param        req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      500  {object}  response.InternalServerError
// @Router       /work-history [get]
func (_i *workHistoryController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}
	reqContext := request.WorkHistoryQueryRequestContext{
		JobTitle:    c.Query("jobTitle"),
		CompanyName: c.Query("companyName"),
		IsCurrent:   c.Query("isCurrent"),
		UserID:      c.Query("userId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	workData, paging, err := _i.workHistoryService.All(req)
	if err != nil {
		return err
	}

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

// Show Work History
// @Summary      Get one Work History
// @Description  API for getting one Work History
// @Tags         Work History
// @Param        id path int true "Work History ID"
// @Param        userId query uint true "User ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      500  {object}  response.InternalServerError
// @Router       /work-history/{id} [get]
func (_i *workHistoryController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	userId, err := strconv.ParseUint(c.Query("userId"), 10, 0)
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"userId parameter is required and must be a valid number"},
		})
	}

	workData, err := _i.workHistoryService.Show(uint(userId), uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Work history successfully retrieved"},
		Data:     workData,
	})
}

// Save Work History
// @Summary      Create Work History
// @Description  API for create Work History
// @Tags         Work History
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        userId query uint true "User ID"
// @Param        payload body request.WorkHistoryCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      500  {object}  response.InternalServerError
// @Router       /work-history [post]
func (_i *workHistoryController) Save(c *fiber.Ctx) error {
	userId, err := strconv.ParseUint(c.Query("userId"), 10, 0)
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"userId parameter is required and must be a valid number"},
		})
	}

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

	dataResult, err := _i.workHistoryService.Save(uint(userId), *req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Work history successfully created"},
		Data:     dataResult,
	})
}

// Update Work History
// @Summary      Update Work History
// @Description  API for update Work History
// @Tags         Work History
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        id path int true "Work History ID"
// @Param        userId query uint true "User ID"
// @Param        payload body request.WorkHistoryUpdateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      500  {object}  response.InternalServerError
// @Router       /work-history/{id} [put]
func (_i *workHistoryController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	userId, err := strconv.ParseUint(c.Query("userId"), 10, 0)
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"userId parameter is required and must be a valid number"},
		})
	}

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

	err = _i.workHistoryService.Update(uint(userId), uint(id), *req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Work history successfully updated"},
	})
}

// Delete Work History
// @Summary      Delete Work History
// @Description  API for delete Work History
// @Tags         Work History
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        id path int true "Work History ID"
// @Param        userId query uint true "User ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      500  {object}  response.InternalServerError
// @Router       /work-history/{id} [delete]
func (_i *workHistoryController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	userId, err := strconv.ParseUint(c.Query("userId"), 10, 0)
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"userId parameter is required and must be a valid number"},
		})
	}

	err = _i.workHistoryService.Delete(uint(userId), uint(id))
	if err != nil {
		return err
	}

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