package controller

import (
	"io"

	"metazone.cc/metalab/internal/middleware"
	"metazone.cc/metalab/internal/model"
	"metazone.cc/metalab/internal/service"
	"metazone.cc/metalab/internal/session"
)

// authUseCase AuthController 对 AuthService 的最小依赖（ISP：5 个方法）
// 注意：Login/Register/ConfirmRestore 不再返回 JWT，session 由 controller 通过 SessionManager 创建
type authUseCase interface {
	Register(req model.RegisterRequest, regIP string) (*model.User, error)
	Login(req model.LoginRequest, loginIP string) (*model.User, error)
	ConfirmRestore(req model.LoginRequest, loginIP string) (*model.User, error)
	CheckEmail(email string) (bool, error)
	IsRegistrationEnabled() bool
}

// rateLimiter AuthController 对 RateLimiter 的最小依赖（ISP：4 个方法）
type rateLimiter interface {
	AllowAccount(email string) middleware.RateLimitResult
	AllowIP(ip string) middleware.RateLimitResult
	Clear(email, ip string)
	ClearIP(ipKey string)
}

// postUseCase PostController 对 PostService 的最小依赖（ISP：8 个方法）
type postUseCase interface {
	Create(userID uint, title, body string) (*model.Post, error)
	GetByID(id uint) (*model.Post, error)
	List(keyword string, page, pageSize int) ([]model.Post, int64, error)
	Update(postID uint, title, body string) error
	Delete(postID uint) error
	SubmitForAudit(postID uint) error
	RecordRead(userID, postID uint)
	RecordGuestRead(visitorID string, postID uint)
	IsPostAccessible(userID uint, role string, postUserID uint) bool
}

// studioUseCase StudioController 对 PostService 的最小依赖（ISP：9 个方法）
type studioUseCase interface {
	Create(userID uint, title, body string) (*model.Post, error)
	GetByID(id uint) (*model.Post, error)
	Update(postID uint, title, body string) error
	Delete(postID uint) error
	SubmitForAudit(postID uint) error
	ListByUser(userID uint, status string, page, pageSize int) ([]model.Post, int64, error)
	GetOverview(userID uint) (*service.StudioOverview, error)
	IsPostAccessible(userID uint, role string, postUserID uint) bool
	CanCreatePost(userID uint) bool
}

// spaceUseCase SpaceController 对 SpaceService 的最小依赖（ISP：5 个方法）
type spaceUseCase interface {
	GetSpaceUser(uid uint) (*model.User, error)
	GetPostsByUser(uid uint, page, pageSize int) ([]model.Post, int64, error)
	CountUserPosts(uid uint) (int64, error)
	GetUserStats(uid uint) (likes, favorites, reads int64, err error)
	UpdateUser(user *model.User) error
}

// favoriteUseCaseForSpace SpaceController 对 FavoriteService 的最小依赖（ISP：收藏夹相关）
type favoriteUseCaseForSpace interface {
	ListFolders(userID uint) (*service.FavoriteListResult, error)
	ListFolderItems(userID, folderID uint, page, pageSize int) (*service.FolderItemsResult, error)
}

// sessionManager 登录管理对会话管理的最小依赖（ISP：4 个方法）
type sessionManager interface {
	ListByUID(uid uint) ([]*session.Session, error)
	Destroy(sid string) error
	DestroyOtherByUID(uid uint, currentSID string) error
	UpdateRemark(sid string, uid uint, remark string) error
}

// levelUseCase LevelController 对 LevelService 的最小依赖
type levelUseCase interface {
	CheckIn(userID uint) (checkedIn bool, newExp int, levelUp bool, err error)
	GetLevel(userID uint) (level int, exp int, checkedIn bool, err error)
	CompleteTask(userID uint, taskType string) (newExp int, levelUp bool, err error)
}

// energyUseCase EnergyController 对 EnergyService 的最小依赖（ISP：3 个方法）
type energyUseCase interface {
	Energize(energizerID uint, postID uint, amount int) (int, error)
	GetEnergyInfo(userID uint) (*service.EnergyInfo, error)
	GetEnergyLogs(userID uint, days, page, pageSize int) ([]model.EnergyLog, int64, error)
}

// energyDeducter 删稿/改名时域能扣减接口（ISP）
type energyDeducter interface {
	DeductOnDeletePost(authorUserID uint, postID uint) error
}

// energyRenameHandler 改名域能操作接口（ISP）
type energyRenameHandler interface {
	DeductOnRename(userID uint) error
}

