
const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');



const Sequelize = db.Sequelize;
const Op = Sequelize.Op;

module.exports = class Premium_plansDBApi {
    

    
    static async create(data, options) {
        const currentUser = (options && options.currentUser) || { id: null };
        const transaction = (options && options.transaction) || undefined;

        const premium_plans = await db.premium_plans.create(
            {
                id: data.id || undefined,
        
        name: data.name
        ||
        null
            ,
            
        tier: data.tier
        ||
        null
            ,
            
        price_monthly: data.price_monthly
        ||
        null
            ,
            
        price_yearly: data.price_yearly
        ||
        null
            ,
            
        unlimited_likes: data.unlimited_likes
        ||
        false
        
            ,
            
        unlimited_chats: data.unlimited_chats
        ||
        false
        
            ,
            
        see_who_liked_me: data.see_who_liked_me
        ||
        false
        
            ,
            
        advanced_filters: data.advanced_filters
        ||
        false
        
            ,
            
        passport_mode: data.passport_mode
        ||
        false
        
            ,
            
        boost_profile: data.boost_profile
        ||
        false
        
            ,
            
        read_receipts: data.read_receipts
        ||
        false
        
            ,
            
        incognito_mode: data.incognito_mode
        ||
        false
        
            ,
            
        description: data.description
        ||
        null
            ,
            
            importHash: data.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
    },
        { transaction },
    );

        

        

        

        return premium_plans;
    }
    

    static async bulkImport(data, options) {
        const currentUser = (options && options.currentUser) || { id: null };
        const transaction = (options && options.transaction) || undefined;

        // Prepare data - wrapping individual data transformations in a map() method
        const premium_plansData = data.map((item, index) => ({
                id: item.id || undefined,
                
                name: item.name
            ||
            null
            ,
            
                tier: item.tier
            ||
            null
            ,
            
                price_monthly: item.price_monthly
            ||
            null
            ,
            
                price_yearly: item.price_yearly
            ||
            null
            ,
            
                unlimited_likes: item.unlimited_likes
            ||
            false
        
            ,
            
                unlimited_chats: item.unlimited_chats
            ||
            false
        
            ,
            
                see_who_liked_me: item.see_who_liked_me
            ||
            false
        
            ,
            
                advanced_filters: item.advanced_filters
            ||
            false
        
            ,
            
                passport_mode: item.passport_mode
            ||
            false
        
            ,
            
                boost_profile: item.boost_profile
            ||
            false
        
            ,
            
                read_receipts: item.read_receipts
            ||
            false
        
            ,
            
                incognito_mode: item.incognito_mode
            ||
            false
        
            ,
            
                description: item.description
            ||
            null
            ,
            
            importHash: item.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
            createdAt: new Date(Date.now() + index * 1000),
    }));

        // Bulk create items
        const premium_plans = await db.premium_plans.bulkCreate(premium_plansData, { transaction });

        // For each item created, replace relation files
        

        return premium_plans;
    }

    static async update(id, data,  options) {
        const currentUser = (options && options.currentUser) || {id: null};
        const transaction = (options && options.transaction) || undefined;
        

        const premium_plans = await db.premium_plans.findByPk(id, {}, {transaction});


        

        const updatePayload = {};
        
        if (data.name !== undefined) updatePayload.name = data.name;
        
        
        if (data.tier !== undefined) updatePayload.tier = data.tier;
        
        
        if (data.price_monthly !== undefined) updatePayload.price_monthly = data.price_monthly;
        
        
        if (data.price_yearly !== undefined) updatePayload.price_yearly = data.price_yearly;
        
        
        if (data.unlimited_likes !== undefined) updatePayload.unlimited_likes = data.unlimited_likes;
        
        
        if (data.unlimited_chats !== undefined) updatePayload.unlimited_chats = data.unlimited_chats;
        
        
        if (data.see_who_liked_me !== undefined) updatePayload.see_who_liked_me = data.see_who_liked_me;
        
        
        if (data.advanced_filters !== undefined) updatePayload.advanced_filters = data.advanced_filters;
        
        
        if (data.passport_mode !== undefined) updatePayload.passport_mode = data.passport_mode;
        
        
        if (data.boost_profile !== undefined) updatePayload.boost_profile = data.boost_profile;
        
        
        if (data.read_receipts !== undefined) updatePayload.read_receipts = data.read_receipts;
        
        
        if (data.incognito_mode !== undefined) updatePayload.incognito_mode = data.incognito_mode;
        
        
        if (data.description !== undefined) updatePayload.description = data.description;
        
        
        updatePayload.updatedById = currentUser.id;

        await premium_plans.update(updatePayload, {transaction});

        
        

        
        

        

        return premium_plans;
    }

    static async deleteByIds(ids, options) {
        const currentUser = (options && options.currentUser) || { id: null };
        const transaction = (options && options.transaction) || undefined;

        const premium_plans = await db.premium_plans.findAll({
            where: {
                id: {
                    [Op.in]: ids,
                },
            },
            transaction,
        });

        await db.sequelize.transaction(async (transaction) => {
            for (const record of premium_plans) {
                await record.update(
                    {deletedBy: currentUser.id},
                    {transaction}
                );
            }
            for (const record of premium_plans) {
                await record.destroy({transaction});
            }
        });


        return premium_plans;
    }

    static async remove(id, options) {
        const currentUser = (options && options.currentUser) || {id: null};
        const transaction = (options && options.transaction) || undefined;

        const premium_plans = await db.premium_plans.findByPk(id, options);

        await premium_plans.update({
            deletedBy: currentUser.id
        }, {
            transaction,
        });

        await premium_plans.destroy({
            transaction
        });

        return premium_plans;
    }

    static async findBy(where, options) {
        const transaction = (options && options.transaction) || undefined;

        const premium_plans = await db.premium_plans.findOne(
            { where },
            { transaction },
        );

        if (!premium_plans) {
            return premium_plans;
        }

        const output = premium_plans.get({plain: true});

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        output.subscriptions_plan = await premium_plans.getSubscriptions_plan({
            transaction
        });
        
        
        
        
        
        
        

        return output;
    }

    static async findAll(
          filter,
           options
        ) {
        const limit = filter.limit || 0;
        let offset = 0;
        let where = {};
        const currentPage = +filter.page;

        

        

        offset = currentPage * limit;

        const orderBy = null;

    const transaction = (options && options.transaction) || undefined;

    let include = [



    ];

        if (filter) {
            if (filter.id) {
                where = {
                    ...where,
                    ['id']: Utils.uuid(filter.id),
                };
            }

            
            if (filter.name) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'premium_plans',
                        'name',
                        filter.name,
                    ),
                };
            }
            
            if (filter.description) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'premium_plans',
                        'description',
                        filter.description,
                    ),
                };
            }
            

            
            

            
            if (filter.price_monthlyRange) {
                const [start, end] = filter.price_monthlyRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    price_monthly: {
                    ...where.price_monthly,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    price_monthly: {
                    ...where.price_monthly,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.price_yearlyRange) {
                const [start, end] = filter.price_yearlyRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    price_yearly: {
                    ...where.price_yearly,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    price_yearly: {
                    ...where.price_yearly,
                            [Op.lte]: end,
                    },
                };
                }
            }
            

            if (filter.active !== undefined) {
                where = {
                    ...where,
                    active: filter.active === true || filter.active === 'true'
                };
            }

            
            if (filter.tier) {
                where = {
                    ...where,
                tier: filter.tier,
            };
            }
            
            if (filter.unlimited_likes) {
                where = {
                    ...where,
                unlimited_likes: filter.unlimited_likes,
            };
            }
            
            if (filter.unlimited_chats) {
                where = {
                    ...where,
                unlimited_chats: filter.unlimited_chats,
            };
            }
            
            if (filter.see_who_liked_me) {
                where = {
                    ...where,
                see_who_liked_me: filter.see_who_liked_me,
            };
            }
            
            if (filter.advanced_filters) {
                where = {
                    ...where,
                advanced_filters: filter.advanced_filters,
            };
            }
            
            if (filter.passport_mode) {
                where = {
                    ...where,
                passport_mode: filter.passport_mode,
            };
            }
            
            if (filter.boost_profile) {
                where = {
                    ...where,
                boost_profile: filter.boost_profile,
            };
            }
            
            if (filter.read_receipts) {
                where = {
                    ...where,
                read_receipts: filter.read_receipts,
            };
            }
            
            if (filter.incognito_mode) {
                where = {
                    ...where,
                incognito_mode: filter.incognito_mode,
            };
            }
            




            if (filter.createdAtRange) {
                const [start, end] = filter.createdAtRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                        ['createdAt']: {
                            ...where.createdAt,
                            [Op.gte]: start,
                        },
                    };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                        ['createdAt']: {
                            ...where.createdAt,
                            [Op.lte]: end,
                        },
                    };
                }
            }
        }
        

        

        const queryOptions = {
            where,
            include,
            distinct: true,
            order: filter.field && filter.sort
                ? [[filter.field, filter.sort]]
                : [['createdAt', 'desc']],
            transaction: options?.transaction,
            logging: console.log
        };

        if (!options?.countOnly) {
            queryOptions.limit = limit ? Number(limit) : undefined;
            queryOptions.offset = offset ? Number(offset) : undefined;
        }

        try {
            const { rows, count } = await db.premium_plans.findAndCountAll(queryOptions);

            return {
                rows: options?.countOnly ? [] : rows,
                count: count
            };
        } catch (error) {
            console.error('Error executing query:', error);
            throw error;
        }
    }

    static async findAllAutocomplete(query, limit, offset, ) {
        let where = {};
        
        

        if (query) {
            where = {
                [Op.or]: [
                    { ['id']: Utils.uuid(query) },
                    Utils.ilike(
                        'premium_plans',
                        'name',
                        query,
                    ),
                ],
            };
        }

        const records = await db.premium_plans.findAll({
            attributes: [ 'id', 'name' ],
            where,
            limit: limit ? Number(limit) : undefined,
            offset: offset ? Number(offset) : undefined,
            orderBy: [['name', 'ASC']],
        });

        return records.map((record) => ({
            id: record.id,
            label: record.name,
        }));
    }

    
};

