import { JWT } from "@fastify/jwt";
import { $Enums } from "@prisma/client";
import { FastifyBaseLogger, FastifyInstance } from "fastify";
import { ZodTypeProvider } from "fastify-type-provider-zod";
import { IncomingMessage, Server, ServerResponse } from "http";

//------------------------------------------------------------------------

declare module "fastify" {
  interface FastifyRequest {
    jwt: JWT;
  }
  export interface FastifyInstance {
    auth: (
      req: FastifyRequest,
      reply: FastifyReply,
      privateAccess: boolean | undefined
    ) => void;
    adminAuth: (
      req: FastifyRequest,
      reply: FastifyReply,
      permission: $Enums.Permission | null | undefined
    ) => void;
  }
}

type UserPayload = {
  id: string;
  email: string;
  role: $Enums.Role;
  permissions: $Enums.Permission[];
};

declare module "@fastify/jwt" {
  interface FastifyJWT {
    user: UserPayload;
  }
}

//------------------------------------------------------------------------

export type FastifyInstanceType = FastifyInstance<
  Server<typeof IncomingMessage, typeof ServerResponse>,
  IncomingMessage,
  ServerResponse<IncomingMessage>,
  FastifyBaseLogger,
  ZodTypeProvider
>;
