package roomadmin

import (
	"database/sql"
	"errors"
	"strconv"
	"strings"

	"hyapp-admin-server/internal/modules/shared"
	"hyapp-admin-server/internal/response"

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

type Handler struct {
	service *Service
	audit   shared.OperationLogger
}

func New(roomDB *sql.DB, userDB *sql.DB, audit shared.OperationLogger) *Handler {
	return &Handler{service: NewService(roomDB, userDB), audit: audit}
}

func (h *Handler) ListRooms(c *gin.Context) {
	query, ok := parseListQuery(c)
	if !ok {
		return
	}
	items, total, err := h.service.ListRooms(c.Request.Context(), query)
	if err != nil {
		response.ServerError(c, "获取房间列表失败")
		return
	}
	response.OK(c, response.Page{Items: items, Page: query.Page, PageSize: query.PageSize, Total: total})
}

func (h *Handler) GetRoomConfig(c *gin.Context) {
	config, err := h.service.GetRoomConfig(c.Request.Context())
	if err != nil {
		response.ServerError(c, "获取房间配置失败")
		return
	}
	response.OK(c, config)
}

func (h *Handler) UpdateRoomConfig(c *gin.Context) {
	var req updateRoomConfigRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "房间配置参数不正确")
		return
	}
	config, err := h.service.UpdateRoomConfig(c.Request.Context(), req)
	if err != nil {
		writeMutationError(c, err, "更新房间配置失败")
		return
	}
	writeRoomAuditLog(c, h.audit, "update-room-config", "room_seat_configs", "seat-counts", "success", "update room seat config")
	response.OK(c, config)
}

func (h *Handler) UpdateRoom(c *gin.Context) {
	roomID, ok := parseRoomID(c)
	if !ok {
		return
	}
	var req updateRoomRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "房间参数不正确")
		return
	}
	room, err := h.service.UpdateRoom(c.Request.Context(), roomID, req)
	if err != nil {
		writeMutationError(c, err, "更新房间失败")
		return
	}
	writeRoomAuditLog(c, h.audit, "update-room", "rooms", roomID, "success", "update room profile")
	response.OK(c, room)
}

func (h *Handler) DeleteRoom(c *gin.Context) {
	roomID, ok := parseRoomID(c)
	if !ok {
		return
	}
	if err := h.service.DeleteRoom(c.Request.Context(), roomID); err != nil {
		writeMutationError(c, err, "删除房间失败")
		return
	}
	writeRoomAuditLog(c, h.audit, "delete-room", "rooms", roomID, "success", "delete room")
	response.OK(c, gin.H{"deleted": true})
}

func parseListQuery(c *gin.Context) (listQuery, bool) {
	options := shared.ListOptions(c)
	query := listQuery{
		Page:          options.Page,
		PageSize:      options.PageSize,
		Keyword:       options.Keyword,
		Status:        options.Status,
		SortBy:        firstQueryValue(c, "sortBy", "sort_by"),
		SortDirection: firstQueryValue(c, "sortDirection", "sort_direction", "order"),
	}
	regionID, ok := parseOptionalInt64Query(c, "regionId", "region_id")
	if !ok {
		return listQuery{}, false
	}
	query.RegionID = regionID
	return normalizeListQuery(query), true
}

func firstQueryValue(c *gin.Context, names ...string) string {
	for _, name := range names {
		if value := strings.TrimSpace(c.Query(name)); value != "" {
			return value
		}
	}
	return ""
}

func parseOptionalInt64Query(c *gin.Context, primary string, fallback string) (int64, bool) {
	raw := strings.TrimSpace(c.Query(primary))
	if raw == "" {
		raw = strings.TrimSpace(c.Query(fallback))
	}
	if raw == "" {
		return 0, true
	}
	value, err := strconv.ParseInt(raw, 10, 64)
	if err != nil || value < 0 {
		response.BadRequest(c, primary+" 参数不正确")
		return 0, false
	}
	return value, true
}

func parseRoomID(c *gin.Context) (string, bool) {
	roomID := strings.TrimSpace(c.Param("room_id"))
	if roomID == "" || len(roomID) > 64 {
		response.BadRequest(c, "房间 ID 参数不正确")
		return "", false
	}
	return roomID, true
}

func writeMutationError(c *gin.Context, err error, fallback string) {
	if errors.Is(err, ErrNotFound) {
		response.NotFound(c, "房间不存在")
		return
	}
	if errors.Is(err, ErrInvalidArgument) {
		response.BadRequest(c, err.Error())
		return
	}
	response.ServerError(c, fallback)
}

func writeRoomAuditLog(c *gin.Context, logger shared.OperationLogger, action string, resource string, resourceID string, status string, detail string) {
	shared.OperationLogWithResourceID(c, logger, action, resource, resourceID, status, detail)
}
