package controller

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

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

type agentController struct {
	agentService service.AgentService
}

type AgentController 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
	FindByAgentId(c *fiber.Ctx) error

}

func NewAgentController(agentService service.AgentService) AgentController {
	return &agentController{agentService: agentService}
}

// All godoc
// @Summary Get list agent
// @Description Get list agent
// @Tags Agent
// @Accept json
// @Produce json
// @Param name query string false "Agent name"
// @Param type query string false "Agent type"
// @Param status query string false "Agent status"
// @Success 200 {object} utilRes.Response
// @Router /agent [get]
func (_i *agentController) All(c *fiber.Ctx) error {
	statusStr := c.Query("status")
	var status *bool

if statusStr != "" {
	val, err := strconv.ParseBool(statusStr)
	if err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"message": "status must be true or false",
		})
	}
	status = &val
}
	req := request.AgentQueryRequest{
		Name: c.Query("name"),
		Type: c.Query("type"),
	Status: status,

	}

	data, err := _i.agentService.All(req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success: true,
		Data:    data,
	})
}


// Show godoc
// @Summary Get agent by ID
// @Tags Agent
// @Accept json
// @Produce json
// @Param id path int true "Agent ID"
// @Success 200 {object} utilRes.Response
// @Router /agent/{id} [get]
func (_i *agentController) Show(c *fiber.Ctx) error {
	id, _ := strconv.ParseUint(c.Params("id"), 10, 64)
	data, err := _i.agentService.Show(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{Success: true, Data: data})
}


// Save godoc
// @Summary Create agent
// @Tags Agent
// @Accept json
// @Produce json
// @Param body body request.AgentCreateRequest true "Agent payload"
// @Success 200 {object} utilRes.Response
// @Router /agent [post]
func (_i *agentController) Save(c *fiber.Ctx) error {
	req := new(request.AgentCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	data, err := _i.agentService.Save(*req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{Success: true, Data: data})
}


// Update godoc
// @Summary Update agent
// @Tags Agent
// @Accept json
// @Produce json
// @Param id path int true "Agent ID"
// @Param body body request.AgentUpdateRequest true "Agent payload"
// @Success 200 {object} utilRes.Response
// @Router /agent/{id} [put]
func (_i *agentController) Update(c *fiber.Ctx) error {
	id, _ := strconv.ParseUint(c.Params("id"), 10, 64)
	req := new(request.AgentUpdateRequest)

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

	if err := _i.agentService.Update(uint(id), *req); err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{Success: true})
}


// Delete godoc
// @Summary Delete agent
// @Tags Agent
// @Accept json
// @Produce json
// @Param id path int true "Agent ID"
// @Success 200 {object} utilRes.Response
// @Router /agent/{id} [delete]
func (_i *agentController) Delete(c *fiber.Ctx) error {
	id, _ := strconv.ParseUint(c.Params("id"), 10, 64)
	if err := _i.agentService.Delete(uint(id)); err != nil {
		return err
	}
	return utilRes.Resp(c, utilRes.Response{Success: true})
}

// Show godoc
// @Summary Get agent by AgentID
// @Tags Agent
// @Accept json
// @Produce json
// @Param agentId path string true "Agent ID"
// @Success 200 {object} utilRes.Response
// @Router /agent/agent_id/{agentId} [get]
func (_i *agentController) FindByAgentId(c *fiber.Ctx) error {
	id:= c.Params("agentId")
	data, err := _i.agentService.FindByAgentId(id)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{Success: true, Data: data})
}