module.exports = {
  async up(queryInterface, Sequelize) {
    const transaction = await queryInterface.sequelize.transaction();

    try {
      const tables = await queryInterface.showAllTables({ transaction });
      const tableNames = tables.map((table) => (typeof table === 'object' ? table.tableName : table));

      if (!tableNames.includes('vorta_social_posts')) {
        await queryInterface.createTable(
          'vorta_social_posts',
          {
            id: {
              type: Sequelize.DataTypes.UUID,
              defaultValue: Sequelize.DataTypes.UUIDV4,
              primaryKey: true,
            },
            userId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'users',
                key: 'id',
              },
            },
            organizationsId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'organizations',
                key: 'id',
              },
            },
            user: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: false,
            },
            content: {
              type: Sequelize.DataTypes.TEXT,
              allowNull: false,
            },
            created_at: {
              type: Sequelize.DataTypes.DATE,
              allowNull: false,
              defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
            },
            importHash: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: true,
              unique: true,
            },
          },
          { transaction },
        );
      }

      if (!tableNames.includes('vorta_social_comments')) {
        await queryInterface.createTable(
          'vorta_social_comments',
          {
            id: {
              type: Sequelize.DataTypes.UUID,
              defaultValue: Sequelize.DataTypes.UUIDV4,
              primaryKey: true,
            },
            post_id: {
              type: Sequelize.DataTypes.UUID,
              allowNull: false,
              references: {
                model: 'vorta_social_posts',
                key: 'id',
              },
            },
            userId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'users',
                key: 'id',
              },
            },
            organizationsId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'organizations',
                key: 'id',
              },
            },
            user: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: false,
            },
            comment: {
              type: Sequelize.DataTypes.TEXT,
              allowNull: false,
            },
            parent_id: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: false,
              defaultValue: '0',
            },
            created_at: {
              type: Sequelize.DataTypes.DATE,
              allowNull: false,
              defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
            },
            importHash: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: true,
              unique: true,
            },
          },
          { transaction },
        );
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      console.error('Failed to create VORTA social feed tables:', error);
      throw error;
    }
  },

  async down(queryInterface) {
    const transaction = await queryInterface.sequelize.transaction();

    try {
      const tables = await queryInterface.showAllTables({ transaction });
      const tableNames = tables.map((table) => (typeof table === 'object' ? table.tableName : table));

      if (tableNames.includes('vorta_social_comments')) {
        await queryInterface.dropTable('vorta_social_comments', { transaction });
      }

      if (tableNames.includes('vorta_social_posts')) {
        await queryInterface.dropTable('vorta_social_posts', { transaction });
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      console.error('Failed to drop VORTA social feed tables:', error);
      throw error;
    }
  },
};
