import { ServerRoute } from '@hapi/hapi'
import { WordpressImportHandler } from '../handlers/wordpressImport.handler'
import { options, error400, error401, error403, error404, error500 } from "../validators/global.validator.schema"
import { Common } from "../../utils/common"
import { wordpressImportRequest, wordpressImportResponse, userImportRequest, userImportResponse, seaQAImportRequest, seaQAImportResponse } from "../validators/wordpressImport.validator"
import { I18N } from '../../utils/i18n';

const wordpressImportHandler = new WordpressImportHandler();

const routes: ServerRoute[] = [
    {
        method: 'POST',
        path: '/wordpress/import',
        handler: wordpressImportHandler.importChunk,
        options: {
            tags: ['api', 'Wordpress'],
            description: I18N.t('WORDPRESS_IMPORT_DESCRIPTION'),
            notes: I18N.t('WORDPRESS_IMPORT_NOTE'),
            plugins: { 'hapi-swagger': { order: 1 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'managePost'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: wordpressImportRequest
            },
            response: {
                status: {
                    200: wordpressImportResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        }
    },
    {
        method: 'POST',
        path: '/wordpress/import-users',
        handler: wordpressImportHandler.importUsers,
        options: {
            tags: ['api', 'Wordpress'],
            description: 'Import users from external source database',
            notes: 'Fetches users from source and migrates to local system',
            plugins: { 'hapi-swagger': { order: 2 } },
            auth: false,
            validate: {
                options: options,
                failAction: Common.failover,
                payload: userImportRequest
            },
            response: {
                status: {
                    200: userImportResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        }
    },
    {
        method: 'POST',
        path: '/wordpress/import-seaqa',
        handler: wordpressImportHandler.importSeaQA,
        options: {
            tags: ['api', 'Wordpress'],
            description: 'Import SeaQA data from external source database',
            notes: 'Fetches SeaQA questions, answers, and comments from source and migrates to local system',
            plugins: { 'hapi-swagger': { order: 3 } },
            auth: { strategies: ['jwt'], scope: ['admin'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: seaQAImportRequest
            },
            response: {
                status: {
                    200: seaQAImportResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        }
    }
];

export default routes;
