package Astrologer

import (
	config "astrology-api/configs"
	dto "astrology-api/dto_astrologer/activity"
	"astrology-api/helpers"
	services "astrology-api/services_astrologer"

	"log"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

///////////////////////////////////////////////////////////
// Activity Controller
///////////////////////////////////////////////////////////

type ActivityController struct {
	service services.ActivityService
}

///////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////

func NewActivityController(service services.ActivityService) *ActivityController {

	log.Printf("config.DB = %v", config.DB)

	return &ActivityController{
		service: service,
	}
}

///////////////////////////////////////////////////////////
// Add Waiting User
///////////////////////////////////////////////////////////

func (c *ActivityController) AddWaiting(ctx *gin.Context) {

	// Request
	var request dto.AddWaitingRequest

	if err := ctx.ShouldBindJSON(&request); err != nil {

		ctx.JSON(
			http.StatusBadRequest,
			helpers.ErrorResponse(err.Error()),
		)

		return
	}

	// Login User
	astrologerID := ctx.MustGet("user_id").(uint)

	// Service
	err := c.service.AddWaiting(
		astrologerID,
		request,
	)

	if err != nil {

		ctx.JSON(
			http.StatusBadRequest,
			helpers.ErrorResponse(err.Error()),
		)

		return
	}

	// Success

	ctx.JSON(
		http.StatusOK,
		helpers.SuccessResponse(
			"User added into waiting queue successfully.",
			nil,
		),
	)
}

///////////////////////////////////////////////////////////
// Waiting List
///////////////////////////////////////////////////////////

func (c *ActivityController) GetWaitingList(ctx *gin.Context) {

	astrologerID := ctx.MustGet("user_id").(uint)
	// Pagination
	request := helpers.GetPaginationRequest(ctx)

	// Service

	response, err := c.service.GetWaitingList(

		astrologerID,

		request,
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse(err.Error()),
		)

		return
	}

	// Success

	ctx.JSON(

		http.StatusOK,

		helpers.SuccessResponse(

			"Waiting list fetched successfully.",

			response,
		),
	)
}

///////////////////////////////////////////////////////////
// Remove Waiting
///////////////////////////////////////////////////////////

func (c *ActivityController) RemoveWaiting(ctx *gin.Context) {

	// Queue ID

	id, err := strconv.ParseUint(

		ctx.Param("id"),

		10,

		64,
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse("invalid queue id"),
		)

		return
	}

	// Service

	err = c.service.RemoveWaiting(

		ctx.MustGet("user_id").(uint),

		uint(id),
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse(err.Error()),
		)

		return
	}

	// Success

	ctx.JSON(

		http.StatusOK,

		helpers.SuccessResponse(

			"Waiting user removed successfully.",

			nil,
		),
	)
}

///////////////////////////////////////////////////////////
// Get Chat History
// GET /api/astrologer/activity/chat/history
///////////////////////////////////////////////////////////

func (c *ActivityController) GetChatHistory(
	ctx *gin.Context,
) {

	// Logged In Astrologer

	astrologerID := ctx.MustGet("user_id").(uint)

	// Default Pagination

	request := helpers.GetPaginationRequest(ctx)

	// Service

	response, err := c.service.GetChatHistory(

		astrologerID,

		request,
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse(
				err.Error(),
			),
		)

		return
	}

	// Success

	ctx.JSON(

		http.StatusOK,

		helpers.SuccessResponse(

			"Chat history fetched successfully.",

			response,
		),
	)
}

///////////////////////////////////////////////////////////
// Get Chat Detail
///////////////////////////////////////////////////////////

