package controllers

import (
	"nearle/services"
	"net/http"
	"strconv"

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

type PartnerController struct {
	partnerService services.PartnerService
}

func NewPartnerController(partnerService services.PartnerService) *PartnerController {
	return &PartnerController{partnerService: partnerService}
}

func (ctl *PartnerController) GetActiveRiders(c *fiber.Ctx) error {
	pid, _ := strconv.Atoi(c.Query("partnerid"))
	aid, _ := strconv.Atoi(c.Query("applocationid"))
	uid, _ := strconv.Atoi(c.Query("userid"))
	tid, _ := strconv.Atoi(c.Query("tenantid"))

	if pid == 0 && aid == 0 && uid == 0 && tid == 0 {
		return c.Status(http.StatusBadRequest).JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusBadRequest,
			"message": "At least one of tenantid, partnerid, applocationid or userid is required",
		})
	}

	result, err := ctl.partnerService.GetActiveRiders(pid, aid, uid, tid)

	if err != nil {
		return c.JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
		})
	}

	return c.JSON(fiber.Map{
		"status":  true,
		"code":    http.StatusOK,
		"message": "Successful",
		"details": result,
	})
}

func (ctl *PartnerController) GetPartners(c *fiber.Ctx) error {
	pid, _ := strconv.Atoi(c.Query("partnerid"))
	aid, _ := strconv.Atoi(c.Query("applocationid"))
	uid, _ := strconv.Atoi(c.Query("userid"))

	result, err := ctl.partnerService.GetPartners(aid, pid, uid)

	if err != nil {
		return c.JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
		})
	}

	return c.JSON(fiber.Map{
		"status":  true,
		"code":    http.StatusOK,
		"message": "Successful",
		"details": result,
	})
}

func (ctl *PartnerController) GetRiderShifts(c *fiber.Ctx) error {

	aid, _ := strconv.Atoi(c.Query("applocationid"))

	result, err := ctl.partnerService.GetRiderShifts(aid)

	if err != nil {
		return c.JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
		})
	}

	return c.JSON(fiber.Map{
		"status":  true,
		"code":    http.StatusOK,
		"message": "Successful",
		"details": result,
	})

}

func (ctl *PartnerController) GetLocationConfig(c *fiber.Ctx) error {

	uid, _ := strconv.Atoi(c.Query("userid"))
	cid, _ := strconv.Atoi(c.Query("configid"))

	result, err := ctl.partnerService.GetLocationConfig(uid, cid)

	if err != nil {
		return c.JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
		})
	}

	return c.JSON(fiber.Map{
		"status":  true,
		"code":    http.StatusOK,
		"message": "Successful",
		"details": result,
	})

}

func (ctl *PartnerController) GetRiderLogs(c *fiber.Ctx) error {
	pid, _ := strconv.Atoi(c.Query("partnerid"))
	aid, _ := strconv.Atoi(c.Query("applocationid"))
	fdate := c.Query("fromdate")
	tdate := c.Query("todate")

	if pid == 0 && aid == 0 {
		return c.Status(http.StatusBadRequest).JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusBadRequest,
			"message": "At least one of partnerid or applocationid is required",
		})
	}

	data, err := ctl.partnerService.GetRiderLogs(pid, aid, fdate, tdate)

	if err != nil {
		return c.JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
		})
	}

	return c.JSON(fiber.Map{
		"status":  true,
		"code":    http.StatusOK,
		"message": "Successful",
		"details": data,
	})
}

func (ctl *PartnerController) GetFleetSummary(c *fiber.Ctx) error {
	aid, _ := strconv.Atoi(c.Query("applocationid"))
	tid, _ := strconv.Atoi(c.Query("tenantid"))
	fdate := c.Query("fromdate")
	tdate := c.Query("todate")

	result, err := ctl.partnerService.GetFleetSummary(aid, tid, fdate, tdate)

	if err != nil {
		return c.JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
		})
	}

	return c.JSON(fiber.Map{
		"status":  true,
		"code":    http.StatusOK,
		"message": "Successful",
		"details": result,
	})
}

func (ctl *PartnerController) GetRiderInfo(c *fiber.Ctx) error {
	uid, _ := strconv.Atoi(c.Query("userid"))

	result, err := ctl.partnerService.GetRiderInfo(uid)
	if err != nil {
		return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
			"status":  false,
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
		})
	}

	return c.JSON(fiber.Map{
		"status":  true,
		"code":    http.StatusOK,
		"message": "Successful",
		"details": result,
	})
}
