package handlers

import (
	"net/http"
	"strconv"

	"github.com/labstack/echo/v4"

	"github.com/treytartt/honeydue-api/internal/apperrors"
	"github.com/treytartt/honeydue-api/internal/dto/requests"
	"github.com/treytartt/honeydue-api/internal/dto/responses"
	"github.com/treytartt/honeydue-api/internal/i18n"
	"github.com/treytartt/honeydue-api/internal/middleware"
	"github.com/treytartt/honeydue-api/internal/services"
)

// ContractorHandler handles contractor-related HTTP requests
type ContractorHandler struct {
	contractorService *services.ContractorService
}

// NewContractorHandler creates a new contractor handler
func NewContractorHandler(contractorService *services.ContractorService) *ContractorHandler {
	return &ContractorHandler{contractorService: contractorService}
}

// ListContractors handles GET /api/contractors/
func (h *ContractorHandler) ListContractors(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	response, err := h.contractorService.ListContractors(user.ID)
	if err != nil {
		return apperrors.Internal(err)
	}
	return c.JSON(http.StatusOK, response)
}

// GetContractor handles GET /api/contractors/:id/
func (h *ContractorHandler) GetContractor(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	contractorID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		return apperrors.BadRequest("error.invalid_contractor_id")
	}

	response, err := h.contractorService.GetContractor(uint(contractorID), user.ID)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, response)
}

// CreateContractor handles POST /api/contractors/
func (h *ContractorHandler) CreateContractor(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	var req requests.CreateContractorRequest
	if err := c.Bind(&req); err != nil {
		return apperrors.BadRequest("error.invalid_request")
	}
	if err := c.Validate(&req); err != nil {
		return err
	}

	response, err := h.contractorService.CreateContractor(&req, user.ID)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusCreated, response)
}

// UpdateContractor handles PUT/PATCH /api/contractors/:id/
func (h *ContractorHandler) UpdateContractor(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	contractorID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		return apperrors.BadRequest("error.invalid_contractor_id")
	}

	var req requests.UpdateContractorRequest
	if err := c.Bind(&req); err != nil {
		return apperrors.BadRequest("error.invalid_request")
	}
	if err := c.Validate(&req); err != nil {
		return err
	}

	response, err := h.contractorService.UpdateContractor(uint(contractorID), user.ID, &req)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, response)
}

// DeleteContractor handles DELETE /api/contractors/:id/
func (h *ContractorHandler) DeleteContractor(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	contractorID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		return apperrors.BadRequest("error.invalid_contractor_id")
	}

	err = h.contractorService.DeleteContractor(uint(contractorID), user.ID)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, responses.MessageResponse{Message: i18n.LocalizedMessage(c, "message.contractor_deleted")})
}

// ToggleFavorite handles POST /api/contractors/:id/toggle-favorite/
func (h *ContractorHandler) ToggleFavorite(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	contractorID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		return apperrors.BadRequest("error.invalid_contractor_id")
	}

	response, err := h.contractorService.ToggleFavorite(uint(contractorID), user.ID)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, response)
}

// GetContractorTasks handles GET /api/contractors/:id/tasks/
func (h *ContractorHandler) GetContractorTasks(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	contractorID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		return apperrors.BadRequest("error.invalid_contractor_id")
	}

	response, err := h.contractorService.GetContractorTasks(uint(contractorID), user.ID)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, response)
}

// ListContractorsByResidence handles GET /api/contractors/by-residence/:residence_id/
func (h *ContractorHandler) ListContractorsByResidence(c echo.Context) error {
	user, err := middleware.MustGetAuthUser(c)
	if err != nil {
		return err
	}
	residenceID, err := strconv.ParseUint(c.Param("residence_id"), 10, 32)
	if err != nil {
		return apperrors.BadRequest("error.invalid_residence_id")
	}

	response, err := h.contractorService.ListContractorsByResidence(uint(residenceID), user.ID)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, response)
}

// GetSpecialties handles GET /api/contractors/specialties/
func (h *ContractorHandler) GetSpecialties(c echo.Context) error {
	specialties, err := h.contractorService.GetSpecialties()
	if err != nil {
		return apperrors.Internal(err)
	}
	return c.JSON(http.StatusOK, specialties)
}
