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

export default defineEventHandler(async (event) => {
  try {
    // Get organization ID from route
    const id = parseInt(event.context.params.id);
    
    if (isNaN(id)) {
      return {
        statusCode: 400,
        message: "Invalid organization ID"
      };
    }
    
    // Get organization with its departments
    const organization = await prisma.organization.findUnique({
      where: {
        org_id: id
      },
      include: {
        departments: {
          select: {
            dp_id: true,
            dp_name: true,
            _count: {
              select: {
                users: true,
                cabinets: true
              }
            }
          }
        }
      }
    });
    
    if (!organization) {
      return {
        statusCode: 404,
        message: "Organization not found"
      };
    }
    
    return {
      statusCode: 200,
      data: organization
    };
  } catch (error) {
    console.error("Error fetching organization:", error);
    
    return {
      statusCode: 500,
      message: "Internal server error",
      error: error.message
    };
  }
}); 