package photos

import (
	"context"
	"errors"
	"io"
	"log/slog"
	"net/http"

	"github.com/go-chi/chi/v5"

	"github.com/ultisuite/ulti-backend/internal/api/apiresponse"
	"github.com/ultisuite/ulti-backend/internal/api/apivalidate"
	"github.com/ultisuite/ulti-backend/internal/api/middleware"
	"github.com/ultisuite/ulti-backend/internal/api/query"
	"github.com/ultisuite/ulti-backend/internal/nextcloud"
	"github.com/ultisuite/ulti-backend/internal/permission"
	photospkg "github.com/ultisuite/ulti-backend/internal/photos"
)

type Handler struct {
	svc    *Service
	logger *slog.Logger
}

type contentLengthContextKey struct{}

func NewHandler(client *photospkg.Client, quota *nextcloud.Client) *Handler {
	return &Handler{
		svc:    NewService(client, quota),
		logger: slog.Default().With("component", "photos-api"),
	}
}

func (h *Handler) Routes() chi.Router {
	r := chi.NewRouter()
	read := middleware.RequirePermission(permission.ResourcePhotos, permission.LevelRead)
	write := middleware.RequirePermission(permission.ResourcePhotos, permission.LevelWrite)

	r.With(read).Get("/assets", h.ListAssets)
	r.With(read).Get("/assets/{assetID}/thumbnail", h.Thumbnail)
	r.With(read).Get("/albums", h.ListAlbums)
	r.With(read).Get("/albums/{albumID}", h.GetAlbum)
	r.With(write).Post("/assets", h.Upload)
	r.With(write).Post("/albums", h.CreateAlbum)
	r.With(write).Patch("/albums/{albumID}", h.UpdateAlbum)
	r.With(write).Delete("/assets/{assetID}", h.DeleteAsset)
	r.With(write).Delete("/albums/{albumID}", h.DeleteAlbum)
	r.With(write).Put("/albums/{albumID}/assets", h.AddAlbumAssets)
	r.With(write).Delete("/albums/{albumID}/assets", h.RemoveAlbumAssets)
	return r
}

func (h *Handler) ListAssets(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	params, err := query.ParseListRequest(r)
	if err != nil {
		apivalidate.WriteQueryError(w, r, err)
		return
	}

	result, err := h.svc.ListAssets(r.Context(), claims.Sub, params)
	if err != nil {
		h.logger.Error("list assets", "error", err)
		apivalidate.WriteInternal(w, r)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, result)
}

func (h *Handler) Upload(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	ctx := context.WithValue(r.Context(), contentLengthContextKey{}, r.ContentLength)

	id, err := h.svc.UploadAsset(ctx, claims.Sub, r.Body, r.Header.Get("Content-Type"))
	if err != nil {
		h.logger.Error("upload asset", "error", err)
		writePhotosError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusCreated, map[string]string{"id": id})
}

func (h *Handler) Thumbnail(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	assetID := chi.URLParam(r, "assetID")
	if verr := validateAssetID(assetID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	body, ct, err := h.svc.GetAssetThumbnail(r.Context(), claims.Sub, assetID)
	if err != nil {
		writePhotosError(w, r, err)
		return
	}
	defer body.Close()

	w.Header().Set("Content-Type", ct)
	io.Copy(w, body)
}

func (h *Handler) DeleteAsset(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	assetID := chi.URLParam(r, "assetID")
	if verr := validateAssetID(assetID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	if err := h.svc.DeleteAsset(r.Context(), claims.Sub, assetID); err != nil {
		h.logger.Error("delete asset", "error", err)
		writePhotosError(w, r, err)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}

func (h *Handler) ListAlbums(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	params, err := query.ParseListRequest(r)
	if err != nil {
		apivalidate.WriteQueryError(w, r, err)
		return
	}

	result, err := h.svc.ListAlbums(r.Context(), claims.Sub, params)
	if err != nil {
		h.logger.Error("list albums", "error", err)
		writePhotosError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, result)
}

func (h *Handler) GetAlbum(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	albumID := chi.URLParam(r, "albumID")
	if verr := validateAlbumID(albumID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	album, err := h.svc.GetAlbum(r.Context(), claims.Sub, albumID)
	if err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("get album", "error", err)
		writePhotosError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, album)
}

func (h *Handler) CreateAlbum(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())

	var req createAlbumRequest
	if err := apivalidate.DecodeJSON(w, r, maxJSONRequestBody, &req); err != nil {
		return
	}
	if verr := validateCreateAlbum(&req); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	album, err := h.svc.CreateAlbum(r.Context(), claims.Sub, &req)
	if err != nil {
		h.logger.Error("create album", "error", err)
		writePhotosError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusCreated, album)
}

func (h *Handler) UpdateAlbum(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	albumID := chi.URLParam(r, "albumID")
	if verr := validateAlbumID(albumID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	var req updateAlbumRequest
	if err := apivalidate.DecodeJSON(w, r, maxJSONRequestBody, &req); err != nil {
		return
	}
	if verr := validateUpdateAlbum(&req); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	album, err := h.svc.UpdateAlbum(r.Context(), claims.Sub, albumID, &req)
	if err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("update album", "error", err)
		writePhotosError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, album)
}

func (h *Handler) DeleteAlbum(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	albumID := chi.URLParam(r, "albumID")
	if verr := validateAlbumID(albumID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	if err := h.svc.DeleteAlbum(r.Context(), claims.Sub, albumID); err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("delete album", "error", err)
		writePhotosError(w, r, err)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}

func (h *Handler) AddAlbumAssets(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	albumID := chi.URLParam(r, "albumID")
	if verr := validateAlbumID(albumID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	var req albumAssetsRequest
	if err := apivalidate.DecodeJSON(w, r, maxJSONRequestBody, &req); err != nil {
		return
	}
	if verr := validateAlbumAssetsRequest(&req); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	result, err := h.svc.AddAlbumAssets(r.Context(), claims.Sub, albumID, req.AssetIDs)
	if err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("add album assets", "error", err)
		writePhotosError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, result)
}

func (h *Handler) RemoveAlbumAssets(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	albumID := chi.URLParam(r, "albumID")
	if verr := validateAlbumID(albumID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	var req albumAssetsRequest
	if err := apivalidate.DecodeJSON(w, r, maxJSONRequestBody, &req); err != nil {
		return
	}
	if verr := validateAlbumAssetsRequest(&req); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	result, err := h.svc.RemoveAlbumAssets(r.Context(), claims.Sub, albumID, req.AssetIDs)
	if err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("remove album assets", "error", err)
		writePhotosError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, result)
}

func writePhotosError(w http.ResponseWriter, r *http.Request, err error) {
	switch {
	case errors.Is(err, ErrNotFound):
		apivalidate.WriteNotFound(w, r, "not found")
	case errors.Is(err, ErrConflict):
		apiresponse.WriteError(w, r, http.StatusConflict, "photos.conflict", "resource conflict", nil)
	case errors.Is(err, ErrForbidden):
		apiresponse.WriteError(w, r, http.StatusForbidden, "photos.forbidden", "forbidden", nil)
	case errors.Is(err, ErrQuotaExceeded):
		apiresponse.WriteError(w, r, http.StatusInsufficientStorage, "photos.quota_exceeded", "quota exceeded", nil)
	case errors.Is(err, ErrInvalid):
		apiresponse.WriteError(w, r, http.StatusBadRequest, apiresponse.CodeInvalidRequest, "invalid request body", nil)
	default:
		apivalidate.WriteInternal(w, r)
	}
}
