import { q as query } from '../../../../chunks/db_Du3AmLCY.mjs';
import { s as signToken, c as comparePassword } from '../../../../chunks/auth_C2RFIYRJ.mjs';
export { renderers } from '../../../../renderers.mjs';

const POST = async ({ request }) => {
  try {
    const { email, password } = await request.json();
    if (!email || !password) {
      return new Response(JSON.stringify({ error: "Email and password required" }), { status: 400 });
    }
    const envEmail = process.env.ADMIN_EMAIL || "";
    const envPassword = process.env.ADMIN_PASSWORD || "";
    if (envEmail && envPassword && email === envEmail && password === envPassword) {
      const token2 = signToken({ email: envEmail, role: "admin" });
      return new Response(JSON.stringify({ token: token2, user: { email: envEmail, name: "Admin" } }), {
        headers: {
          "Content-Type": "application/json",
          "Set-Cookie": `admin_token=${token2}; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=${7 * 86400}`
        }
      });
    }
    const result = await query("SELECT * FROM admin_users WHERE email = $1 LIMIT 1", [email]);
    if (!result.rows.length) {
      return new Response(JSON.stringify({ error: "Invalid credentials" }), { status: 401 });
    }
    const user = result.rows[0];
    const valid = await comparePassword(password, user.password_hash);
    if (!valid) {
      return new Response(JSON.stringify({ error: "Invalid credentials" }), { status: 401 });
    }
    const token = signToken({ email: user.email, role: "admin" });
    return new Response(JSON.stringify({ token, user: { email: user.email, name: user.name } }), {
      headers: {
        "Content-Type": "application/json",
        "Set-Cookie": `admin_token=${token}; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=${7 * 86400}`
      }
    });
  } catch (err) {
    console.error("Login error:", err);
    return new Response(JSON.stringify({ error: "Internal error" }), { status: 500 });
  }
};

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  POST
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
