import { requireAuth } from '@/middlewares/require-auth.middleware';
import { acceptComplexInvitationsHandler } from '@/modules/complex/handlers/accept-complex-invitations.handler';
import { cancelComplexInvitationHandler } from '@/modules/complex/handlers/cancel-complex-invitation.handler';
import { createComplexHandler } from '@/modules/complex/handlers/create-complex.handler';
import { getComplexByIdHandler } from '@/modules/complex/handlers/get-complex-by-id.handler';
import { getComplexBySlugHandler } from '@/modules/complex/handlers/get-complex-by-slug.handler';
import { getCurrentComplexHandler } from '@/modules/complex/handlers/get-current-complex.handler';
import { inviteComplexUserHandler } from '@/modules/complex/handlers/invite-complex-user.handler';
import { listComplexUsersHandler } from '@/modules/complex/handlers/list-complex-users.handler';
import { listMyComplexesHandler } from '@/modules/complex/handlers/list-my-complexes.handler';
import { resendComplexInvitationHandler } from '@/modules/complex/handlers/resend-complex-invitation.handler';
import { revokeComplexUserHandler } from '@/modules/complex/handlers/revoke-complex-user.handler';
import { selectComplexHandler } from '@/modules/complex/handlers/select-complex.handler';
import { updateComplexHandler } from '@/modules/complex/handlers/update-complex.handler';
import type { AppEnv } from '@/types/hono';
import { zValidator } from '@hono/zod-validator';
import {
  acceptComplexInvitationsSchema,
  createComplexSchema,
  inviteComplexUserSchema,
  updateComplexSchema,
} from '@repo/api-contract';
import { Hono } from 'hono';
import { z } from 'zod';

export const complexRoutes = new Hono<AppEnv>();
const complexIdParamsSchema = z.object({ id: z.uuid() });
const complexSlugParamsSchema = z.object({ slug: z.string().trim().min(1) });

complexRoutes.use('*', requireAuth);

complexRoutes.post('/', zValidator('json', createComplexSchema), createComplexHandler);
complexRoutes.get('/me', getCurrentComplexHandler);
complexRoutes.post('/select', selectComplexHandler);
complexRoutes.post(
  '/accept-pending',
  zValidator('json', acceptComplexInvitationsSchema),
  acceptComplexInvitationsHandler
);
complexRoutes.get('/mine', listMyComplexesHandler);
complexRoutes.get(
  '/slug/:slug',
  zValidator('param', complexSlugParamsSchema),
  getComplexBySlugHandler
);

complexRoutes.get(
  '/:id/users',
  zValidator('param', complexIdParamsSchema),
  listComplexUsersHandler
);
complexRoutes.post(
  '/:id/users',
  zValidator('param', complexIdParamsSchema),
  zValidator('json', inviteComplexUserSchema),
  inviteComplexUserHandler
);
complexRoutes.post(
  '/:id/invitations/:invitationId/resend',
  zValidator('param', z.object({ id: z.uuid(), invitationId: z.uuid() })),
  resendComplexInvitationHandler
);
complexRoutes.delete(
  '/:id/invitations/:invitationId',
  zValidator('param', z.object({ id: z.uuid(), invitationId: z.uuid() })),
  cancelComplexInvitationHandler
);
complexRoutes.delete(
  '/:id/users/:userId',
  zValidator('param', z.object({ id: z.uuid(), userId: z.string().min(1) })),
  revokeComplexUserHandler
);

complexRoutes.get('/:id', zValidator('param', complexIdParamsSchema), getComplexByIdHandler);
complexRoutes.patch(
  '/:id',
  zValidator('param', complexIdParamsSchema),
  zValidator('json', updateComplexSchema),
  updateComplexHandler
);
