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

export default defineEventHandler(async (event) => {
  try {
    const user = event.context.user;
    if (!user) {
      throw createError({
        statusCode: 401,
        statusMessage: "Authentication required",
      });
    }
    const smsConfig = await prisma.notification_delivery_config.findFirst({
      where: { channel_type: 'sms' },
      select: {
        is_enabled: true,
        provider: true,
        provider_config: true,
        status: true,
        success_rate: true,
        created_at: true,
        updated_at: true
      }
    });
    if (!smsConfig) {
      return {
        success: true,
        data: {
          enabled: false,
          provider: 'twilio',
          status: 'Not Configured',
          successRate: 0
        }
      };
    }
    return {
      success: true,
      data: {
        enabled: smsConfig.is_enabled,
        provider: smsConfig.provider,
        status: smsConfig.status,
        successRate: smsConfig.success_rate,
        config: smsConfig.provider_config
      }
    };
  } catch (error) {
    console.error('Error fetching SMS configuration:', error);
    if (error.statusCode) throw error;
    throw createError({
      statusCode: 500,
      statusMessage: 'Failed to fetch SMS configuration'
    });
  } finally {
  }
}); 