package controllers

import (
	"errors"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"github.com/lib/pq"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
	"strconv"
)

func (c *Controller) ChangeQuizSettings(ctx *fiber.Ctx) error {
	accountID, ok := middleware.GetAccountId(ctx)
	if !ok {
		return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
	}

	quizID := ctx.Params("quizID")
	if quizID == "" {
		return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
	}

	quizIDInt, err := strconv.Atoi(quizID)
	if err != nil {
		return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
	}

	//accountID := "64f2cd7a7047f28fdabf6d9e"

	var request model.RulesReq
	if err := ctx.BodyParser(&request); err != nil {
		return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
	}

	err = c.service.ChangeQuizSettings(ctx.Context(), &request, accountID, quizIDInt)
	if err != nil {
		switch {
		case errors.Is(err, pj_errors.ErrNotFound):
			return ctx.Status(fiber.StatusNotFound).SendString("rule not found")
		default:
			return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
		}
	}
	return ctx.SendStatus(fiber.StatusOK)
}

func (c *Controller) SetQuizSettings(ctx *fiber.Ctx) error {
	accountID, ok := middleware.GetAccountId(ctx)
	if !ok {
		return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
	}

	quizID := ctx.Params("quizID")
	if quizID == "" {
		return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
	}

	quizIDInt, err := strconv.Atoi(quizID)
	if err != nil {
		return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
	}

	//accountID := "64f2cd7a7047f28fdabf6d9e"

	var request model.RulesReq
	if err := ctx.BodyParser(&request); err != nil {
		return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
	}

	err = c.service.SetQuizSettings(ctx.Context(), &request, accountID, quizIDInt)
	if err != nil {
		pqErr, ok := err.(*pq.Error)
		if ok && pqErr.Code == "23505" {
			return ctx.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("quiz settings already exist for accountID %s and quizID %d", accountID, quizIDInt))
		}
		switch {
		case errors.Is(err, pj_errors.ErrNotFound):
			return ctx.Status(fiber.StatusNotFound).SendString("not found user for this rule")
		default:
			return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
		}
	}
	return ctx.SendStatus(fiber.StatusOK)
}

func (c *Controller) GettingQuizRules(ctx *fiber.Ctx) error {
	quizID := ctx.Params("quizID")
	if quizID == "" {
		return ctx.Status(fiber.StatusBadRequest).SendString("quizID is nil")
	}

	quizIDInt, err := strconv.Atoi(quizID)
	if err != nil {
		return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
	}

	response, err := c.service.GettingQuizRules(ctx.Context(), quizIDInt)

	if err != nil {
		switch {
		case errors.Is(err, pj_errors.ErrNotFound):
			return ctx.Status(fiber.StatusNotFound).SendString("rule not found")
		default:
			return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
		}
	}
	return ctx.Status(fiber.StatusOK).JSON(response)
}
