import { getAuthenticatedUserId } from '../../utils/auth';

interface HabitDto {
  id: number;
  name: string;
  daysOfWeek: number[];
}

export default defineEventHandler(async (event): Promise<HabitDto> => {
  const userId = getAuthenticatedUserId(event);
  const { name, daysOfWeek } = await readBody(event);

  // --- Validation ---
  if (!name || !Array.isArray(daysOfWeek)) {
    throw createError({ statusCode: 400, statusMessage: 'Invalid input: name and daysOfWeek are required.' });
  }

  // Sanitize daysOfWeek to ensure it's a unique set of valid numbers
  const validDays = daysOfWeek.filter(day => typeof day === 'number' && day >= 0 && day <= 6);
  const sanitizedDays = [...new Set(validDays)].sort();


  // --- Business Rule: Max 3 Habits ---
  const habitCount = await prisma.habit.count({ where: { userId } });
  if (habitCount >= 3) {
    throw createError({ statusCode: 400, statusMessage: 'Maximum number of 3 habits reached.' });
  }

  // --- Create Habit ---
  const newHabit = await prisma.habit.create({
    data: {
      userId,
      name,
      daysOfWeek: sanitizedDays,
    },
  });

  // Set 201 Created status
  setResponseStatus(event, 201);
  
  // Return DTO
  return {
    id: newHabit.id,
    name: newHabit.name,
    daysOfWeek: newHabit.daysOfWeek as number[],
  };
});
