import prisma from "~/server/utils/prisma";

export default defineEventHandler(async (event) => {
  try {
    // Get current user from auth middleware
    const user = event.context.user;
    if (!user) {
      throw createError({
        statusCode: 401,
        statusMessage: "Authentication required",
      });
    }

    // Get delivery settings
    const settings = await prisma.notification_delivery_settings.findFirst({
      select: {
        auto_retry: true,
        enable_fallback: true,
        max_retries: true,
        retry_delay: true,
        priority: true,
        enable_reports: true,
        created_at: true,
        updated_at: true
      }
    });

    if (!settings) {
      return {
        success: true,
        data: {
          autoRetry: true,
          enableFallback: true,
          maxRetries: 3,
          retryDelay: 30,
          priority: 'normal',
          enableReports: true
        }
      };
    }

    return {
      success: true,
      data: {
        autoRetry: settings.auto_retry,
        enableFallback: settings.enable_fallback,
        maxRetries: settings.max_retries,
        retryDelay: settings.retry_delay,
        priority: settings.priority,
        enableReports: settings.enable_reports
      }
    };
  } catch (error) {
    console.error('Error fetching delivery settings:', error);
    
    if (error.statusCode) {
      throw error;
    }
    
    throw createError({
      statusCode: 500,
      statusMessage: 'Failed to fetch delivery settings'
    });
  } finally {
  }
}); 