package server

import (
	"errors"
	"net/http"

	"galaxy/backend/internal/server/handlers"
	"galaxy/backend/internal/server/httperr"
	"galaxy/backend/internal/server/middleware/userid"
	"galaxy/backend/internal/telemetry"
	"galaxy/backend/internal/user"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// UserAccountHandlers groups the handlers under `/api/v1/user/account/*`.
// The current implementation ships real implementations backed by `*user.Service`; tests
// that supply a nil service fall back to the Stage-3 placeholder body
// so the contract test continues to validate the OpenAPI envelope
// without booting a database.
type UserAccountHandlers struct {
	svc *user.Service
	logger *zap.Logger
}

// NewUserAccountHandlers constructs the handler set. svc may be nil —
// in that case every handler returns 501 not_implemented, matching the
// pre-Stage-5.2 placeholder. logger may also be nil; zap.NewNop is
// used in that case.
func NewUserAccountHandlers(svc *user.Service, logger *zap.Logger) *UserAccountHandlers {
	if logger == nil {
		logger = zap.NewNop()
	}
	return &UserAccountHandlers{svc: svc, logger: logger.Named("http.user.account")}
}

// Get handles GET /api/v1/user/account.
func (h *UserAccountHandlers) Get() gin.HandlerFunc {
	if h.svc == nil {
		return handlers.NotImplemented("userAccountGet")
	}
	return func(c *gin.Context) {
		userID, ok := userid.FromContext(c.Request.Context())
		if !ok {
			httperr.Abort(c, http.StatusBadRequest, httperr.CodeInvalidRequest, "X-User-ID header is required")
			return
		}
		ctx := c.Request.Context()
		account, err := h.svc.GetAccount(ctx, userID)
		if err != nil {
			respondAccountError(c, h.logger, "user account get", ctx, err)
			return
		}
		c.JSON(http.StatusOK, accountResponseToWire(account))
	}
}

// UpdateProfile handles PATCH /api/v1/user/account/profile.
func (h *UserAccountHandlers) UpdateProfile() gin.HandlerFunc {
	if h.svc == nil {
		return handlers.NotImplemented("userAccountUpdateProfile")
	}
	return func(c *gin.Context) {
		userID, ok := userid.FromContext(c.Request.Context())
		if !ok {
			httperr.Abort(c, http.StatusBadRequest, httperr.CodeInvalidRequest, "X-User-ID header is required")
			return
		}
		var req updateProfileRequestWire
		if err := c.ShouldBindJSON(&req); err != nil {
			httperr.Abort(c, http.StatusBadRequest, httperr.CodeInvalidRequest, "request body must be valid JSON")
			return
		}
		ctx := c.Request.Context()
		account, err := h.svc.UpdateProfile(ctx, userID, user.UpdateProfileInput{
			DisplayName: req.DisplayName,
		})
		if err != nil {
			respondAccountError(c, h.logger, "user account update profile", ctx, err)
			return
		}
		c.JSON(http.StatusOK, accountResponseToWire(account))
	}
}

// UpdateSettings handles PATCH /api/v1/user/account/settings.
func (h *UserAccountHandlers) UpdateSettings() gin.HandlerFunc {
	if h.svc == nil {
		return handlers.NotImplemented("userAccountUpdateSettings")
	}
	return func(c *gin.Context) {
		userID, ok := userid.FromContext(c.Request.Context())
		if !ok {
			httperr.Abort(c, http.StatusBadRequest, httperr.CodeInvalidRequest, "X-User-ID header is required")
			return
		}
		var req updateSettingsRequestWire
		if err := c.ShouldBindJSON(&req); err != nil {
			httperr.Abort(c, http.StatusBadRequest, httperr.CodeInvalidRequest, "request body must be valid JSON")
			return
		}
		ctx := c.Request.Context()
		account, err := h.svc.UpdateSettings(ctx, userID, user.UpdateSettingsInput{
			PreferredLanguage: req.PreferredLanguage,
			TimeZone: req.TimeZone,
		})
		if err != nil {
			respondAccountError(c, h.logger, "user account update settings", ctx, err)
			return
		}
		c.JSON(http.StatusOK, accountResponseToWire(account))
	}
}

// Delete handles POST /api/v1/user/account/delete.
func (h *UserAccountHandlers) Delete() gin.HandlerFunc {
	if h.svc == nil {
		return handlers.NotImplemented("userAccountDelete")
	}
	return func(c *gin.Context) {
		userID, ok := userid.FromContext(c.Request.Context())
		if !ok {
			httperr.Abort(c, http.StatusBadRequest, httperr.CodeInvalidRequest, "X-User-ID header is required")
			return
		}
		ctx := c.Request.Context()
		actor := user.ActorRef{Type: "user", ID: userID.String()}
		if err := h.svc.SoftDelete(ctx, userID, actor); err != nil {
			if errors.Is(err, user.ErrAccountNotFound) {
				httperr.Abort(c, http.StatusNotFound, httperr.CodeNotFound, "account not found")
				return
			}
			h.logger.Warn("user account soft-delete returned cascade errors",
				append(telemetry.TraceFieldsFromContext(ctx), zap.Error(err))...,
			)
			// Cascade errors do not change the canonical state — the
			// account is soft-deleted in Postgres. Surface 204 so the
			// caller's UI proceeds to a logged-out state.
		}
		c.Status(http.StatusNoContent)
	}
}

// updateProfileRequestWire mirrors `UpdateProfileRequest` from openapi.yaml.
type updateProfileRequestWire struct {
	DisplayName *string `json:"display_name,omitempty"`
}

// updateSettingsRequestWire mirrors `UpdateSettingsRequest` from openapi.yaml.
type updateSettingsRequestWire struct {
	PreferredLanguage *string `json:"preferred_language,omitempty"`
	TimeZone *string `json:"time_zone,omitempty"`
}
