
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 Source_chunksDBApi {
    

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

        const source_chunks = await db.source_chunks.create(
            {
                id: data.id || undefined,
        
        chunk_index: data.chunk_index
        ||
        null
            ,
            
        chunk_text: data.chunk_text
        ||
        null
            ,
            
        token_count_estimate: data.token_count_estimate
        ||
        null
            ,
            
        embedding_provider: data.embedding_provider
        ||
        null
            ,
            
        embedding_model: data.embedding_model
        ||
        null
            ,
            
        chunk_kind: data.chunk_kind
        ||
        null
            ,
            
            importHash: data.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
    },
        { transaction },
    );

        
        await source_chunks.setSource( data.source || null, {
            transaction,
        });
        
        await source_chunks.setOrganizations( data.organizations || null, {
            transaction,
        });
        

        

        

        return source_chunks;
    }
    

    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 source_chunksData = data.map((item, index) => ({
                id: item.id || undefined,
                
                chunk_index: item.chunk_index
            ||
            null
            ,
            
                chunk_text: item.chunk_text
            ||
            null
            ,
            
                token_count_estimate: item.token_count_estimate
            ||
            null
            ,
            
                embedding_provider: item.embedding_provider
            ||
            null
            ,
            
                embedding_model: item.embedding_model
            ||
            null
            ,
            
                chunk_kind: item.chunk_kind
            ||
            null
            ,
            
            importHash: item.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
            createdAt: new Date(Date.now() + index * 1000),
    }));

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

        // For each item created, replace relation files
        

        return source_chunks;
    }

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

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


        

        const updatePayload = {};
        
        if (data.chunk_index !== undefined) updatePayload.chunk_index = data.chunk_index;
        
        
        if (data.chunk_text !== undefined) updatePayload.chunk_text = data.chunk_text;
        
        
        if (data.token_count_estimate !== undefined) updatePayload.token_count_estimate = data.token_count_estimate;
        
        
        if (data.embedding_provider !== undefined) updatePayload.embedding_provider = data.embedding_provider;
        
        
        if (data.embedding_model !== undefined) updatePayload.embedding_model = data.embedding_model;
        
        
        if (data.chunk_kind !== undefined) updatePayload.chunk_kind = data.chunk_kind;
        
        
        updatePayload.updatedById = currentUser.id;

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

        
        
        if (data.source !== undefined) {
            await source_chunks.setSource(
              
              data.source,
              
              { transaction }
            );
        }
        
        if (data.organizations !== undefined) {
            await source_chunks.setOrganizations(
              
              data.organizations,
              
              { transaction }
            );
        }
        

        
        

        

        return source_chunks;
    }

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

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

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


        return source_chunks;
    }

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

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

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

        await source_chunks.destroy({
            transaction
        });

        return source_chunks;
    }

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

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

        if (!source_chunks) {
            return source_chunks;
        }

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

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        output.source = await source_chunks.getSource({
            transaction
        });
        
        
        output.organizations = await source_chunks.getOrganizations({
            transaction
        });
        
        

        return output;
    }

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

        
        const user = (options && options.currentUser) || null;
        const userOrganizations = (user && user.organizations?.id) || null;
        

        
        if (userOrganizations) {
            if (options?.currentUser?.organizationsId) {
                where.organizationsId = options.currentUser.organizationsId;
            }
        }
        

        offset = currentPage * limit;

        const orderBy = null;

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

    let include = [

      {
        model: db.sources,
        as: 'source',
        
        where: filter.source ? {
          [Op.or]: [
            { id: { [Op.in]: filter.source.split('|').map(term => Utils.uuid(term)) } },
            {
              title: {
                [Op.or]: filter.source.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
              }
            },
          ]
        } : {},
        
      },

      {
        model: db.organizations,
        as: 'organizations',
        
      },



    ];

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

            
            if (filter.chunk_text) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'source_chunks',
                        'chunk_text',
                        filter.chunk_text,
                    ),
                };
            }
            
            if (filter.embedding_provider) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'source_chunks',
                        'embedding_provider',
                        filter.embedding_provider,
                    ),
                };
            }
            
            if (filter.embedding_model) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'source_chunks',
                        'embedding_model',
                        filter.embedding_model,
                    ),
                };
            }
            

            
            

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

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

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

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

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

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

            
            if (filter.chunk_kind) {
                where = {
                    ...where,
                chunk_kind: filter.chunk_kind,
            };
            }
            


      

      
      if (filter.organizations) {
        const listItems = filter.organizations.split('|').map(item => {
          return  Utils.uuid(item)
        });

        where = {
          ...where,
          organizationsId: {[Op.or]: listItems}
        };
      }
      



            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,
                        },
                    };
                }
            }
        }
        

        
        if (globalAccess) {
            delete where.organizationsId;
        }
        

        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.source_chunks.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,  globalAccess, organizationId,) {
        let where = {};
        
        
        if (!globalAccess && organizationId) {
            where.organizationId = organizationId;
        }
        

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

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

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

    
};

