import prisma from "../../utils/prisma";

export default defineEventHandler(async (event) => {
  try {
    // Get user ID from route
    const id = parseInt(event.context.params.id);
    
    if (isNaN(id)) {
      return {
        statusCode: 400,
        message: "Invalid user ID"
      };
    }
    
    // Get user with related data
    const user = await prisma.user.findUnique({
      where: {
        userID: id
      },
      select: {
        userID: true,
        userUsername: true,
        userFullName: true,
        userEmail: true,
        userPhone: true,
        userStatus: true,
        dp_id: true,
        userCreatedDate: true,
        userModifiedDate: true,
        department: {
          select: {
            dp_id: true,
            dp_name: true,
            organization: {
              select: {
                org_id: true,
                org_name: true
              }
            }
          }
        },
        userrole: {
          select: {
            userRoleID: true,
            role: {
              select: {
                roleID: true,
                roleName: true,
                roleDescription: true
              }
            }
          }
        }
      }
    });
    
    if (!user) {
      return {
        statusCode: 404,
        message: "User not found"
      };
    }
    
    return {
      statusCode: 200,
      data: user
    };
  } catch (error) {
    console.error("Error fetching user:", error);
    
    return {
      statusCode: 500,
      message: "Internal server error",
      error: error.message
    };
  }
}); 