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

export default defineEventHandler(async (event) => {
  try {
    // Get department ID from route
    const id = parseInt(event.context.params.id);
    
    if (isNaN(id)) {
      return {
        statusCode: 400,
        message: "Invalid department ID"
      };
    }
    
    // Get department with related data
    const department = await prisma.department.findUnique({
      where: {
        dp_id: id
      },
      include: {
        organization: {
          select: {
            org_id: true,
            org_name: true,
            org_country: true,
            org_state: true,
            org_active: true
          }
        },
        cabinets: {
          select: {
            cb_id: true,
            cb_name: true,
            cb_private: true
          }
        },
        user: {
          select: {
            userID: true,
            userUsername: true,
            userFullName: true,
            userEmail: true,
            userStatus: true
          }
        }
      }
    });
    
    if (!department) {
      return {
        statusCode: 404,
        message: "Department not found"
      };
    }
    
    return {
      statusCode: 200,
      data: department
    };
  } catch (error) {
    console.error("Error fetching department:", error);
    
    return {
      statusCode: 500,
      message: "Internal server error",
      error: error.message
    };
  }
}); 