package drive

import (
	"io"
	"net/http"
	"strconv"
	"strings"

	"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/driveroot"
	"github.com/ultisuite/ulti-backend/internal/nextcloud"
)

func (h *Handler) registerOrgAndMountRoutes(r chi.Router, read, write func(http.Handler) http.Handler) {
	r.With(read).Get("/org-folders", h.ListOrgFolders)
	r.With(read).Get("/org-folders/{folderID}/files/*", h.ListOrgFolderFiles)
	r.With(read).Get("/org-folders/{folderID}/files/info/*", h.GetOrgFolderFileInfo)
	r.With(read).Get("/org-folders/{folderID}/download/*", h.DownloadOrgFolderFile)
	r.With(read).Get("/org-folders/{folderID}/preview/*", h.PreviewOrgFolderFile)
	r.With(write).Post("/org-folders/{folderID}/files/*", h.UploadOrgFolderFile)
	r.With(write).Post("/org-folders/{folderID}/folders/*", h.CreateOrgFolderDir)
	r.With(write).Delete("/org-folders/{folderID}/files/*", h.DeleteOrgFolderFile)

	r.With(read).Get("/mounts", h.ListMounts)
	r.With(write).Post("/mounts", h.CreateMount)
	r.With(write).Delete("/mounts/{mountID}", h.DeleteMount)
	r.With(read).Get("/mounts/{mountID}/oauth-url", h.GetMountOAuthURL)
	r.With(write).Post("/mounts/{mountID}/oauth/complete", h.CompleteMountOAuth)
	r.With(read).Get("/mounts/{mountID}/files/*", h.ListMountFiles)
	r.With(read).Get("/mounts/{mountID}/files/info/*", h.GetMountFileInfo)
	r.With(read).Get("/mounts/{mountID}/files/external-url/*", h.GetMountFileExternalURL)
	r.With(read).Get("/mounts/{mountID}/download/*", h.DownloadMountFile)
	r.With(read).Get("/mounts/{mountID}/preview/*", h.PreviewMountFile)
	r.With(write).Post("/mounts/{mountID}/files/*", h.UploadMountFile)
	r.With(write).Post("/mounts/{mountID}/folders/*", h.CreateMountDir)
	r.With(write).Delete("/mounts/{mountID}/files/*", h.DeleteMountFile)
}

func (h *Handler) ListOrgFolders(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	folders, err := h.svc.ListOrgFoldersForUser(r.Context(), ncUser)
	if err != nil {
		h.logger.Error("list org folders", "error", err)
		writeDriveError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"folders": folders})
}

func (h *Handler) ListOrgFolderFiles(w http.ResponseWriter, r *http.Request) {
	h.listRootFiles(w, r, driveroot.KindOrg, chi.URLParam(r, "folderID"))
}

func (h *Handler) ListMountFiles(w http.ResponseWriter, r *http.Request) {
	h.listRootFiles(w, r, driveroot.KindMount, chi.URLParam(r, "mountID"))
}

func (h *Handler) listRootFiles(w http.ResponseWriter, r *http.Request, kind driveroot.Kind, rootID string) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	params, err := query.ParseListRequest(r)
	if err != nil {
		apivalidate.WriteQueryError(w, r, err)
		return
	}
	path := nextcloud.NormalizeClientPath(chi.URLParam(r, "*"))
	ref := driveroot.Ref{Kind: kind, RootID: rootID, Path: path}
	result, err := h.svc.ListFilesAtRoot(r.Context(), ncUser, ref, params)
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	h.svc.EnrichSources(r.Context(), claims.Sub, result.Files)
	apiresponse.WriteJSON(w, http.StatusOK, result)
}

func (h *Handler) GetOrgFolderFileInfo(w http.ResponseWriter, r *http.Request) {
	h.getRootFileInfo(w, r, driveroot.KindOrg, chi.URLParam(r, "folderID"))
}

func (h *Handler) GetMountFileInfo(w http.ResponseWriter, r *http.Request) {
	h.getRootFileInfo(w, r, driveroot.KindMount, chi.URLParam(r, "mountID"))
}

func (h *Handler) GetMountFileExternalURL(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	platformUserID, err := h.svc.platformUserID(r.Context(), claims.Sub)
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	mountID := chi.URLParam(r, "mountID")
	path := nextcloud.NormalizeClientPath(chi.URLParam(r, "*"))
	externalURL, err := h.svc.ResolveMountExternalURL(r.Context(), ncUser, mountID, platformUserID, path)
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"external_url": externalURL})
}

