import { validate } from '@/lib/http/validate';
import { requireAuth } from '@/middlewares/require-auth.middleware';
import { requireSuperAdmin } from '@/middlewares/require-super-admin.middleware';
import { blockUserHandler } from '@/modules/admin/handlers/block-user.handler';
import { createPlanHandler } from '@/modules/admin/handlers/create-plan.handler';
import { deletePlanHandler } from '@/modules/admin/handlers/delete-plan.handler';
import { getGeoStatsHandler } from '@/modules/admin/handlers/get-geo-stats.handler';
import { getUserSessionsHandler } from '@/modules/admin/handlers/get-user-sessions.handler';
import { listComplexesHandler } from '@/modules/admin/handlers/list-complexes.handler';
import { listPlansAdminHandler } from '@/modules/admin/handlers/list-plans-admin.handler';
import { listUsersHandler } from '@/modules/admin/handlers/list-users.handler';
import { revokeAllSessionsHandler } from '@/modules/admin/handlers/revoke-all-sessions.handler';
import { unblockUserHandler } from '@/modules/admin/handlers/unblock-user.handler';
import { updatePlanHandler } from '@/modules/admin/handlers/update-plan.handler';
import type { AppEnv } from '@/types/hono';
import {
  adminBlockUserSchema,
  adminCreatePlanSchema,
  adminUpdatePlanSchema,
} from '@repo/api-contract';
import { Hono } from 'hono';
import { z } from 'zod';

export const adminRoutes = new Hono<AppEnv>();

adminRoutes.use('*', requireAuth, requireSuperAdmin);

adminRoutes.get('/complexes', listComplexesHandler);

adminRoutes.get('/plans', listPlansAdminHandler);
adminRoutes.post('/plans', validate.json(adminCreatePlanSchema), createPlanHandler);
adminRoutes.patch(
  '/plans/:code',
  validate.param(z.object({ code: z.string().min(1).max(10) })),
  validate.json(adminUpdatePlanSchema),
  updatePlanHandler
);
adminRoutes.delete(
  '/plans/:code',
  validate.param(z.object({ code: z.string().min(1).max(10) })),
  deletePlanHandler
);

adminRoutes.get('/geo-stats', getGeoStatsHandler);

adminRoutes.get('/users', listUsersHandler);
adminRoutes.post(
  '/users/:id/block',
  validate.param(z.object({ id: z.string().min(1) })),
  validate.json(adminBlockUserSchema),
  blockUserHandler
);
adminRoutes.post(
  '/users/:id/unblock',
  validate.param(z.object({ id: z.string().min(1) })),
  unblockUserHandler
);
adminRoutes.get(
  '/users/:id/sessions',
  validate.param(z.object({ id: z.string().min(1) })),
  getUserSessionsHandler
);
adminRoutes.post(
  '/users/:id/sessions/revoke-all',
  validate.param(z.object({ id: z.string().min(1) })),
  revokeAllSessionsHandler
);
