import { Request, ResponseToolkit, ResponseObject } from '@hapi/hapi';
import { CommentService } from "../services/comment.service";
import { AppError } from "../../utils/errors";
import { Common } from "../../utils/common"

export class CommentHandler {
    private getCommentServiceObject = async (request: Request) => {
        let variables = await Common.getVariables(request);
        let commentService = new CommentService(variables);
        return commentService;
    }

    create = async (request: Request, h: ResponseToolkit): Promise<ResponseObject> => {
        try {
            let commentService = await this.getCommentServiceObject(request);
            const payload = request.payload as CommentCreateRequestObject;
            let comment = await commentService.createComment(payload);
            return h.response({ message: "REQUEST_PROCESSED_SUCCESSFULLY", responseData: comment }).code(201);
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_IN_SERVICE', err);
        }
    }

    getById = async (request: Request, h: ResponseToolkit): Promise<ResponseObject> => {
        try {
            let commentService = await this.getCommentServiceObject(request);
            const { id } = request.params as any;
            let comment = await commentService.getById(id);
            return h.response({ message: "REQUEST_PROCESSED_SUCCESSFULLY", responseData: comment }).code(200);
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_IN_SERVICE', err);
        }
    }

    list = async (request: Request, h: ResponseToolkit): Promise<ResponseObject> => {
        try {
            let commentService = await this.getCommentServiceObject(request);
            let listRequest = request.query as unknown as CommentListRequestObject;
            let comments = await commentService.getList(listRequest);
            return h.response({ message: "REQUEST_PROCESSED_SUCCESSFULLY", responseData: comments }).code(200);
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_IN_SERVICE', err);
        }
    }

    updateStatus = async (request: Request, h: ResponseToolkit): Promise<ResponseObject> => {
        try {
            let commentService = await this.getCommentServiceObject(request);
            const { id } = request.params as any;
            const { status } = request.payload as any;
            let comment = await commentService.updateStatus(id, status);
            return h.response({ message: "REQUEST_PROCESSED_SUCCESSFULLY", responseData: comment }).code(200);
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_IN_SERVICE', err);
        }
    }

    updateContent = async (request: Request, h: ResponseToolkit): Promise<ResponseObject> => {
        try {
            let commentService = await this.getCommentServiceObject(request);
            const { id } = request.params as any;
            const payload = request.payload as CommentUpdateRequestObject;
            let comment = await commentService.updateContent(id, payload);
            return h.response({ message: "REQUEST_PROCESSED_SUCCESSFULLY", responseData: comment }).code(200);
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_IN_SERVICE', err);
        }
    }

    delete = async (request: Request, h: ResponseToolkit): Promise<ResponseObject> => {
        try {
            let commentService = await this.getCommentServiceObject(request);
            const { id } = request.params as any;
            await commentService.deleteComment(id);
            return h.response({ message: "REQUEST_PROCESSED_SUCCESSFULLY" }).code(200);
        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_IN_SERVICE', err);
        }
    }
}
