import { Sequelize, QueryInterface, QueryTypes } from 'sequelize';
import * as Constants from "../src/api/config/constants"
import { permission } from 'process';
import _ from 'lodash';
const date = new Date()
interface universalIdentifier {
    id: number
}
export default {
    up: async (queryInterface: QueryInterface) => {
        let languages = await queryInterface.sequelize.query(`select id from languages where code in ('${process.env.DEFAULT_LANGUAGE_CODE}','en') limit 1`, { type: QueryTypes.SELECT }) as universalIdentifier[];
        let user = await queryInterface.sequelize.query(`select id from users order by id ASC limit 0,1`, { type: QueryTypes.SELECT }) as universalIdentifier[];
        if (languages.length && user.length) {
            const contentTypeData = [
                {
                    code: 'permission',
                    user_id: null,
                    accountId: null,
                    status: Constants.CONTENT_TYPE.STATUS.ACTIVE,
                    sort_order: 1,
                    created_at: date,
                    updated_at: date,
                    categoryTypeContent: {
                        name: "Permission",
                        language_id: languages[0].id,
                        description: "Category to manage role permissions",
                        description_text: "Category to manage role permissions"
                    }
                },
                {
                    code: 'setting',
                    user_id: null,
                    accountId: null,
                    status: Constants.CONTENT_TYPE.STATUS.ACTIVE,
                    created_at: date,
                    updated_at: date,
                    sort_order: 2,
                    categoryTypeContent: {
                        name: "Setting",
                        language_id: languages[0].id,
                        description: "Category to manage settings",
                        description_text: "Category to manage settings"
                    }
                },
                {
                    code: 'faq',
                    user_id: null,
                    accountId: null,
                    status: Constants.CONTENT_TYPE.STATUS.ACTIVE,
                    created_at: date,
                    updated_at: date,
                    sort_order: 3,
                    categoryTypeContent: {
                        name: "faq",
                        language_id: languages[0].id,
                        description: "Category to manage faq",
                        description_text: "Category to manage faq"
                    }
                },
                {
                    code: 'tag',
                    user_id: null,
                    accountId: null,
                    status: Constants.CONTENT_TYPE.STATUS.ACTIVE,
                    created_at: date,
                    updated_at: date,
                    sort_order: 4,
                    categoryTypeContent: {
                        name: "Tag",
                        language_id: languages[0].id,
                        description: "Category to manage tag",
                        description_text: "Category to manage tag"
                    }
                },
                {
                    code: 'post',
                    user_id: null,
                    accountId: null,
                    status: Constants.CONTENT_TYPE.STATUS.ACTIVE,
                    created_at: date,
                    updated_at: date,
                    sort_order: 5,
                    categoryTypeContent: {
                        name: "Post",
                        language_id: languages[0].id,
                        description: "Category to manage post",
                        description_text: "Category to manage post"
                    }
                },
                {
                    code: 'rank',
                    user_id: null,
                    accountId: null,
                    status: Constants.CONTENT_TYPE.STATUS.ACTIVE,
                    created_at: date,
                    updated_at: date,
                    sort_order: 6,
                    categoryTypeContent: {
                        name: "Rank",
                        language_id: languages[0].id,
                        description: "Category to manage ranks",
                        description_text: "Category to manage ranks"
                    }
                }
            ];
            let offset = 0;
            let categoryTypes = await queryInterface.sequelize.query(`select id from category_types order by id ASC limit 0,1`, { type: QueryTypes.SELECT }) as universalIdentifier[];
            if (categoryTypes.length == 0) {
                for (let contentType of contentTypeData) {
                    let contentTypeDetails = _.pick(contentType, ['code', 'user_id', 'status', 'sort_order', 'created_at', 'updated_at']);
                    let data = await queryInterface.bulkInsert('category_types', [contentTypeDetails]);
                    let categoryTypeDetails = await queryInterface.sequelize.query(`select id from category_types order by id ASC limit ${offset},1`, { type: QueryTypes.SELECT }) as universalIdentifier[];
                    let content = { ...contentType.categoryTypeContent, ...{ category_type_id: categoryTypeDetails[0].id, created_at: date, updated_at: date } }
                    try {
                        await queryInterface.bulkInsert('category_type_content', [content]);
                    } catch (err) {
                        console.log(err);
                    }
                    offset++;
                }
            } else {
                console.log("category type already initialized")
            }
        } else {
            console.log("unable to created category type")
        }
    },
    down: async (queryInterface: QueryInterface) => {
        await queryInterface.bulkDelete('category_types', { code: [process.env.DEFAULT_LANGUAGE_CODE!, 'en'] }, {})
    },
};
