package http

import (
	"go.uber.org/zap"

	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/blockedemail"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/collection"
	commonhttp "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/common"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/dashboard"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/file"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/inviteemail"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/me"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/middleware"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/tag"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/user"
	svc_auth "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/service/auth"
)

// ProvideHandlers wires all HTTP handlers for Wire DI
func ProvideHandlers(
	cfg *config.Config,
	logger *zap.Logger,

	// Common
	versionHandler *commonhttp.MapleFileVersionHTTPHandler,

	// Dashboard
	getDashboard *dashboard.GetDashboardHTTPHandler,

	// Me
	getMe *me.GetMeHTTPHandler,
	updateMe *me.PutUpdateMeHTTPHandler,
	deleteMe *me.DeleteMeHTTPHandler,
	changeEmailRequest *me.PostChangeEmailRequestHTTPHandler,
	changeEmailVerify *me.PostChangeEmailVerifyHTTPHandler,

	// User
	userPublicLookup *user.UserPublicLookupHTTPHandler,

	// Blocked Email
	createBlockedEmail *blockedemail.CreateBlockedEmailHTTPHandler,
	listBlockedEmails *blockedemail.ListBlockedEmailsHTTPHandler,
	deleteBlockedEmail *blockedemail.DeleteBlockedEmailHTTPHandler,

	// Invite Email
	sendInviteEmail *inviteemail.SendInviteEmailHTTPHandler,

	// Collection - Basic CRUD
	createCollection *collection.CreateCollectionHTTPHandler,
	getCollection *collection.GetCollectionHTTPHandler,
	listUserCollections *collection.ListUserCollectionsHTTPHandler,
	updateCollection *collection.UpdateCollectionHTTPHandler,
	softDeleteCollection *collection.SoftDeleteCollectionHTTPHandler,
	archiveCollection *collection.ArchiveCollectionHTTPHandler,
	restoreCollection *collection.RestoreCollectionHTTPHandler,

	// Collection - Hierarchical
	findCollectionsByParent *collection.FindCollectionsByParentHTTPHandler,
	findRootCollections *collection.FindRootCollectionsHTTPHandler,
	moveCollection *collection.MoveCollectionHTTPHandler,

	// Collection - Sharing
	shareCollection *collection.ShareCollectionHTTPHandler,
	removeMember *collection.RemoveMemberHTTPHandler,
	listSharedCollections *collection.ListSharedCollectionsHTTPHandler,

	// Collection - Filtered
	getFilteredCollections *collection.GetFilteredCollectionsHTTPHandler,

	// Collection - Sync
	collectionSync *collection.CollectionSyncHTTPHandler,

	// File - CRUD
	softDeleteFile *file.SoftDeleteFileHTTPHandler,
	deleteMultipleFiles *file.DeleteMultipleFilesHTTPHandler,
	getFile *file.GetFileHTTPHandler,
	listFilesByCollection *file.ListFilesByCollectionHTTPHandler,
	updateFile *file.UpdateFileHTTPHandler,
	createPendingFile *file.CreatePendingFileHTTPHandler,
	completeFileUpload *file.CompleteFileUploadHTTPHandler,
	getPresignedUploadURL *file.GetPresignedUploadURLHTTPHandler,
	getPresignedDownloadURL *file.GetPresignedDownloadURLHTTPHandler,
	reportDownloadCompleted *file.ReportDownloadCompletedHTTPHandler,
	archiveFile *file.ArchiveFileHTTPHandler,
	restoreFile *file.RestoreFileHTTPHandler,
	listRecentFiles *file.ListRecentFilesHTTPHandler,

	// File - Sync
	fileSync *file.FileSyncHTTPHandler,

	// Tag handlers
	createTag *tag.CreateTagHTTPHandler,
	listTags *tag.ListTagsHTTPHandler,
	getTag *tag.GetTagHTTPHandler,
	updateTag *tag.UpdateTagHTTPHandler,
	deleteTag *tag.DeleteTagHTTPHandler,
	assignTag *tag.AssignTagHTTPHandler,
	unassignTag *tag.UnassignTagHTTPHandler,
	getTagsForCollection *tag.GetTagsForCollectionHTTPHandler,
	getTagsForFile *tag.GetTagsForFileHTTPHandler,
	listCollectionsByTag *tag.ListCollectionsByTagHandler,
	listFilesByTag *tag.ListFilesByTagHandler,
	searchByTags *tag.SearchByTagsHandler,
) *Handlers {
	return NewHandlers(
		// Common
		versionHandler,

		// Dashboard
		getDashboard,

		// Me
		getMe,
		updateMe,
		deleteMe,
		changeEmailRequest,
		changeEmailVerify,

		// User
		userPublicLookup,

		// Blocked Email
		createBlockedEmail,
		listBlockedEmails,
		deleteBlockedEmail,

		// Invite Email
		sendInviteEmail,

		// Collection - Basic CRUD
		createCollection,
		getCollection,
		listUserCollections,
		updateCollection,
		softDeleteCollection,
		archiveCollection,
		restoreCollection,

		// Collection - Hierarchical
		findCollectionsByParent,
		findRootCollections,
		moveCollection,

		// Collection - Sharing
		shareCollection,
		removeMember,
		listSharedCollections,

		// Collection - Filtered
		getFilteredCollections,

		// Collection Sync
		collectionSync,

		// File - CRUD
		softDeleteFile,
		deleteMultipleFiles,
		getFile,
		listFilesByCollection,
		updateFile,
		createPendingFile,
		completeFileUpload,
		getPresignedUploadURL,
		getPresignedDownloadURL,
		reportDownloadCompleted,
		archiveFile,
		restoreFile,
		listRecentFiles,

		// File Sync
		fileSync,

		// Tag handlers
		createTag,
		listTags,
		getTag,
		updateTag,
		deleteTag,
		assignTag,
		unassignTag,
		getTagsForCollection,
		getTagsForFile,
		listCollectionsByTag,
		listFilesByTag,
		searchByTags,
	)
}

// ProvideServer provides the HTTP server for Wire DI
func ProvideServer(
	cfg *config.Config,
	logger *zap.Logger,
	handlers *Handlers,
	registerService svc_auth.RegisterService,
	verifyEmailService svc_auth.VerifyEmailService,
	resendVerificationService svc_auth.ResendVerificationService,
	requestOTTService svc_auth.RequestOTTService,
	verifyOTTService svc_auth.VerifyOTTService,
	completeLoginService svc_auth.CompleteLoginService,
	refreshTokenService svc_auth.RefreshTokenService,
	recoveryInitiateService svc_auth.RecoveryInitiateService,
	recoveryVerifyService svc_auth.RecoveryVerifyService,
	recoveryCompleteService svc_auth.RecoveryCompleteService,
	rateLimitMiddleware *middleware.RateLimitMiddleware,
	securityHeadersMiddleware *middleware.SecurityHeadersMiddleware,
) *WireServer {
	return NewWireServer(
		cfg,
		logger,
		handlers,
		registerService,
		verifyEmailService,
		resendVerificationService,
		requestOTTService,
		verifyOTTService,
		completeLoginService,
		refreshTokenService,
		recoveryInitiateService,
		recoveryVerifyService,
		recoveryCompleteService,
		rateLimitMiddleware,
		securityHeadersMiddleware,
	)
}
