"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.down = down;
exports.up = up;

var _slug = require("../slug");

async function up(knex) {
  await knex.schema.alterTable('image', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
  }).alterTable('notificationsHistory', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.dropForeign('userId');
  }).alterTable('accessToken', table => {
    table.dropForeign('userId');
  }).alterTable('seen', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('userRating', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('watchlist', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('userId');
  }).alterTable('listItem', table => {
    table.dropForeign('episodeId');
    table.dropForeign('seasonId');
    table.dropForeign('mediaItemId');
    table.dropForeign('listId');
  }).alterTable('list', table => {
    table.dropForeign('userId');
  }).alterTable('episode', table => {
    table.dropForeign('tvShowId');
    table.dropForeign('seasonId');
  }).alterTable('season', table => {
    table.dropForeign('tvShowId');
  });
  await knex.schema.alterTable('list', table => {
    table.boolean('isWatchlist');
    table.index('isWatchlist');
  });
  await knex('list').update('isWatchlist', false);
  await knex('list').update('rank', knex.raw(`rank + 1`));
  await knex('list').update('slug', `watchlist-${(0, _slug.randomSlugId)()}`).where('slug', 'watchlist');
  await knex.schema.alterTable('list', table => {
    table.boolean('isWatchlist').notNullable().alter({
      alterNullable: true
    });
  });
  const users = await knex('user');

  for (const user of users) {
    const createdAt = new Date().getTime();
    const [watchlist] = await knex('list').insert({
      name: 'Watchlist',
      slug: 'watchlist',
      userId: user.id,
      privacy: 'private',
      allowComments: false,
      displayNumbers: false,
      createdAt: createdAt,
      updatedAt: createdAt,
      sortBy: 'recently-aired',
      sortOrder: 'desc',
      isWatchlist: true,
      rank: 0
    }).returning('*');
    const watchlistItems = await knex('watchlist').where('userId', user.id);

    if (watchlistItems.length > 0) {
      await knex.batchInsert('listItem', watchlistItems.map((item, index) => ({
        listId: watchlist.id,
        mediaItemId: item.mediaItemId,
        addedAt: item.addedAt || new Date().getTime(),
        rank: index
      })), 30);
    }
  }

  await knex.schema.dropTable('watchlist');
  await knex.schema.alterTable('season', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
  }).alterTable('episode', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('image', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('notificationsHistory', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('episodeId').references('id').inTable('episode');
  }).alterTable('list', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('listItem', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('listId').references('id').inTable('list');
  }).alterTable('userRating', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('seen', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('accessToken', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.foreign('userId').references('id').inTable('user');
  });
}

async function down(knex) {
  await knex.schema.alterTable('image', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
  }).alterTable('notificationsHistory', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.dropForeign('userId');
  }).alterTable('accessToken', table => {
    table.dropForeign('userId');
  }).alterTable('seen', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('userRating', table => {
    table.dropForeign('mediaItemId');
    table.dropForeign('seasonId');
    table.dropForeign('episodeId');
    table.dropForeign('userId');
  }).alterTable('listItem', table => {
    table.dropForeign('episodeId');
    table.dropForeign('seasonId');
    table.dropForeign('mediaItemId');
    table.dropForeign('listId');
  }).alterTable('list', table => {
    table.dropForeign('userId');
  }).alterTable('episode', table => {
    table.dropForeign('tvShowId');
    table.dropForeign('seasonId');
  }).alterTable('season', table => {
    table.dropForeign('tvShowId');
  });
  await knex.schema.createTable('watchlist', table => {
    table.increments('id').primary();
    table.integer('userId').notNullable();
    table.integer('mediaItemId').notNullable();
    table.index('userId');
    table.index('mediaItemId');
    table.double('addedAt');
  });
  const users = await knex('user');

  for (const user of users) {
    const watchlist = await knex('list').where('isWatchlist', true).where('userId', user.id).first();

    if (watchlist) {
      const watchlistItems = await knex('listItem').where('listId', watchlist.id);

      if (watchlistItems.length > 0) {
        await knex.batchInsert('watchlist', watchlistItems.map(item => ({
          mediaItemId: item.mediaItemId,
          addedAt: item.addedAt || new Date().getTime(),
          userId: user.id
        })), 30);
        await knex('listItem').delete().where('listId', watchlist.id);
      }

      await knex('list').delete().where('id', watchlist.id);
    }
  }

  await knex('list').update('rank', knex.raw(`rank - 1`));
  await knex.schema.alterTable('list', table => {
    table.dropIndex('isWatchlist');
    table.dropColumn('isWatchlist');
  });
  await knex.schema.alterTable('season', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
  }).alterTable('episode', table => {
    table.foreign('tvShowId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('image', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('seasonId').references('id').inTable('season');
  }).alterTable('notificationsHistory', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('episodeId').references('id').inTable('episode');
  }).alterTable('list', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('listItem', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('listId').references('id').inTable('list');
  }).alterTable('watchlist', table => {
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('userRating', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('seasonId').references('id').inTable('season');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('seen', table => {
    table.foreign('episodeId').references('id').inTable('episode');
    table.foreign('mediaItemId').references('id').inTable('mediaItem');
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('accessToken', table => {
    table.foreign('userId').references('id').inTable('user');
  }).alterTable('notificationPlatformsCredentials', table => {
    table.foreign('userId').references('id').inTable('user');
  });
}
//# sourceMappingURL=20220427212000_watchlistToList.js.map