import { type User, type InsertUser, type Label, type InsertLabel, type SharedLabel, type Task, type InsertTask, type XpEvent, type InsertXpEvent, type Goal, type InsertGoal, type Reward, type InsertReward, type UserReward, type InsertUserReward, type SystemSettings, type InsertSystemSettings, type SharedTask, type InsertSharedTask, type UserTaskAccess, type InsertUserTaskAccess, type InsertPasswordResetToken, type PasswordResetToken, type Conversation, type InsertConversation, type Message, type InsertMessage, type AuditLog, type InsertAuditLog, type TaskTimeLog, type InsertTaskTimeLog, type BreakLog, type EnergyLog, type BodyDoublingSession, type FocusSession, type DailyChallenge } from "@shared/schema"; import * as schema from "@shared/schema"; import { getDatabase, pool } from "./db"; import { eq, sql, and, desc, asc, gt, ne, or, isNull } from "drizzle-orm"; import { randomUUID } from "crypto"; import session from "express-session"; import createMemoryStore from "memorystore"; import connectPg from "connect-pg-simple"; const MemoryStore = createMemoryStore(session); const PostgresStore = connectPg(session); export interface IStorage { sessionStore: session.Store; getUser(id: string): Promise; getUserByUsername(username: string): Promise; getUserByEmail(email: string): Promise; getUserByApiKey(apiKey: string): Promise; createUser(user: InsertUser & { role?: string; isActive?: boolean }): Promise; updateUserApiKey(userId: string, apiKey: string | null): Promise; updateUser(id: string, updates: Partial): Promise; deleteUser(id: string): Promise; getAllUsers(): Promise; updateUserXP(id: string, xp: number): Promise; // Social & Leaderboard getLeaderboard(): Promise; searchUsers(query: string): Promise; shareTask(sharedTask: InsertSharedTask): Promise; shareAllTasks(access: InsertUserTaskAccess): Promise; createSharedTask(sharedTask: InsertSharedTask): Promise; // Alias for shareTask standard naming createUserTaskAccess(access: InsertUserTaskAccess): Promise; // Alias getSharedTasks(userId: string): Promise; // Tasks shared WITH user getSharedTask(taskId: string, userId: string): Promise; // Check specific share getUserTaskAccess(viewerId: string): Promise; // Access Viewer has to Owners checkUserTaskAccess(ownerId: string, viewerId: string): Promise; // Check specific access getTaskSharedUsers(taskId: string): Promise; // Get users a task is shared WITH unshareTask(taskId: string, userId: string): Promise; // Unshare specific task from user // Shared Labels shareLabel(labelId: string, sharedWithUserId: string, sharedByUserId: string, permission?: string): Promise; getSharedLabels(userId: string): Promise; // Labels shared WITH user getLabelSharedUsers(labelId: string): Promise; // Users label is shared WITH unshareLabel(labelId: string, userId: string): Promise; getLabelShares(labelId: string): Promise; // System Settings (Admin) getSystemSettings(key: string): Promise; setSystemSettings(key: string, value: string): Promise; hasAdminUser(): Promise; // Labels getAllLabels(): Promise; getLabels(userId: string): Promise; getLabel(id: string): Promise