
import { SessionDao } from "../dao/session.dao";
import { Common } from "../../utils/common";
import { AppError } from "../../utils/errors";
import Hapi, { Request } from '@hapi/hapi'

export class SessionService {
    static initiateSession = async (server: Hapi.Server): Promise<void> => {
        try {
            await SessionDao.initiateSession(server);
            console.log('Active sessions has been initialized');
            return
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_WITH_DAO', err);
        }
    }

    static setSession = async (request: Request, accountId: number | null, userId: number, token: Text): Promise<void> => {
        try {
            await SessionDao.set(request, accountId, userId, token);
            return;
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_WITH_DAO', err);
        }
    }

    static verifySession = async (request: Request, accountId: number | null, userId: number, token: Text): Promise<boolean> => {
        try {
            let session = await SessionDao.verify(request, accountId, userId, token);
            return session;
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_WITH_DAO', err);
        }
    }

    static remove = async (request: Request, accountId: number | null, userId: number, token: Text | null, removeAll: boolean): Promise<boolean> => {
        try {
            let session = await SessionDao.remove(request, accountId, userId, token, removeAll);
            return session;
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_WITH_DAO', err);
        }
    }
}
