import { JoiBuilder } from "../../utils/joiSchemaBuilder";

const wordpressDbConfigSchemaDef: SchemaDefinition = {
    host: { type: 'string', description: 'WORDPRESS_DB_HOST', example: '127.0.0.1', required: false },
    port: { type: 'number', description: 'WORDPRESS_DB_PORT', example: 3306, required: false },
    user: { type: 'string', description: 'WORDPRESS_DB_USER', example: 'wp_user', required: false },
    password: { type: 'string', description: 'WORDPRESS_DB_PASSWORD', example: 'wp_password', required: false },
    database: { type: 'string', description: 'WORDPRESS_DB_NAME', example: 'wordpress', required: false },
    prefix: { type: 'string', description: 'WORDPRESS_DB_PREFIX', example: 'wp_', required: false }
};

const wordpressImportRequestSchemaDef: SchemaDefinition = {
    chunkSize: { type: 'number', description: 'WORDPRESS_IMPORT_CHUNK_SIZE', example: 50, required: false, defaultValue: 50, min: 1 },
    page: { type: 'number', description: 'WORDPRESS_IMPORT_PAGE', example: 1, required: false, defaultValue: 1, min: 1 },
    startPostId: { type: 'number', description: 'WORDPRESS_IMPORT_START_POST_ID', example: 0, required: false, defaultValue: 0, min: 0 },
    postTypes: {
        type: 'array',
        label: 'wordpress-post-types',
        description: 'WORDPRESS_IMPORT_POST_TYPES',
        required: false,
        allowNull: true,
        items: { type: 'string', description: 'WORDPRESS_IMPORT_POST_TYPE', example: 'post' }
    },
    postType: { type: 'string', description: 'WORDPRESS_TARGET_POST_TYPE', example: 'post', required: false, defaultValue: 'post' },
    importPosts: { type: 'boolean', description: 'WORDPRESS_IMPORT_POSTS_FLAG', example: true, required: false, defaultValue: true },
    importComments: { type: 'boolean', description: 'WORDPRESS_IMPORT_COMMENTS_FLAG', example: true, required: false, defaultValue: true },
    importCategories: { type: 'boolean', description: 'WORDPRESS_IMPORT_CATEGORIES_FLAG', example: true, required: false, defaultValue: true },
    importTags: { type: 'boolean', description: 'WORDPRESS_IMPORT_TAGS_FLAG', example: true, required: false, defaultValue: true },
    authorId: { type: 'number', description: 'WORDPRESS_IMPORT_AUTHOR_ID', example: 1, required: false },
    commentUserId: { type: 'number', description: 'WORDPRESS_IMPORT_COMMENT_USER_ID', example: 1, required: false },
    wpDb: {
        type: 'object',
        label: 'wordpress-db-config',
        description: 'WORDPRESS_DB_CONFIG',
        required: false,
        fields: wordpressDbConfigSchemaDef
    }
};

const wordpressImportResponseDataSchemaDef: SchemaDefinition = {
    importedPosts: { type: 'number', description: 'WORDPRESS_IMPORTED_POSTS', example: 10, required: true },
    skippedPosts: { type: 'number', description: 'WORDPRESS_SKIPPED_POSTS', example: 2, required: true },
    importedComments: { type: 'number', description: 'WORDPRESS_IMPORTED_COMMENTS', example: 25, required: true },
    importedCategories: { type: 'number', description: 'WORDPRESS_IMPORTED_CATEGORIES', example: 5, required: true },
    importedTags: { type: 'number', description: 'WORDPRESS_IMPORTED_TAGS', example: 7, required: true },
    lastPostId: { type: 'number', description: 'WORDPRESS_LAST_POST_ID', example: 123, required: false, allowNull: true, defaultValue: null },
    hasMore: { type: 'boolean', description: 'WORDPRESS_HAS_MORE', example: true, required: true },
    totalPages: { type: 'number', description: 'WORDPRESS_IMPORT_TOTAL_PAGES', example: 10, required: false },
    currentPage: { type: 'number', description: 'WORDPRESS_IMPORT_CURRENT_PAGE', example: 1, required: false },
    errors: {
        type: 'array',
        label: 'wordpress-import-errors',
        description: 'WORDPRESS_IMPORT_ERRORS',
        required: false,
        allowEmpty: true,
        allowNull: true,
        items: {
            type: 'object',
            label: 'wordpress-import-error',
            description: 'WORDPRESS_IMPORT_ERROR',
            fields: {
                itemId: { type: 'number', description: 'WORDPRESS_IMPORT_ERROR_ITEM_ID', example: 101, required: false, allowNull: true },
                type: { type: 'string', description: 'WORDPRESS_IMPORT_ERROR_TYPE', example: 'post', required: false, allowNull: true },
                message: { type: 'string', description: 'WORDPRESS_IMPORT_ERROR_MESSAGE', example: 'POST_CONTENT_ALREADY_EXISTS', required: true }
            }
        }
    }
};

