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

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

        const media_items = await db.media_items.create(
            {
                id: data.id || undefined,
        
        media_type: data.media_type
        ||
        null
            ,
            
        title: data.title
        ||
        null
            ,
            
        caption: data.caption
        ||
        null
            ,
            
        captured_at: data.captured_at
        ||
        null
            ,
            
        uploaded_at: data.uploaded_at
        ||
        null
            ,
            
        width: data.width
        ||
        null
            ,
            
        height: data.height
        ||
        null
            ,
            
        duration_seconds: data.duration_seconds
        ||
        null
            ,
            
        camera_make: data.camera_make
        ||
        null
            ,
            
        camera_model: data.camera_model
        ||
        null
            ,
            
        gps_latitude: data.gps_latitude
        ||
        null
            ,
            
        gps_longitude: data.gps_longitude
        ||
        null
            ,
            
        visibility: data.visibility
        ||
        null
            ,
            
        is_duplicate: data.is_duplicate
        ||
        false
        
            ,
            
        duplicate_group_key: data.duplicate_group_key
        ||
        null
            ,
            
            importHash: data.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
    },
        { transaction },
    );

        
        await media_items.setAlbum( data.album || null, {
            transaction,
        });
        
        await media_items.setUploader( data.uploader || null, {
            transaction,
        });
        
        await media_items.setStorage_object( data.storage_object || null, {
            transaction,
        });
        
        await media_items.setOrganizations( data.organizations || null, {
            transaction,
        });
        

        
        await media_items.setAi_tags(data.ai_tags || [], {
            transaction,
        });
        
        await media_items.setUser_tags(data.user_tags || [], {
            transaction,
        });
        
        await media_items.setLikes(data.likes || [], {
            transaction,
        });
        
        await media_items.setComments(data.comments || [], {
            transaction,
        });
        
        await media_items.setSaves(data.saves || [], {
            transaction,
        });
        
        await media_items.setMentions(data.mentions || [], {
            transaction,
        });
        
        await media_items.setFace_detections(data.face_detections || [], {
            transaction,
        });
        
        await media_items.setWatermarked_downloads(data.watermarked_downloads || [], {
            transaction,
        });
        

        

        return media_items;
    }
    

    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 media_itemsData = data.map((item, index) => ({
                id: item.id || undefined,
                
                media_type: item.media_type
            ||
            null
            ,
            
                title: item.title
            ||
            null
            ,
            
                caption: item.caption
            ||
            null
            ,
            
                captured_at: item.captured_at
            ||
            null
            ,
            
                uploaded_at: item.uploaded_at
            ||
            null
            ,
            
                width: item.width
            ||
            null
            ,
            
                height: item.height
            ||
            null
            ,
            
                duration_seconds: item.duration_seconds
            ||
            null
            ,
            
                camera_make: item.camera_make
            ||
            null
            ,
            
                camera_model: item.camera_model
            ||
            null
            ,
            
                gps_latitude: item.gps_latitude
            ||
            null
            ,
            
                gps_longitude: item.gps_longitude
            ||
            null
            ,
            
                visibility: item.visibility
            ||
            null
            ,
            
                is_duplicate: item.is_duplicate
            ||
            false
        
            ,
            
                duplicate_group_key: item.duplicate_group_key
            ||
            null
            ,
            
            importHash: item.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
            createdAt: new Date(Date.now() + index * 1000),
    }));

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

        // For each item created, replace relation files
        

        return media_items;
    }

    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 media_items = await db.media_items.findByPk(id, {}, {transaction});


        

        const updatePayload = {};
        
        if (data.media_type !== undefined) updatePayload.media_type = data.media_type;
        
        
        if (data.title !== undefined) updatePayload.title = data.title;
        
        
        if (data.caption !== undefined) updatePayload.caption = data.caption;
        
        
        if (data.captured_at !== undefined) updatePayload.captured_at = data.captured_at;
        
        
        if (data.uploaded_at !== undefined) updatePayload.uploaded_at = data.uploaded_at;
        
        
        if (data.width !== undefined) updatePayload.width = data.width;
        
        
        if (data.height !== undefined) updatePayload.height = data.height;
        
        
        if (data.duration_seconds !== undefined) updatePayload.duration_seconds = data.duration_seconds;
        
        
        if (data.camera_make !== undefined) updatePayload.camera_make = data.camera_make;
        
        
        if (data.camera_model !== undefined) updatePayload.camera_model = data.camera_model;
        
        
        if (data.gps_latitude !== undefined) updatePayload.gps_latitude = data.gps_latitude;
        
        
        if (data.gps_longitude !== undefined) updatePayload.gps_longitude = data.gps_longitude;
        
        
        if (data.visibility !== undefined) updatePayload.visibility = data.visibility;
        
        
        if (data.is_duplicate !== undefined) updatePayload.is_duplicate = data.is_duplicate;
        
        
        if (data.duplicate_group_key !== undefined) updatePayload.duplicate_group_key = data.duplicate_group_key;
        
        
        updatePayload.updatedById = currentUser.id;

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

        
        
        if (data.album !== undefined) {
            await media_items.setAlbum(
              
              data.album,
              
              { transaction }
            );
        }
        
        if (data.uploader !== undefined) {
            await media_items.setUploader(
              
              data.uploader,
              
              { transaction }
            );
        }
        
        if (data.storage_object !== undefined) {
            await media_items.setStorage_object(
              
              data.storage_object,
              
              { transaction }
            );
        }
        
        if (data.organizations !== undefined) {
            await media_items.setOrganizations(
              
              data.organizations,
              
              { transaction }
            );
        }
        

        
        
        if (data.ai_tags !== undefined) {
            await media_items.setAi_tags(data.ai_tags, { transaction });
        }
        
        if (data.user_tags !== undefined) {
            await media_items.setUser_tags(data.user_tags, { transaction });
        }
        
        if (data.likes !== undefined) {
            await media_items.setLikes(data.likes, { transaction });
        }
        
        if (data.comments !== undefined) {
            await media_items.setComments(data.comments, { transaction });
        }
        
        if (data.saves !== undefined) {
            await media_items.setSaves(data.saves, { transaction });
        }
        
        if (data.mentions !== undefined) {
            await media_items.setMentions(data.mentions, { transaction });
        }
        
        if (data.face_detections !== undefined) {
            await media_items.setFace_detections(data.face_detections, { transaction });
        }
        
        if (data.watermarked_downloads !== undefined) {
            await media_items.setWatermarked_downloads(data.watermarked_downloads, { transaction });
        }
        

        

        return media_items;
    }

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

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

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


        return media_items;
    }

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

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

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

        await media_items.destroy({
            transaction
        });

        return media_items;
    }

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

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

        if (!media_items) {
            return media_items;
        }

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

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        output.upload_files_media_item = await media_items.getUpload_files_media_item({
            transaction
        });
        
        
        
        output.media_ai_tags_media_item = await media_items.getMedia_ai_tags_media_item({
            transaction
        });
        
        
        output.media_user_tags_media_item = await media_items.getMedia_user_tags_media_item({
            transaction
        });
        
        
        output.likes_media_item = await media_items.getLikes_media_item({
            transaction
        });
        
        
        output.favourites_media_item = await media_items.getFavourites_media_item({
            transaction
        });
        
        
        output.comments_media_item = await media_items.getComments_media_item({
            transaction
        });
        
        
        output.mentions_media_item = await media_items.getMentions_media_item({
            transaction
        });
        
        
        output.notifications_media_item = await media_items.getNotifications_media_item({
            transaction
        });
        
        
        
        
        output.download_jobs_media_item = await media_items.getDownload_jobs_media_item({
            transaction
        });
        
        
        
        
        output.ai_embeddings_media_item = await media_items.getAi_embeddings_media_item({
            transaction
        });
        
        
        output.face_detections_media_item = await media_items.getFace_detections_media_item({
            transaction
        });
        
        
        
        
        output.ai_captions_media_item = await media_items.getAi_captions_media_item({
            transaction
        });
        
        
        output.moderation_flags_media_item = await media_items.getModeration_flags_media_item({
            transaction
        });
        
        
        
        output.story_items_media_item = await media_items.getStory_items_media_item({
            transaction
        });
        
        
        output.analytics_events_media_item = await media_items.getAnalytics_events_media_item({
            transaction
        });
        
        
        
        
        output.album = await media_items.getAlbum({
            transaction
        });
        
        
        output.uploader = await media_items.getUploader({
            transaction
        });
        
        
        output.storage_object = await media_items.getStorage_object({
            transaction
        });
        
        
        output.ai_tags = await media_items.getAi_tags({
            transaction
        });
        
        
        output.user_tags = await media_items.getUser_tags({
            transaction
        });
        
        
        output.likes = await media_items.getLikes({
            transaction
        });
        
        
        output.comments = await media_items.getComments({
            transaction
        });
        
        
        output.saves = await media_items.getSaves({
            transaction
        });
        
        
        output.mentions = await media_items.getMentions({
            transaction
        });
        
        
        output.face_detections = await media_items.getFace_detections({
            transaction
        });
        
        
        output.watermarked_downloads = await media_items.getWatermarked_downloads({
            transaction
        });
        
        
        output.organizations = await media_items.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.albums,
        as: 'album',
        
        where: filter.album ? {
          [Op.or]: [
            { id: { [Op.in]: filter.album.split('|').map(term => Utils.uuid(term)) } },
            {
              title: {
                [Op.or]: filter.album.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
              }
            },
          ]
        } : {},
        
      },

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

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

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


      {
        model: db.media_ai_tags,
        as: 'ai_tags',
        required: false,
      },

      {
        model: db.media_user_tags,
        as: 'user_tags',
        required: false,
      },

      {
        model: db.likes,
        as: 'likes',
        required: false,
      },

      {
        model: db.comments,
        as: 'comments',
        required: false,
      },

      {
        model: db.favourites,
        as: 'saves',
        required: false,
      },

      {
        model: db.mentions,
        as: 'mentions',
        required: false,
      },

      {
        model: db.face_detections,
        as: 'face_detections',
        required: false,
      },

      {
        model: db.download_jobs,
        as: 'watermarked_downloads',
        required: false,
      },


    ];

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

            
            if (filter.title) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'media_items',
                        'title',
                        filter.title,
                    ),
                };
            }
            
            if (filter.caption) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'media_items',
                        'caption',
                        filter.caption,
                    ),
                };
            }
            
            if (filter.camera_make) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'media_items',
                        'camera_make',
                        filter.camera_make,
                    ),
                };
            }
            
            if (filter.camera_model) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'media_items',
                        'camera_model',
                        filter.camera_model,
                    ),
                };
            }
            
            if (filter.duplicate_group_key) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'media_items',
                        'duplicate_group_key',
                        filter.duplicate_group_key,
                    ),
                };
            }
            

            
            

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
            if (filter.media_type) {
                where = {
                    ...where,
                media_type: filter.media_type,
            };
            }
            
            if (filter.visibility) {
                where = {
                    ...where,
                visibility: filter.visibility,
            };
            }
            
            if (filter.is_duplicate) {
                where = {
                    ...where,
                is_duplicate: filter.is_duplicate,
            };
            }
            


      

      

      

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

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


      if (filter.ai_tags) {
        const searchTerms = filter.ai_tags.split('|');

        include = [
          {
            model: db.media_ai_tags,
            as: 'ai_tags_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  source: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }

      if (filter.user_tags) {
        const searchTerms = filter.user_tags.split('|');

        include = [
          {
            model: db.media_user_tags,
            as: 'user_tags_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  tagged_by: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }

      if (filter.likes) {
        const searchTerms = filter.likes.split('|');

        include = [
          {
            model: db.likes,
            as: 'likes_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  liked_at: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }

      if (filter.comments) {
        const searchTerms = filter.comments.split('|');

        include = [
          {
            model: db.comments,
            as: 'comments_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  content: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }

      if (filter.saves) {
        const searchTerms = filter.saves.split('|');

        include = [
          {
            model: db.favourites,
            as: 'saves_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  saved_at: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }

      if (filter.mentions) {
        const searchTerms = filter.mentions.split('|');

        include = [
          {
            model: db.mentions,
            as: 'mentions_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  mentioned_user: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }

      if (filter.face_detections) {
        const searchTerms = filter.face_detections.split('|');

        include = [
          {
            model: db.face_detections,
            as: 'face_detections_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  confidence: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }

      if (filter.watermarked_downloads) {
        const searchTerms = filter.watermarked_downloads.split('|');

        include = [
          {
            model: db.download_jobs,
            as: 'watermarked_downloads_filter',
            required: searchTerms.length > 0,
            where: searchTerms.length > 0 ? {
              [Op.or]: [
                { id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
                { 
                  status: {
                    [Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
                  }
                }
              ]
            } : undefined
          },
          ...include,
        ]
      }


            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.media_items.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(
                        'media_items',
                        'title',
                        query,
                    ),
                ],
            };
        }

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

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

    
};

