import { ServerRoute } from '@hapi/hapi'
import { AttachmentHandler } from '../handlers/attachment.handler'
import { options } from "../validators/global.validator.schema"
import { Common } from "../../utils/common"
import {
    attachmentResponse,
    uploadRequest,
    getByIdRequest,
    getByUniqueNameRequest,
    sizeRequest,
    attachmentListRequest,
    attachmentListResponse,
    attachmentDownloadResponse,
} from "../validators/attachment.validator"
const attachmentHandler = new AttachmentHandler();
import { error400, error401, error403, error404, error500, confirmationOnly } from "../validators/global.validator.schema"
import { I18N } from '../../utils/i18n';

const routes: ServerRoute[] = [
    {
        method: 'POST',
        path: '/attachment',
        handler: attachmentHandler.upload,
        options: {
            tags: ['api', 'Attachment'],
            description: I18N.t('UPLOAD_FILE_TO_SERVER_DESCRIPTION'),
            notes: I18N.t('UPLOAD_FILE_TO_SERVER_NOTE'),
            auth: { strategies: ['jwt'], mode: 'optional' },
            payload: {
                maxBytes: 1025 * 1024 * +process.env.MAX_UPLOAD_LIMIT!,
                output: 'stream',
                parse: true,
                multipart: true,
                allow: 'multipart/form-data',
            },
            plugins: { 'hapi-swagger': { payloadType: 'form', order: 1 } },
            validate: {
                headers: Common.routeHeaders("optionalauthorized"),
                options: options,
                failAction: Common.failover,
                payload: uploadRequest,
            },
            response: {
                status: {
                    201: attachmentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: "GET",
        path: "/attachment/byId/{id}",
        handler: attachmentHandler.view,
        options: {
            tags: ["api", "Attachment"],
            description: I18N.t('VIEW_FILE_BY_ID_DESCRIPTION'),
            notes: I18N.t('VIEW_FILE_NOTE'),
            plugins: {
                'hapi-swagger': {
                    order: 2,

                    responses: {
                        200: {
                            description: 'Successful attachment view by ID',
                            schema: {
                                type: 'string', // Indicate a file stream
                                format: 'binary' // Indicate binary content
                            }
                        }
                    }
                }
            },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                params: getByIdRequest,
                failAction: Common.failover
            },
            response: {
                status: {
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: "GET",
        path: "/attachment/byId/{id}/download",
        handler: attachmentHandler.download,
        options: {
            tags: ["api", "Attachment"],
            description: I18N.t('DOWNLOAD_FILE_BY_ID_DESCRIPTION'),
            notes: I18N.t('DOWNLOAD_FILE_NOTE'),
            plugins: { 'hapi-swagger': { order: 3 } },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                params: getByIdRequest,
                failAction: Common.failover
            },
            response: {
                status: {
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: "GET",
        path: "/attachment/{uniqueName}",
        handler: attachmentHandler.view,
        options: {
            tags: ["api", "Attachment"],
            description: I18N.t('VIEW_FILE_BY_UNIQUE_NAME_DESCRIPTION'),
            notes: I18N.t('VIEW_FILE_NOTE'),
            plugins: {
                'hapi-swagger': {
                    order: 2,

                    responses: {
                        200: {
                            description: 'Successful attachment view by unique name',
                            schema: {
                                type: 'string', // Indicate a file stream
                                format: 'binary' // Indicate binary content
                            }
                        }
                    }
                }
            },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                params: getByUniqueNameRequest,
                failAction: Common.failover
            },
            response: {
                status: {
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: "GET",
        path: "/attachment/{uniqueName}/download",
        handler: attachmentHandler.download,
        options: {
            tags: ["api", "Attachment"],
            description: I18N.t('DOWNLOAD_FILE_BY_UNIQUE_NAME_DESCRIPTION'),
            notes: I18N.t('DOWNLOAD_FILE_NOTE'),
            plugins: { 'hapi-swagger': { order: 3 } },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                params: getByUniqueNameRequest,
                failAction: Common.failover
            },
            response: {
                status: {
                    // 200: attachmentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: "DELETE",
        path: "/attachment/byId/{id}",
        handler: attachmentHandler.delete,
        options: {
            tags: ["api", "Attachment"],
            description: I18N.t('DELETE_FILE_BY_ID_DESCRIPTION'),
            notes: I18N.t('DELETE_FILE_NOTE'),
            plugins: { 'hapi-swagger': { order: 2 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user'] },
            validate: {
                headers: Common.routeHeaders("optionalauthorized"),
                options: options,
                params: getByIdRequest,
                failAction: Common.failover
            },
            response: {
                status: {
                    200: attachmentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: "DELETE",
        path: "/attachment/{uniqueName}",
        handler: attachmentHandler.delete,
        options: {
            tags: ["api", "Attachment"],
            description: I18N.t('DELETE_FILE_BY_UNIQUE_NAME_DESCRIPTION'),
            notes: I18N.t('DELETE_FILE_NOTE'),
            plugins: { 'hapi-swagger': { order: 2 } },
            auth: { strategies: ['jwt'], scope: ['admin'] },
            validate: {
                headers: Common.routeHeaders("optionalauthorized"),
                options: options,
                params: getByUniqueNameRequest,
                failAction: Common.failover
            },
            response: {
                status: {
                    200: attachmentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: "GET",
        path: "/attachment/list",
        handler: attachmentHandler.list,
        options: {
            tags: ['api', 'Attachment'],
            description: I18N.t('GET_ALL_FILE_DESCRIPTION'),
            notes: I18N.t('GET_ALL_FILE_NOTE'),
            plugins: { 'hapi-swagger': { order: 6 } },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                failAction: Common.failover,
                query: attachmentListRequest
            },
            response: {
                status: {
                    200: attachmentListResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
]
export default routes
