package User

import (
	config "astrology-api/configs"
	dto "astrology-api/dto"
	repositories "astrology-api/repositories"
	services "astrology-api/services"
	"net/http"
	"strconv"

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

type AstrologerController struct {
	astrologerService services.AstrologerService
}

func NewAstrologerController() *AstrologerController {

	db := config.DB

	astrologerRepo := repositories.NewAstrologerRepository(db)

	return &AstrologerController{
		astrologerService: services.NewAstrologerService(astrologerRepo),
	}
}

func (c *AstrologerController) GetAstrologers(ctx *gin.Context) {

	var req dto.AstrologerListRequest

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

		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":400,
			"message":err.Error(),
		})
		return
	}

	res, err := c.astrologerService.GetAstrologers(req)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{

			"status":500,

			"message":err.Error(),
		})

		return
	}

	ctx.JSON(http.StatusOK, gin.H{

		"status":200,

		"message":"Astrologer List",

		"recordList":res,
	})
}

func (ac *AstrologerController) SearchAstrologers(c *gin.Context) {

	var req dto.SearchAstrologerRequest

		if err := c.ShouldBindJSON(&req); err != nil {

			c.JSON(http.StatusBadRequest, gin.H{
				"message": err.Error(),
				"status": 400,
			})
			return
		}

		if userID, exists := c.Get("user_id"); exists {
			req.UserID = userID.(uint)
		}

		recordList, err := ac.astrologerService.SearchAstrologers(req)

		if err != nil {

			c.JSON(http.StatusInternalServerError, gin.H{
				"message": err.Error(),
				"status": 500,
			})
			return
		}

		if recordList == nil {
			recordList = []dto.SearchAstrologerResponse{}
		}

		c.JSON(http.StatusOK, gin.H{
			"message": "Search Astrologers",
			"status": 200,
			"recordList": recordList,
		})
}

func (ac *AstrologerController) GetAstrologerByID(c *gin.Context){

	var req dto.GetAstrologerByIDRequest

	if err:=c.ShouldBindJSON(&req);err!=nil{

		c.JSON(http.StatusBadRequest,gin.H{
			"status":400,
			"message":err.Error(),
		})

		return
	}

	if userID,exists:=c.Get("user_id");exists{

		req.UserID=userID.(uint)
	}

	record,err:=ac.astrologerService.GetAstrologerByID(req)

	if err!=nil{

		c.JSON(http.StatusInternalServerError,gin.H{
			"status":500,
			"message":err.Error(),
		})

		return
	}

	c.JSON(http.StatusOK,gin.H{

		"status":200,

		"message":"Get Astrologer Profile",

		"recordList":[]interface{}{record},
	})
}
// GetTopAstrologers is the website home page's "expert astrologers, ready
// now" block: the highest rated, most attended astrologers. Public — no login.
//
// GET /api/user/astrologers/top?limit=3  (limit 1-10, default 3)
func (c *AstrologerController) GetTopAstrologers(ctx *gin.Context) {

	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "3"))

	res, err := c.astrologerService.GetTopAstrologers(limit)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status":  500,
			"message": err.Error(),
		})

		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"status":     200,
		"message":    "Top Astrologers",
		"recordList": res,
	})
}