func (h *Handler) getRootFileInfo(w http.ResponseWriter, r *http.Request, kind driveroot.Kind, rootID string) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	path := nextcloud.NormalizeClientPath(chi.URLParam(r, "*"))
	ref := driveroot.Ref{Kind: kind, RootID: rootID, Path: path}
	file, err := h.svc.StatFileAtRoot(r.Context(), ncUser, ref)
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	h.svc.EnrichSources(r.Context(), claims.Sub, []nextcloud.FileInfo{file})
	apiresponse.WriteJSON(w, http.StatusOK, file)
}

func (h *Handler) DownloadOrgFolderFile(w http.ResponseWriter, r *http.Request) {
	h.downloadRootFile(w, r, driveroot.KindOrg, chi.URLParam(r, "folderID"))
}

func (h *Handler) DownloadMountFile(w http.ResponseWriter, r *http.Request) {
	h.downloadRootFile(w, r, driveroot.KindMount, chi.URLParam(r, "mountID"))
}

func (h *Handler) downloadRootFile(w http.ResponseWriter, r *http.Request, kind driveroot.Kind, rootID string) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	path := chi.URLParam(r, "*")
	if verr := validatePath(path); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}
	ref := driveroot.Ref{Kind: kind, RootID: rootID, Path: nextcloud.NormalizeClientPath(path)}
	body, contentType, err := h.svc.DownloadAtRoot(r.Context(), ncUser, ref)
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	defer body.Close()
	w.Header().Set("Content-Type", contentType)
	io.Copy(w, body)
}

func (h *Handler) PreviewOrgFolderFile(w http.ResponseWriter, r *http.Request) {
	h.previewRootFile(w, r, driveroot.KindOrg, chi.URLParam(r, "folderID"))
}

func (h *Handler) PreviewMountFile(w http.ResponseWriter, r *http.Request) {
	h.previewRootFile(w, r, driveroot.KindMount, chi.URLParam(r, "mountID"))
}

func (h *Handler) previewRootFile(w http.ResponseWriter, r *http.Request, kind driveroot.Kind, rootID string) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	path := chi.URLParam(r, "*")
	ref := driveroot.Ref{Kind: kind, RootID: rootID, Path: nextcloud.NormalizeClientPath(path)}
	file, err := h.svc.StatFileAtRoot(r.Context(), ncUser, ref)
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	width, _ := strconv.Atoi(r.URL.Query().Get("w"))
	height, _ := strconv.Atoi(r.URL.Query().Get("h"))
	_ = width
	_ = height
	var body io.ReadCloser
	var contentType string
	if kind == driveroot.KindPersonal {
		body, contentType, err = h.svc.Preview(r.Context(), ncUser, file.Path, width, height)
	} else {
		body, contentType, err = h.svc.DownloadAtRoot(r.Context(), ncUser, ref)
	}
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	defer body.Close()
	w.Header().Set("Content-Type", contentType)
	w.Header().Set("Cache-Control", "private, max-age=300")
	io.Copy(w, body)
}

func (h *Handler) UploadOrgFolderFile(w http.ResponseWriter, r *http.Request) {
	h.uploadRootFile(w, r, driveroot.KindOrg, chi.URLParam(r, "folderID"))
}

func (h *Handler) UploadMountFile(w http.ResponseWriter, r *http.Request) {
	h.uploadRootFile(w, r, driveroot.KindMount, chi.URLParam(r, "mountID"))
}

func (h *Handler) uploadRootFile(w http.ResponseWriter, r *http.Request, kind driveroot.Kind, rootID string) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	path := chi.URLParam(r, "*")
	if verr := validatePath(path); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}
	ref := driveroot.Ref{Kind: kind, RootID: rootID, Path: nextcloud.NormalizeClientPath(path)}
	if err := h.svc.UploadAtRoot(r.Context(), ncUser, ref, r.Body, r.Header.Get("Content-Type"), r.ContentLength); err != nil {
		writeDriveError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusCreated, map[string]string{"status": "uploaded", "path": path})
}

func (h *Handler) CreateOrgFolderDir(w http.ResponseWriter, r *http.Request) {
	h.createRootDir(w, r, driveroot.KindOrg, chi.URLParam(r, "folderID"))
}

func (h *Handler) CreateMountDir(w http.ResponseWriter, r *http.Request) {
	h.createRootDir(w, r, driveroot.KindMount, chi.URLParam(r, "mountID"))
}

