import { d as db, a as adminUsers } from '../../admin/create-upload-progress-table/POST-C-OoiMoW.js';
import { eq } from 'drizzle-orm';

async function handler(req, res) {
  try {
    const users = await db.select({
      id: adminUsers.id,
      username: adminUsers.username,
      email: adminUsers.email,
      fullName: adminUsers.fullName,
      role: adminUsers.role
    }).from(adminUsers).where(eq(adminUsers.isActive, true)).limit(1);
    if (users.length === 0) {
      return res.status(404).json({
        success: false,
        error: "No admin user found"
      });
    }
    const user = users[0];
    let initials = "A";
    if (user.fullName) {
      const names = user.fullName.split(" ");
      initials = names.map((n) => n[0]).join("").toUpperCase().slice(0, 2);
    } else if (user.username) {
      initials = user.username.slice(0, 2).toUpperCase();
    }
    res.json({
      success: true,
      user: {
        id: user.id,
        username: user.username,
        email: user.email,
        fullName: user.fullName,
        role: user.role,
        initials
      }
    });
  } catch (error) {
    if (error?.code === "ER_NO_SUCH_TABLE" || error?.cause?.code === "ER_NO_SUCH_TABLE") {
      return res.status(404).json({
        success: false,
        error: "No admin user found",
        setupRequired: true
      });
    }
    console.error("Failed to get current user:", error);
    res.status(500).json({
      success: false,
      error: "Failed to get current user",
      message: String(error)
    });
  }
}

export { handler as h };