const wordpressImportResponseSchemaDef: SchemaDefinition = {
    message: { type: 'string', description: 'RESPONSE_MESSAGE', example: 'REQUEST_PROCESSED_SUCCESSFULLY' },
    responseData: {
        type: 'object',
        label: 'wordpress-import-response-data',
        description: 'WORDPRESS_IMPORT_RESPONSE_DATA',
        fields: wordpressImportResponseDataSchemaDef
    }
};

const userImportRequestSchemaDef: SchemaDefinition = {
    chunkSize: { type: 'number', description: 'IMPORT_CHUNK_SIZE', example: 100, required: false, defaultValue: 100, min: 1 },
    page: { type: 'number', description: 'IMPORT_PAGE', example: 1, required: false, defaultValue: 1, min: 1 },
    startUserId: { type: 'number', description: 'IMPORT_START_USER_ID', example: 0, required: false, defaultValue: 0, min: 0 },
    defaultRoleId: { type: 'number', description: 'IMPORT_DEFAULT_ROLE_ID', example: 2, required: false },
    fetchAll: { type: 'boolean', description: 'FETCH_ALL_USERS_FLAG', example: true, required: false, defaultValue: false },
    wpDb: {
        type: 'object',
        label: 'user-db-config',
        description: 'USER_SOURCE_DB_CONFIG',
        required: false,
        fields: wordpressDbConfigSchemaDef
    }
};

const userImportResponseDataSchemaDef: SchemaDefinition = {
    imported: { type: 'number', description: 'IMPORTED_USERS', example: 10, required: true },
    skipped: { type: 'number', description: 'SKIPPED_USERS', example: 2, required: true },
    errors: { type: 'number', description: 'ERROR_USERS', example: 1, required: true },
    lastUserId: { type: 'number', description: 'LAST_USER_ID', example: 123, required: false, allowNull: true, defaultValue: null },
    hasMore: { type: 'boolean', description: 'HAS_MORE_USERS', example: true, required: true },
    totalPages: { type: 'number', description: 'TOTAL_PAGES', example: 10, required: false },
    currentPage: { type: 'number', description: 'CURRENT_PAGE', example: 1, required: false },
    errorLog: {
        type: 'array',
        label: 'user-import-error-log',
        description: 'USER_IMPORT_ERROR_LOG',
        required: false,
        allowEmpty: true,
        allowNull: true,
        items: {
            type: 'object',
            label: 'user-import-error-item',
            description: 'USER_IMPORT_ERROR_ITEM',
            fields: {
                email: { type: 'string', description: 'USER_EMAIL', required: true },
                error: { type: 'string', description: 'ERROR_MESSAGE', required: true }
            }
        }
    }
};

const userImportResponseSchemaDef: SchemaDefinition = {
    message: { type: 'string', description: 'RESPONSE_MESSAGE', example: 'REQUEST_PROCESSED_SUCCESSFULLY' },
    responseData: {
        type: 'object',
        label: 'user-import-response-data',
        description: 'USER_IMPORT_RESPONSE_DATA',
        fields: userImportResponseDataSchemaDef
    }
};

