import { pgTable, timestamp, varchar, boolean, serial, integer, text } from 'drizzle-orm/pg-core';
import { relations, eq, and, gt, lt } from 'drizzle-orm';
import { randomBytes } from 'crypto';
import bcrypt from 'bcryptjs';
import { NextResponse } from 'next/server';

// src/auth/legacy/schema/index.ts
var legacyUsers = pgTable("users", {
  id: serial("id").primaryKey(),
  phone: varchar("phone", { length: 20 }).notNull().unique(),
  password: varchar("password", { length: 255 }).notNull(),
  name: varchar("name", { length: 100 }),
  email: varchar("email", { length: 255 }),
  isActive: boolean("is_active").notNull().default(true),
  role: varchar("role", { length: 20 }).notNull().default("user"),
  lastLoginAt: timestamp("last_login_at"),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").defaultNow().notNull()
});
var legacyUserSessions = pgTable("user_sessions", {
  id: serial("id").primaryKey(),
  userId: integer("user_id").notNull().references(() => legacyUsers.id, { onDelete: "cascade" }),
  sessionToken: varchar("session_token", { length: 255 }).notNull().unique(),
  expiresAt: timestamp("expires_at").notNull(),
  createdAt: timestamp("created_at").defaultNow().notNull()
});
var legacyVerificationCodes = pgTable("verification_codes", {
  id: serial("id").primaryKey(),
  phone: text("phone").notNull(),
  code: text("code").notNull(),
  expiresAt: timestamp("expires_at").notNull(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  used: boolean("used").default(false).notNull()
});
var legacyUsersRelations = relations(legacyUsers, ({ many }) => ({
  sessions: many(legacyUserSessions)
}));
var legacyUserSessionsRelations = relations(legacyUserSessions, ({ one }) => ({
  user: one(legacyUsers, {
    fields: [legacyUserSessions.userId],
    references: [legacyUsers.id]
  })
}));
var LegacyAuthDbService = class {
  constructor(config) {
    this.db = config.db;
    this.tables = {
      users: config.tables?.users ?? legacyUsers,
      userSessions: config.tables?.userSessions ?? legacyUserSessions,
      verificationCodes: config.tables?.verificationCodes ?? legacyVerificationCodes
    };
    this.saltRounds = config.saltRounds || 12;
    this.sessionDurationMs = config.sessionDurationMs || 30 * 24 * 60 * 60 * 1e3;
  }
  async verifyPassword(phone, password) {
    try {
      const result = await this.db.select().from(this.tables.users).where(eq(this.tables.users.phone, phone)).limit(1);
      const user = result[0];
      if (!user) return null;
      if (!user.isActive) return null;
      const isValid = await bcrypt.compare(password, user.password);
      if (!isValid) return null;
      const { password: _, ...userWithoutPassword } = user;
      return userWithoutPassword;
    } catch (error) {
      console.error("\u{1F4A5} [LegacyAuthDbService] \u5BC6\u7801\u9A8C\u8BC1\u5F02\u5E38:", error);
      return null;
    }
  }
  async createUser(phone, password, name) {
    try {
      const existingUser = await this.db.select().from(this.tables.users).where(eq(this.tables.users.phone, phone)).limit(1);
      if (existingUser.length > 0) {
        throw new Error("\u7528\u6237\u5DF2\u5B58\u5728");
      }
      const hashedPassword = await bcrypt.hash(password, this.saltRounds);
      const result = await this.db.insert(this.tables.users).values({
        phone,
        password: hashedPassword,
        name: name || null,
        isActive: true,
        role: "user"
      }).returning();
      const createdUser = result[0];
      if (!createdUser) {
        throw new Error("\u521B\u5EFA\u7528\u6237\u5931\u8D25: \u672A\u8FD4\u56DE\u7528\u6237\u8BB0\u5F55");
      }
      const { password: _, ...userWithoutPassword } = createdUser;
      return userWithoutPassword;
    } catch (error) {
      console.error("\u521B\u5EFA\u7528\u6237\u5931\u8D25:", error);
      throw new Error("\u521B\u5EFA\u7528\u6237\u5931\u8D25");
    }
  }
  async updateLastLogin(userId) {
    try {
      await this.db.update(this.tables.users).set({ lastLoginAt: /* @__PURE__ */ new Date(), updatedAt: /* @__PURE__ */ new Date() }).where(eq(this.tables.users.id, userId));
    } catch (error) {
      console.error("\u66F4\u65B0\u6700\u540E\u767B\u5F55\u65F6\u95F4\u5931\u8D25:", error);
      throw new Error("\u66F4\u65B0\u6700\u540E\u767B\u5F55\u65F6\u95F4\u5931\u8D25");
    }
  }
  async createSession(userId) {
    try {
      const sessionToken = randomBytes(32).toString("hex");
      const expiresAt = new Date(Date.now() + this.sessionDurationMs);
      const result = await this.db.insert(this.tables.userSessions).values({
        userId,
        sessionToken,
        expiresAt
      }).returning();
      const session = result[0];
      if (!session) {
        throw new Error("\u521B\u5EFA\u4F1A\u8BDD\u5931\u8D25: \u672A\u8FD4\u56DE\u4F1A\u8BDD\u8BB0\u5F55");
      }
      return session;
    } catch (error) {
      console.error("\u521B\u5EFA\u4F1A\u8BDD\u5931\u8D25:", error);
      throw new Error("\u521B\u5EFA\u4F1A\u8BDD\u5931\u8D25");
    }
  }
  async validateSession(sessionToken) {
    try {
      const now = /* @__PURE__ */ new Date();
      const result = await this.db.select({
        session: this.tables.userSessions,
        user: this.tables.users
      }).from(this.tables.userSessions).innerJoin(this.tables.users, eq(this.tables.userSessions.userId, this.tables.users.id)).where(
        and(
          eq(this.tables.userSessions.sessionToken, sessionToken),
          gt(this.tables.userSessions.expiresAt, now),
          eq(this.tables.users.isActive, true)
        )
      ).limit(1);
      const row = result[0];
      if (!row) {
        return { valid: false };
      }
      const { password: _, ...userWithoutPassword } = row.user;
      return { valid: true, user: userWithoutPassword };
    } catch (error) {
      console.error("\u4F1A\u8BDD\u9A8C\u8BC1\u5931\u8D25:", error);
      return { valid: false };
    }
  }
  async deleteSession(sessionToken) {
    try {
      await this.db.delete(this.tables.userSessions).where(eq(this.tables.userSessions.sessionToken, sessionToken));
    } catch (error) {
      console.error("\u5220\u9664\u4F1A\u8BDD\u5931\u8D25:", error);
      throw new Error("\u5220\u9664\u4F1A\u8BDD\u5931\u8D25");
    }
  }
  async deleteUserSessions(userId) {
    try {
      await this.db.delete(this.tables.userSessions).where(eq(this.tables.userSessions.userId, userId));
    } catch (error) {
      console.error("\u5220\u9664\u7528\u6237\u4F1A\u8BDD\u5931\u8D25:", error);
      throw new Error("\u5220\u9664\u7528\u6237\u4F1A\u8BDD\u5931\u8D25");
    }
  }
  async cleanupExpiredSessions() {
    try {
      const now = /* @__PURE__ */ new Date();
      await this.db.delete(this.tables.userSessions).where(lt(this.tables.userSessions.expiresAt, now));
    } catch (error) {
      console.error("\u6E05\u7406\u8FC7\u671F\u4F1A\u8BDD\u5931\u8D25:", error);
    }
  }
  async getUserByPhone(phone) {
    try {
      const result = await this.db.select().from(this.tables.users).where(eq(this.tables.users.phone, phone)).limit(1);
      const user = result[0];
      if (!user) return null;
      const { password: _, ...userWithoutPassword } = user;
      return userWithoutPassword;
    } catch (error) {
      console.error("\u67E5\u8BE2\u7528\u6237\u5931\u8D25:", error);
      throw error;
    }
  }
  async sendVerificationCode(phone) {
    try {
      await this.cleanupExpiredVerificationCodes();
      const existingCode = await this.db.select().from(this.tables.verificationCodes).where(
        and(
          eq(this.tables.verificationCodes.phone, phone),
          eq(this.tables.verificationCodes.used, false),
          gt(this.tables.verificationCodes.expiresAt, /* @__PURE__ */ new Date())
        )
      ).limit(1);
      const currentCode = existingCode[0];
      if (currentCode) {
        const timeDiff = Date.now() - currentCode.createdAt.getTime();
        if (timeDiff < 60 * 1e3) {
          throw new Error("\u9A8C\u8BC1\u7801\u53D1\u9001\u8FC7\u4E8E\u9891\u7E41\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5");
        }
      }
      const code = Math.floor(1e5 + Math.random() * 9e5).toString();
      await this.db.insert(this.tables.verificationCodes).values({
        phone,
        code,
        expiresAt: new Date(Date.now() + 10 * 60 * 1e3)
      }).returning();
      return code;
    } catch (error) {
      console.error("\u53D1\u9001\u9A8C\u8BC1\u7801\u5931\u8D25:", error);
      throw error;
    }
  }
  async verifyCode(phone, code) {
    try {
      const result = await this.db.select().from(this.tables.verificationCodes).where(
        and(
          eq(this.tables.verificationCodes.phone, phone),
          eq(this.tables.verificationCodes.code, code),
          eq(this.tables.verificationCodes.used, false),
          gt(this.tables.verificationCodes.expiresAt, /* @__PURE__ */ new Date())
        )
      ).limit(1);
      const verificationCode = result[0];
      if (!verificationCode) return false;
      await this.db.update(this.tables.verificationCodes).set({ used: true }).where(eq(this.tables.verificationCodes.id, verificationCode.id));
      return true;
    } catch (error) {
      console.error("\u9A8C\u8BC1\u7801\u9A8C\u8BC1\u5931\u8D25:", error);
      throw error;
    }
  }
  async resetPassword(phone, newPassword) {
    try {
      const hashedPassword = await bcrypt.hash(newPassword, this.saltRounds);
      await this.db.update(this.tables.users).set({ password: hashedPassword }).where(eq(this.tables.users.phone, phone));
    } catch (error) {
      console.error("\u5BC6\u7801\u91CD\u7F6E\u5931\u8D25:", error);
      throw error;
    }
  }
  async cleanupExpiredVerificationCodes() {
    try {
      const now = /* @__PURE__ */ new Date();
      await this.db.delete(this.tables.verificationCodes).where(lt(this.tables.verificationCodes.expiresAt, now)).returning();
    } catch (error) {
      console.error("\u6E05\u7406\u8FC7\u671F\u9A8C\u8BC1\u7801\u5931\u8D25:", error);
    }
  }
};

// src/auth/legacy/utils/authUtils.ts
function validatePhoneNumber(phone) {
  return /^1[3-9]\d{9}$/.test(phone);
}
function validatePassword(password) {
  if (!password) {
    return { valid: false, message: "\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A" };
  }
  if (password.length < 6) {
    return { valid: false, message: "\u5BC6\u7801\u957F\u5EA6\u81F3\u5C116\u4F4D" };
  }
  return { valid: true };
}

// src/auth/legacy/routes/login.ts
function createLegacyLoginHandler(config) {
  return async (request) => {
    try {
      const { phone, password } = await request.json();
      if (!phone || !password) {
        return NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u624B\u673A\u53F7\u548C\u5BC6\u7801" },
          { status: 400 }
        );
      }
      if (!validatePhoneNumber(phone)) {
        return NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7\u683C\u5F0F" },
          { status: 400 }
        );
      }
      const passwordValidation = validatePassword(password);
      if (!passwordValidation.valid) {
        return NextResponse.json(
          { success: false, message: passwordValidation.message || "\u5BC6\u7801\u683C\u5F0F\u9519\u8BEF" },
          { status: 400 }
        );
      }
      const user = await config.authService.verifyPassword(phone, password);
      if (!user) {
        return NextResponse.json(
          { success: false, message: "\u624B\u673A\u53F7\u6216\u5BC6\u7801\u9519\u8BEF" },
          { status: 401 }
        );
      }
      const session = await config.authService.createSession(user.id);
      await config.authService.updateLastLogin(user.id);
      const response = NextResponse.json({
        success: true,
        message: "\u767B\u5F55\u6210\u529F",
        user,
        sessionToken: session.sessionToken,
        data: { user, sessionToken: session.sessionToken }
      });
      const cookieOptions = {
        name: config.cookieOptions?.name || "session_token",
        httpOnly: config.cookieOptions?.httpOnly !== false,
        secure: config.cookieOptions?.secure || false,
        sameSite: config.cookieOptions?.sameSite || "lax",
        maxAge: config.cookieOptions?.maxAge || 30 * 24 * 60 * 60,
        path: config.cookieOptions?.path || "/"
      };
      response.cookies.set(cookieOptions.name, session.sessionToken, {
        httpOnly: cookieOptions.httpOnly,
        secure: cookieOptions.secure,
        sameSite: cookieOptions.sameSite,
        maxAge: cookieOptions.maxAge,
        path: cookieOptions.path
      });
      return response;
    } catch (error) {
      console.error("\u{1F4A5} [LegacyLogin] \u767B\u5F55\u5F02\u5E38:", error);
      return NextResponse.json(
        { success: false, message: "\u767B\u5F55\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5" },
        { status: 500 }
      );
    }
  };
}
function createLegacyRegisterHandler(config) {
  return async (request) => {
    try {
      const { phone, password, name } = await request.json();
      if (!phone || !password) {
        return NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u624B\u673A\u53F7\u548C\u5BC6\u7801" },
          { status: 400 }
        );
      }
      if (!validatePhoneNumber(phone)) {
        return NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7\u683C\u5F0F" },
          { status: 400 }
        );
      }
      const passwordValidation = validatePassword(password);
      if (!passwordValidation.valid) {
        return NextResponse.json(
          { success: false, message: passwordValidation.message || "\u5BC6\u7801\u683C\u5F0F\u9519\u8BEF" },
          { status: 400 }
        );
      }
      const user = await config.authService.createUser(phone, password, name);
      const session = await config.authService.createSession(user.id);
      await config.authService.updateLastLogin(user.id);
      const response = NextResponse.json({
        success: true,
        message: "\u6CE8\u518C\u6210\u529F",
        user,
        sessionToken: session.sessionToken,
        data: { user, sessionToken: session.sessionToken }
      });
      const cookieOptions = {
        name: config.cookieOptions?.name || "session_token",
        httpOnly: config.cookieOptions?.httpOnly !== false,
        secure: config.cookieOptions?.secure || false,
        sameSite: config.cookieOptions?.sameSite || "lax",
        maxAge: config.cookieOptions?.maxAge || 30 * 24 * 60 * 60,
        path: config.cookieOptions?.path || "/"
      };
      response.cookies.set(cookieOptions.name, session.sessionToken, {
        httpOnly: cookieOptions.httpOnly,
        secure: cookieOptions.secure,
        sameSite: cookieOptions.sameSite,
        maxAge: cookieOptions.maxAge,
        path: cookieOptions.path
      });
      return response;
    } catch (error) {
      console.error("\u{1F4A5} [LegacyRegister] \u6CE8\u518C\u5F02\u5E38:", error);
      return NextResponse.json(
        { success: false, message: "\u6CE8\u518C\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5" },
        { status: 500 }
      );
    }
  };
}
function createLegacyLogoutHandler(config) {
  return async (request) => {
    try {
      const cookieName = config.cookieOptions?.name || "session_token";
      const sessionToken = request.cookies.get(cookieName)?.value;
      if (sessionToken) {
        await config.authService.deleteSession(sessionToken);
      }
      const response = NextResponse.json({
        success: true,
        message: "\u767B\u51FA\u6210\u529F"
      });
      response.cookies.set(cookieName, "", {
        httpOnly: true,
        secure: config.cookieOptions?.secure || false,
        sameSite: config.cookieOptions?.sameSite || "lax",
        maxAge: 0,
        path: config.cookieOptions?.path || "/"
      });
      return response;
    } catch (error) {
      console.error("\u{1F4A5} [LegacyLogout] \u767B\u51FA\u5F02\u5E38:", error);
      return NextResponse.json(
        { success: false, message: "\u767B\u51FA\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5" },
        { status: 500 }
      );
    }
  };
}
function createLegacyValidateHandler(config) {
  return async (request) => {
    try {
      const cookieName = config.cookieOptions?.name || "session_token";
      const sessionToken = request.cookies.get(cookieName)?.value;
      if (!sessionToken) {
        return NextResponse.json(
          { success: true, valid: false, user: null, data: { valid: false, user: null } },
          { status: 200 }
        );
      }
      const validation = await config.authService.validateSession(sessionToken);
      return NextResponse.json({
        success: true,
        valid: validation.valid,
        user: validation.user,
        message: validation.valid ? "\u4F1A\u8BDD\u6709\u6548" : "\u4F1A\u8BDD\u65E0\u6548",
        data: validation
      });
    } catch (error) {
      console.error("\u{1F4A5} [LegacyValidate] \u9A8C\u8BC1\u5F02\u5E38:", error);
      return NextResponse.json(
        { success: false, message: "\u9A8C\u8BC1\u5931\u8D25" },
        { status: 500 }
      );
    }
  };
}
function createLegacySendVerificationCodeHandler(config) {
  return async (request) => {
    try {
      const { phone } = await request.json();
      if (!validatePhoneNumber(phone)) {
        return NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7\u683C\u5F0F" },
          { status: 400 }
        );
      }
      const user = await config.authService.getUserByPhone(phone);
      if (!user) {
        return NextResponse.json(
          { success: false, message: "\u8BE5\u624B\u673A\u53F7\u672A\u6CE8\u518C" },
          { status: 404 }
        );
      }
      await config.authService.sendVerificationCode(phone);
      return NextResponse.json({
        success: true,
        message: "\u9A8C\u8BC1\u7801\u5DF2\u53D1\u9001"
      });
    } catch (error) {
      console.error("\u{1F4A5} [LegacySendVerificationCode] \u53D1\u9001\u5F02\u5E38:", error);
      return NextResponse.json(
        { success: false, message: "\u53D1\u9001\u9A8C\u8BC1\u7801\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5" },
        { status: 500 }
      );
    }
  };
}
function createLegacyResetPasswordHandler(config) {
  return async (request) => {
    try {
      const { phone, newPassword, verificationCode } = await request.json();
      if (!validatePhoneNumber(phone)) {
        return NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7\u683C\u5F0F" },
          { status: 400 }
        );
      }
      const passwordValidation = validatePassword(newPassword);
      if (!passwordValidation.valid) {
        return NextResponse.json(
          { success: false, message: passwordValidation.message || "\u5BC6\u7801\u683C\u5F0F\u9519\u8BEF" },
          { status: 400 }
        );
      }
      const isValidCode = await config.authService.verifyCode(phone, verificationCode);
      if (!isValidCode) {
        return NextResponse.json(
          { success: false, message: "\u9A8C\u8BC1\u7801\u65E0\u6548\u6216\u5DF2\u8FC7\u671F" },
          { status: 400 }
        );
      }
      await config.authService.resetPassword(phone, newPassword);
      return NextResponse.json({
        success: true,
        message: "\u5BC6\u7801\u91CD\u7F6E\u6210\u529F"
      });
    } catch (error) {
      console.error("\u{1F4A5} [LegacyResetPassword] \u91CD\u7F6E\u5F02\u5E38:", error);
      return NextResponse.json(
        { success: false, message: "\u91CD\u7F6E\u5BC6\u7801\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5" },
        { status: 500 }
      );
    }
  };
}

// src/auth/legacy/server.ts
function createLegacyValidateApiAuth(authService, config = {}) {
  const cookieName = config.cookieName || "session_token";
  return async (request) => {
    try {
      const sessionToken = request.cookies.get(cookieName)?.value;
      if (!sessionToken) return null;
      const validation = await authService.validateSession(sessionToken);
      return validation.valid && validation.user ? validation.user : null;
    } catch (error) {
      console.error("\u{1F4A5} [LegacyAuth] API\u6743\u9650\u9A8C\u8BC1\u5F02\u5E38:", error);
      return null;
    }
  };
}

export { LegacyAuthDbService, createLegacyLoginHandler, createLegacyLogoutHandler, createLegacyRegisterHandler, createLegacyResetPasswordHandler, createLegacySendVerificationCodeHandler, createLegacyValidateApiAuth, createLegacyValidateHandler, legacyUserSessions, legacyUserSessionsRelations, legacyUsers, legacyUsersRelations, legacyVerificationCodes };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map