import { AdminBookingServiceError } from '@/modules/admin-booking/services/admin-booking.service';
import { updateRecurringGroup } from '@/modules/admin-booking/services/admin-recurring-booking.service';
import type { AppContext } from '@/types/hono';
import type { UpdateRecurringGroupInput } from '@repo/api-contract';

type GroupIdParams = { groupId: string };

export async function updateRecurringGroupHandler(c: AppContext) {
  const { groupId } = c.req.valid('param' as never) as GroupIdParams;
  const payload = c.req.valid('json' as never) as UpdateRecurringGroupInput;
  const user = c.get('user');

  try {
    const group = await updateRecurringGroup(user.id, groupId, payload);
    return c.json(group);
  } catch (error) {
    if (error instanceof AdminBookingServiceError) {
      return c.json({ message: error.message }, error.status);
    }
    throw error;
  }
}