func (h *Handler) createRootDir(w http.ResponseWriter, r *http.Request, kind driveroot.Kind, rootID string) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	path := chi.URLParam(r, "*")
	if verr := validatePath(path); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}
	ref := driveroot.Ref{Kind: kind, RootID: rootID, Path: nextcloud.NormalizeClientPath(path)}
	if err := h.svc.CreateFolderAtRoot(r.Context(), ncUser, ref); err != nil {
		writeDriveError(w, r, err)
		return
	}
	w.WriteHeader(http.StatusCreated)
}

func (h *Handler) DeleteOrgFolderFile(w http.ResponseWriter, r *http.Request) {
	h.deleteRootFile(w, r, driveroot.KindOrg, chi.URLParam(r, "folderID"))
}

func (h *Handler) DeleteMountFile(w http.ResponseWriter, r *http.Request) {
	h.deleteRootFile(w, r, driveroot.KindMount, chi.URLParam(r, "mountID"))
}

func (h *Handler) deleteRootFile(w http.ResponseWriter, r *http.Request, kind driveroot.Kind, rootID string) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	path := chi.URLParam(r, "*")
	ref := driveroot.Ref{Kind: kind, RootID: rootID, Path: nextcloud.NormalizeClientPath(path)}
	if err := h.svc.DeleteAtRoot(r.Context(), ncUser, ref); err != nil {
		writeDriveError(w, r, err)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}

func (h *Handler) ListMounts(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	platformUserID, err := h.svc.platformUserID(r.Context(), claims.Sub)
	if err != nil {
		apivalidate.WriteInternal(w, r)
		return
	}
	orgSlugs := parseOrgSlugs(r.URL.Query().Get("org_slugs"))
	mounts, err := h.svc.ListMountsForUser(r.Context(), platformUserID, ncUser, orgSlugs)
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"mounts": mounts})
}

func (h *Handler) CreateMount(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	platformUserID, err := h.svc.platformUserID(r.Context(), claims.Sub)
	if err != nil {
		apivalidate.WriteInternal(w, r)
		return
	}
	var req createMountRequest
	if err := apivalidate.DecodeJSON(w, r, maxJSONRequestBody, &req); err != nil {
		return
	}
	mount, err := h.svc.CreateMount(r.Context(), platformUserID, ncUser, CreateMountParams{
		Scope:        req.Scope,
		OrgSlug:      req.OrgSlug,
		DisplayName:  req.DisplayName,
		BackendType:  req.BackendType,
		WebDAV:       req.WebDAV,
		OAuthBackend: req.OAuthBackend,
		OAuthAuth:    req.OAuthAuth,
	})
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusCreated, mount)
}

func (h *Handler) DeleteMount(w http.ResponseWriter, r *http.Request) {
	mountID := chi.URLParam(r, "mountID")
	if err := h.svc.DeleteMount(r.Context(), mountID); err != nil {
		writeDriveError(w, r, err)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}

func (h *Handler) GetMountOAuthURL(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	platformUserID, err := h.svc.platformUserID(r.Context(), claims.Sub)
	if err != nil {
		apivalidate.WriteInternal(w, r)
		return
	}
	url, err := h.svc.GetMountOAuthURL(r.Context(), chi.URLParam(r, "mountID"), platformUserID, ncUser, strings.TrimSpace(r.URL.Query().Get("redirect_uri")))
	if err != nil {
		writeDriveError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]string{"oauth_url": url})
}

type completeMountOAuthRequest struct {
	Code        string `json:"code"`
	RedirectURI string `json:"redirect_uri"`
}

func (h *Handler) CompleteMountOAuth(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	ncUser, ok := h.nextcloudUser(w, r, claims)
	if !ok {
		return
	}
	platformUserID, err := h.svc.platformUserID(r.Context(), claims.Sub)
	if err != nil {
		apivalidate.WriteInternal(w, r)
		return
	}
	var req completeMountOAuthRequest
	if err := apivalidate.DecodeJSON(w, r, maxJSONRequestBody, &req); err != nil {
		return
	}
	if err := h.svc.CompleteMountOAuth(r.Context(), chi.URLParam(r, "mountID"), platformUserID, ncUser, strings.TrimSpace(req.RedirectURI), req.Code); err != nil {
		writeDriveError(w, r, err)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]string{"status": "active"})
}

func parseOrgSlugs(raw string) []string {
	raw = strings.TrimSpace(raw)
	if raw == "" {
		return nil
	}
	parts := strings.Split(raw, ",")
	out := make([]string, 0, len(parts))
	for _, p := range parts {
		p = strings.TrimSpace(strings.ToLower(p))
		if p != "" {
			out = append(out, p)
		}
	}
	return out
}
