import { EntityManager } from '@mikro-orm/core';
import { FastifyReply } from 'fastify';
import { SignOptions, VerifyOptions } from 'jsonwebtoken';

declare module 'fastify' {
    interface AppOptions {
        jwt: {
            sign: (payload: object, options?: SignOptions) => string;
            verify: (token: string, options?: VerifyOptions) => object;
        };
    }

    interface FastifyInstance {
        user?: User;

        orm: {
            em: EntityManager;
        };
        jwt: {
            sign: (payload: object, options?: SignOptions) => string;
            verify: (token: string, options?: VerifyOptions) => object;
        };
        authenticate: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
    }

    interface FastifyRequest {
        diContainer: DIContainer;
        em: EntityManager;
        jwtVerify: () => Promise<void>;
        // controllers
    }
}
declare module 'fastify' {
    interface FastifyRequest {
        userDetails?: jwt.JwtPayload & { roles: string[] };
    }
}
