import { getCurrentComplex } from '@/modules/complex/services/complex.service';
import type { AppContext } from '@/types/hono';
import { getCookie } from 'hono/cookie';

const SELECTED_COMPLEX_COOKIE = 'selected-complex-id';

export async function getCurrentComplexHandler(c: AppContext) {
  const user = c.get('user');
  const complexId = getCookie(c, SELECTED_COMPLEX_COOKIE);

  if (!complexId) {
    return c.json(null);
  }

  const complex = await getCurrentComplex(user.id, complexId);

  if (!complex) {
    return c.json(null);
  }

  return c.json(complex);
}
