'use strict';

var server = require('next/server');

// src/auth/legacy/routes/login.ts

// 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 server.NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u624B\u673A\u53F7\u548C\u5BC6\u7801" },
          { status: 400 }
        );
      }
      if (!validatePhoneNumber(phone)) {
        return server.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 server.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 server.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 = server.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 server.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 server.NextResponse.json(
          { success: false, message: "\u8BF7\u8F93\u5165\u624B\u673A\u53F7\u548C\u5BC6\u7801" },
          { status: 400 }
        );
      }
      if (!validatePhoneNumber(phone)) {
        return server.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 server.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 = server.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 server.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 = server.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 server.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 server.NextResponse.json(
          { success: true, valid: false, user: null, data: { valid: false, user: null } },
          { status: 200 }
        );
      }
      const validation = await config.authService.validateSession(sessionToken);
      return server.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 server.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 server.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 server.NextResponse.json(
          { success: false, message: "\u8BE5\u624B\u673A\u53F7\u672A\u6CE8\u518C" },
          { status: 404 }
        );
      }
      await config.authService.sendVerificationCode(phone);
      return server.NextResponse.json({
        success: true,
        message: "\u9A8C\u8BC1\u7801\u5DF2\u53D1\u9001"
      });
    } catch (error) {
      console.error("\u{1F4A5} [LegacySendVerificationCode] \u53D1\u9001\u5F02\u5E38:", error);
      return server.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 server.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 server.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 server.NextResponse.json(
          { success: false, message: "\u9A8C\u8BC1\u7801\u65E0\u6548\u6216\u5DF2\u8FC7\u671F" },
          { status: 400 }
        );
      }
      await config.authService.resetPassword(phone, newPassword);
      return server.NextResponse.json({
        success: true,
        message: "\u5BC6\u7801\u91CD\u7F6E\u6210\u529F"
      });
    } catch (error) {
      console.error("\u{1F4A5} [LegacyResetPassword] \u91CD\u7F6E\u5F02\u5E38:", error);
      return server.NextResponse.json(
        { success: false, message: "\u91CD\u7F6E\u5BC6\u7801\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5" },
        { status: 500 }
      );
    }
  };
}

exports.createLegacyLoginHandler = createLegacyLoginHandler;
exports.createLegacyLogoutHandler = createLegacyLogoutHandler;
exports.createLegacyRegisterHandler = createLegacyRegisterHandler;
exports.createLegacyResetPasswordHandler = createLegacyResetPasswordHandler;
exports.createLegacySendVerificationCodeHandler = createLegacySendVerificationCodeHandler;
exports.createLegacyValidateHandler = createLegacyValidateHandler;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map