import { ServerRoute } from '@hapi/hapi'
import { CommentHandler } from '../handlers/comment.handler'
import { options } from "../validators/global.validator.schema"
import { Common } from "../../utils/common"
import {
    commentCreateRequest,
    commentUpdateRequest,
    commentStatusUpdateRequest,
    commentIdentifier,
    commentResponse,
    commentListRequest,
    commentListResponse
} from "../validators/comment.validator"
import { I18N } from '../../utils/i18n';
import { error400, error401, error403, error404, error500, confirmationOnly } from "../validators/global.validator.schema"

const commentHandler = new CommentHandler();

const routes: ServerRoute[] = [
    {
        method: 'POST',
        path: '/comment',
        handler: commentHandler.create,
        options: {
            tags: ['api', 'Comment'],
            description: I18N.t('CREATE_COMMENT_DESCRIPTION'),
            notes: I18N.t('CREATE_COMMENT_NOTE'),
            plugins: { 'hapi-swagger': { order: 1 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: commentCreateRequest,
            },
            response: {
                status: {
                    201: commentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/comment/{id}',
        handler: commentHandler.getById,
        options: {
            tags: ['api', 'Comment'],
            description: I18N.t('GET_COMMENT_BY_ID_DESCRIPTION'),
            notes: I18N.t('GET_COMMENT_BY_ID_NOTE'),
            plugins: { 'hapi-swagger': { order: 2 } },
            auth: { strategies: ['jwt'], mode: 'optional' },
            validate: {
                headers: Common.routeHeaders("optionalauthorized"),
                options: options,
                failAction: Common.failover,
                params: commentIdentifier
            },
            response: {
                status: {
                    200: commentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/comment/list',
        handler: commentHandler.list,
        options: {
            tags: ['api', 'Comment'],
            description: I18N.t('LIST_COMMENT_DESCRIPTION'),
            notes: I18N.t('LIST_COMMENT_NOTE'),
            plugins: { 'hapi-swagger': { order: 3 } },
            auth: { strategies: ['jwt'], mode: 'optional' },
            validate: {
                headers: Common.routeHeaders("optionalauthorized"),
                options: options,
                failAction: Common.failover,
                query: commentListRequest
            },
            response: {
                status: {
                    200: commentListResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'PATCH',
        path: '/comment/{id}',
        handler: commentHandler.updateContent,
        options: {
            tags: ['api', 'Comment'],
            description: I18N.t('UPDATE_COMMENT_DESCRIPTION'),
            notes: I18N.t('UPDATE_COMMENT_NOTE'),
            plugins: { 'hapi-swagger': { order: 4 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user', 'managePost'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                params: commentIdentifier,
                payload: commentUpdateRequest
            },
            response: {
                status: {
                    200: commentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'PATCH',
        path: '/comment/{id}/status',
        handler: commentHandler.updateStatus,
        options: {
            tags: ['api', 'Comment'],
            description: I18N.t('UPDATE_COMMENT_STATUS_DESCRIPTION'),
            notes: I18N.t('UPDATE_COMMENT_STATUS_NOTE'),
            plugins: { 'hapi-swagger': { order: 5 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user', 'managePost'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                params: commentIdentifier,
                payload: commentStatusUpdateRequest
            },
            response: {
                status: {
                    200: commentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'DELETE',
        path: '/comment/{id}',
        handler: commentHandler.delete,
        options: {
            tags: ['api', 'Comment'],
            description: I18N.t('DELETE_COMMENT_DESCRIPTION'),
            notes: I18N.t('DELETE_COMMENT_NOTE'),
            plugins: { 'hapi-swagger': { order: 6 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user', 'managePost'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                params: commentIdentifier
            },
            response: {
                status: {
                    200: confirmationOnly,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    }
];

export default routes;