// energyAdminUseCase AdminEnergyController 对 EnergyService 的依赖（ISP：4 个方法）
type energyAdminUseCase interface {
	AdminAdjust(operatorUID uint, userIDs []uint, amount int, description string, mode string) error
	GetAdminEnergyLogs(energyType string, page, pageSize int) ([]model.EnergyLog, int64, error)
	GetFundBalance() (int, error)
	GetFundLogs(logType string, page, pageSize int) ([]model.FundLog, int64, error)
}

// commentUseCase CommentController 对 CommentService 的最小依赖（ISP：6 个方法）
type commentUseCase interface {
	CreateRoot(userID, postID uint, body string) (*model.Comment, error)
	CreateReply(userID, parentID uint, body string) (*model.Comment, error)
	Delete(commentID uint, requesterID uint, isAdmin bool) error
	ListRootComments(postID uint, page, pageSize int) ([]model.Comment, int64, error)
	ListReplies(rootID uint) ([]model.Comment, error)
	SearchUsers(keyword string) ([]model.UserSearchResult, error)
}

// reactionUseCase ReactionController 对 ReactionService 的最小依赖（ISP：4 个方法）
type reactionUseCase interface {
	ToggleLike(userID, postID uint) (bool, error)
	ToggleDislike(userID, postID uint) (bool, error)
	GetReaction(userID, postID uint) (service.ReactionType, error)
	GetPostLikesCount(postID uint) (int, error)
}

// followUseCase FollowController 对 FollowService 的最小依赖（ISP：4 个方法）
type followUseCase interface {
	Toggle(followerID, followeeID uint) (bool, error)
	GetStatus(currentUserID, targetUserID uint) (*service.FollowStatus, error)
	ListFollowers(userID, currentUserID uint, page, pageSize int) (*service.FollowListResult, error)
	ListFollowing(userID, currentUserID uint, page, pageSize int) (*service.FollowListResult, error)
}

// favoriteUseCase FavoriteController 对 FavoriteService 的最小依赖（ISP：9 个方法）
type favoriteUseCase interface {
	ListFolders(userID uint) (*service.FavoriteListResult, error)
	CreateFolder(userID uint, name, description string, isPublic bool) (*model.Folder, error)
	UpdateFolder(userID, folderID uint, name, description string, isPublic bool) error
	DeleteFolder(userID, folderID uint) error
	ListFolderItems(userID, folderID uint, page, pageSize int) (*service.FolderItemsResult, error)
	AddFavorite(userID, postID uint, folderID *uint) error
	RemoveFavorite(userID, postID uint) error
	ToggleFavorite(userID, postID uint) (bool, error)
	GetPostFavoriteStatus(userID, postID uint) (*service.FavoriteStatus, error)
}

// profileProvider SettingsController 对 AuthService 的最小依赖（ISP：4 个方法）
type profileProvider interface {
	GetProfile(userID uint) (*model.User, error)
	UpdateProfile(userID uint, username, bio string) error
	ChangePassword(userID uint, currentPassword, newPassword string) error
	DeleteAccount(userID uint, password, reason string) error
}

// avatarProvider SettingsController 头像上传对 Service 的最小依赖（ISP：2 个方法）
type avatarProvider interface {
	ProcessAvatar(userID uint, file io.Reader, contentType string, cropX, cropY, cropSize int) (string, error)
	ProcessImage(userID uint, file io.Reader, contentType string, cropX, cropY, cropSize int) (string, error)
}

// auditSubmittable SettingsController 对 AuditService 的依赖（ISP：4 个方法）
type auditSubmittable interface {
	ShouldAudit(userID uint) (bool, error)
	SubmitProfileChanges(userID uint, currentUser *model.User, newUsername, newBio string) error
	Submit(userID uint, auditType, newValue string) error
	GetPendingTypes(userID uint) ([]string, error)
}

// sessionConfig SettingsController 对配置的最小依赖（ISP：2 个方法）
type sessionConfig interface {
	GetIdleTimeout() int
	GetRememberTimeout() int
}

// taskCompleter SettingsController 对 LevelService 的依赖（ISP：2 个方法）
type taskCompleter interface {
	CompleteTask(userID uint, taskType string) (newExp int, levelUp bool, err error)
	HasCompletedTask(userID uint, taskType string) (bool, error)
}

// notifyPrefReader SettingsController 通知偏好的接口（ISP）
type notifyPrefReader interface {
	GetNotifyPrefs(userID uint) (map[string]bool, error)
	UpdateNotifyPref(userID uint, key string, enabled bool) error
}