func (c *ActivityController) GetChatDetail(
	ctx *gin.Context,
) {

	// Logged In Astrologer

	astrologerID := ctx.MustGet("user_id").(uint)

	// Chat ID

	chatID, err := strconv.ParseUint(
		ctx.Param("id"),
		10,
		64,
	)

	if err != nil {

		ctx.JSON(
			http.StatusBadRequest,
			helpers.ErrorResponse("invalid chat id"),
		)

		return
	}

	// Service

	response, err := c.service.GetChatDetail(
		astrologerID,
		uint(chatID),
	)

	if err != nil {

		ctx.JSON(
			http.StatusBadRequest,
			helpers.ErrorResponse(err.Error()),
		)

		return
	}

	// Success Response

	ctx.JSON(
		http.StatusOK,
		helpers.SuccessResponse(
			"Chat detail fetched successfully.",
			response,
		),
	)
}

///////////////////////////////////////////////////////////
// Get Call History
// GET /api/astrologer/activity/call/history
///////////////////////////////////////////////////////////

func (c *ActivityController) GetCallHistory(
	ctx *gin.Context,
) {

	// Logged In Astrologer

	astrologerID := ctx.MustGet("user_id").(uint)

	// Default Pagination

	request := helpers.GetPaginationRequest(ctx)

	// Request DTO

	// Service

	response, err := c.service.GetCallHistory(

		astrologerID,

		request,
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse(
				err.Error(),
			),
		)

		return
	}

	// Success

	ctx.JSON(

		http.StatusOK,

		helpers.SuccessResponse(

			"Call history fetched successfully.",

			response,
		),
	)
}

///////////////////////////////////////////////////////////
// Get Call Detail
// GET /api/astrologer/activity/call/history/:id
///////////////////////////////////////////////////////////

func (c *ActivityController) GetCallDetail(ctx *gin.Context) {

	// Logged In Astrologer

	astrologerID := ctx.MustGet("user_id").(uint)

	// Call ID

	callID, err := strconv.ParseUint(

		ctx.Param("id"),

		10,

		64,
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse(
				"invalid call id",
			),
		)

		return
	}

	// Service

	response, err := c.service.GetCallDetail(

		astrologerID,

		uint(callID),
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse(
				err.Error(),
			),
		)

		return
	}

	// Success

	ctx.JSON(

		http.StatusOK,

		helpers.SuccessResponse(

			"Call detail fetched successfully.",

			response,
		),
	)
}

///////////////////////////////////////////////////////////
// Get AstroMall Orders
// GET /api/astrologer/activity/astromall/orders
///////////////////////////////////////////////////////////

func (c *ActivityController) GetAstromallOrders(
	ctx *gin.Context,
) {

	// Logged In Astrologer

	astrologerID := ctx.MustGet("user_id").(uint)

	// Default Pagination

	request := helpers.GetPaginationRequest(ctx)

	// Service

	response, err := c.service.GetAstromallOrders(

		astrologerID,

		request,
	)

	if err != nil {

		ctx.JSON(

			http.StatusBadRequest,

			helpers.ErrorResponse(
				err.Error(),
			),
		)

		return
	}

	// Success Response

	ctx.JSON(

		http.StatusOK,

		helpers.SuccessResponse(

			"AstroMall orders fetched successfully.",

			response,
		),
	)
}

///////////////////////////////////////////////////////////

// Get AstroMall Order Detail

///////////////////////////////////////////////////////////

func (c *ActivityController) GetAstromallOrderDetail(ctx *gin.Context) {

	// Logged In Astrologer

	astrologerID := ctx.MustGet("user_id").(uint)
	// Order ID
	orderID, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		ctx.JSON(
			http.StatusBadRequest,
			helpers.ErrorResponse(
				"invalid order id",
			),
		)
		return
	}
	// Service
	response, err := c.service.GetAstromallOrderDetail(
		astrologerID,
		uint(orderID),
	)
	if err != nil {
		ctx.JSON(
			http.StatusBadRequest,
			helpers.ErrorResponse(err.Error()),
		)
		return
	}

	// Success Response

	ctx.JSON(

		http.StatusOK,

		helpers.SuccessResponse(

			"AstroMall order detail fetched successfully.",

			response,
		),
	)
}