const seaQAImportRequestSchemaDef: SchemaDefinition = {
    chunkSize: { type: 'number', description: 'IMPORT_CHUNK_SIZE', example: 100, required: false, defaultValue: 100, min: 1 },
    page: { type: 'number', description: 'IMPORT_PAGE', example: 1, required: false, defaultValue: 1, min: 1 },
    wpDb: {
        type: 'object',
        label: 'seaqa-db-config',
        description: 'SEAQA_SOURCE_DB_CONFIG',
        required: false,
        fields: wordpressDbConfigSchemaDef
    }
};

const seaQAImportResponseDataSchemaDef: SchemaDefinition = {
    importedQuestions: { type: 'number', description: 'IMPORTED_QUESTIONS', example: 10, required: true },
    skippedQuestions: { type: 'number', description: 'SKIPPED_QUESTIONS', example: 2, required: true },
    importedAnswers: { type: 'number', description: 'IMPORTED_ANSWERS', example: 10, required: true },
    skippedAnswers: { type: 'number', description: 'SKIPPED_ANSWERS', example: 2, required: true },
    importedComments: { type: 'number', description: 'IMPORTED_COMMENTS', example: 10, required: true },
    skippedComments: { type: 'number', description: 'SKIPPED_COMMENTS', example: 2, required: true },
    errors: { type: 'number', description: 'ERRORS_COUNT', example: 1, required: true },
    hasMore: { type: 'boolean', description: 'HAS_MORE', example: true, required: true },
    totalPages: { type: 'number', description: 'TOTAL_PAGES', example: 10, required: false },
    currentPage: { type: 'number', description: 'CURRENT_PAGE', example: 1, required: false },
    errorLog: {
        type: 'array',
        label: 'seaqa-import-error-log',
        description: 'SEAQA_IMPORT_ERROR_LOG',
        required: false,
        allowEmpty: true,
        allowNull: true,
        items: {
            type: 'object',
            label: 'seaqa-import-error-item',
            description: 'SEAQA_IMPORT_ERROR_ITEM',
            fields: {
                entityId: { type: 'number', description: 'ENTITY_ID', required: true },
                type: { type: 'string', description: 'ENTITY_TYPE', required: true },
                error: { type: 'string', description: 'ERROR_MESSAGE', required: true }
            }
        }
    }
};

const seaQAImportResponseSchemaDef: SchemaDefinition = {
    message: { type: 'string', description: 'RESPONSE_MESSAGE', example: 'REQUEST_PROCESSED_SUCCESSFULLY' },
    responseData: {
        type: 'object',
        label: 'seaqa-import-response-data',
        description: 'SEAQA_IMPORT_RESPONSE_DATA',
        fields: seaQAImportResponseDataSchemaDef
    }
};

export const wordpressImportRequest = JoiBuilder.buildJoiSchema(wordpressImportRequestSchemaDef)
    .label("wordpress-import-request")
    .description("WORDPRESS_IMPORT_REQUEST_SCHEMA");

export const wordpressImportResponse = JoiBuilder.buildJoiSchema(wordpressImportResponseSchemaDef)
    .label("wordpress-import-response")
    .description("WORDPRESS_IMPORT_RESPONSE_SCHEMA");

export const userImportRequest = JoiBuilder.buildJoiSchema(userImportRequestSchemaDef)
    .label("user-import-request")
    .description("USER_IMPORT_REQUEST_SCHEMA");

export const userImportResponse = JoiBuilder.buildJoiSchema(userImportResponseSchemaDef)
    .label("user-import-response")
    .description("USER_IMPORT_RESPONSE_SCHEMA");

export const seaQAImportRequest = JoiBuilder.buildJoiSchema(seaQAImportRequestSchemaDef)
    .label("seaqa-import-request")
    .description("SEAQA_IMPORT_REQUEST_SCHEMA");

export const seaQAImportResponse = JoiBuilder.buildJoiSchema(seaQAImportResponseSchemaDef)
    .label("seaqa-import-response")
    .description("SEAQA_IMPORT_RESPONSE_SCHEMA");
