/**
 * Types partagés pour les routes Fastify
 */
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import type { Pool } from "mysql2/promise";
import type { Transporter } from "nodemailer";
import type { AppConfig } from "../config.js";

export interface RouteContext {
  db: Pool;
  config: AppConfig;
  mailer: Transporter | null;
  signAccessToken: (user: {
    id: number;
    email: string;
    emailVerified?: boolean;
  }) => string;
  signRefreshToken: (user: {
    id: number;
    email: string;
    emailVerified?: boolean;
  }) => string;
  setRefreshCookie: (reply: FastifyReply, token: string) => void;
  getRefreshCookie: (request: FastifyRequest) => string | undefined;
  clearRefreshCookie: (reply: FastifyReply) => void;
  requireAuth: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
  requireAdmin: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
  packageVersion: string;
  appEnv: string;
  getOnlineUserIds?: () => number[];
  getUserLastSeen?: (userId: number) => number | null;
  onlinePresenceWindowMs?: number;
}

export type RoutePlugin = (
  app: FastifyInstance,
  ctx: RouteContext,
) => Promise<void>;
