import { PrismaClient } from '@prisma/client';

// Initialize Prisma client
const prisma = new PrismaClient();

export default defineEventHandler(async (event) => {
  // Get the task ID from the route params
  const taskId = event.context.params.id;
  
  try {
    // Find the task
    let task;
    
    if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(taskId)) {
      // If it looks like a UUID
      task = await prisma.task.findUnique({
        where: { taskUUID: taskId },
        include: {
          case: {
            select: {
              caseID: true,
              caseUUID: true,
              caseName: true,
              caseStatus: true,
              process: {
                select: {
                  processID: true,
                  processUUID: true,
                  processName: true
                }
              }
            }
          },
          assignedTo: {
            select: {
              userID: true,
              userFullName: true,
              userUsername: true
            }
          },
          form: {
            select: {
              formID: true,
              formUUID: true,
              formName: true,
              formDescription: true,
              formComponents: true,
              formStatus: true,
              customCSS: true,
              customScript: true,
              formEvents: true,
              scriptMode: true
            }
          }
        }
      });
    } else if (!isNaN(parseInt(taskId))) {
      // If it's a numeric ID
      task = await prisma.task.findUnique({
        where: { taskID: parseInt(taskId) },
        include: {
          case: {
            select: {
              caseID: true,
              caseUUID: true,
              caseName: true,
              caseStatus: true,
              process: {
                select: {
                  processID: true,
                  processUUID: true,
                  processName: true
                }
              }
            }
          },
          assignedTo: {
            select: {
              userID: true,
              userFullName: true,
              userUsername: true
            }
          },
          form: {
            select: {
              formID: true,
              formUUID: true,
              formName: true,
              formDescription: true,
              formComponents: true,
              formStatus: true,
              customCSS: true,
              customScript: true,
              formEvents: true,
              scriptMode: true
            }
          }
        }
      });
    }
    
    if (!task) {
      return {
        success: false,
        error: 'Task not found'
      };
    }
    
    return {
      success: true,
      task
    };
  } catch (error) {
    console.error(`Error fetching task ${taskId}:`, error);
    
    return {
      success: false,
      error: 'Failed to fetch task',
      details: process.env.NODE_ENV === 'development' ? error.message : undefined
    };
  }
}); 