import { MigrationInterface, QueryRunner } from 'typeorm';

export class addGenres1710922390801 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            INSERT INTO public.assets (id, workspace_id, parent_ids, project_id, name, title, icon, scope_id, is_abstract, created_at, updated_at, deleted_at, creator_user_id) VALUES ('00000000-0000-0000-0000-100000000004', '00000000-0000-0000-0000-000000000001', '{}', 1, null, '[[t:Genres]]', null, 3, false, '2023-12-03 19:41:47.077038 +00:00', '2023-12-03 23:32:09.501950 +00:00', null, null);
        `);
    await queryRunner.query(
      `
            INSERT INTO public.asset_blocks (project_id, block_key, asset_id, title, props, created_at, updated_at, index, has_formula) VALUES (1, 1, '00000000-0000-0000-0000-100000000004', '[[t:Info]]', 
            $1, 
            '2023-12-03 22:44:50.452839 +00:00', '2023-12-03 22:48:37.892137 +00:00', 1, false);
        `,
      [
        JSON.stringify({
          '\\items': [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
            19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
            36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
            53, 54, 55, 56, 57,
          ],
          '\\items\\0': null,
          '\\items\\1': null,
          '\\items\\2': null,
          '\\items\\3': null,
          '\\items\\4': null,
          '\\items\\5': null,
          '\\items\\6': null,
          '\\items\\7': null,
          '\\items\\8': null,
          '\\items\\9': null,
          '\\items\\10': null,
          '\\items\\11': null,
          '\\items\\12': null,
          '\\items\\13': null,
          '\\items\\14': null,
          '\\items\\15': null,
          '\\items\\16': null,
          '\\items\\17': null,
          '\\items\\18': null,
          '\\items\\19': null,
          '\\items\\20': null,
          '\\items\\21': null,
          '\\items\\22': null,
          '\\items\\23': null,
          '\\items\\24': null,
          '\\items\\25': null,
          '\\items\\26': null,
          '\\items\\27': null,
          '\\items\\28': null,
          '\\items\\29': null,
          '\\items\\30': null,
          '\\items\\31': null,
          '\\items\\32': null,
          '\\items\\33': null,
          '\\items\\34': null,
          '\\items\\35': null,
          '\\items\\36': null,
          '\\items\\37': null,
          '\\items\\38': null,
          '\\items\\39': null,
          '\\items\\40': null,
          '\\items\\41': null,
          '\\items\\42': null,
          '\\items\\43': null,
          '\\items\\44': null,
          '\\items\\45': null,
          '\\items\\46': null,
          '\\items\\47': null,
          '\\items\\48': null,
          '\\items\\49': null,
          '\\items\\50': null,
          '\\items\\51': null,
          '\\items\\52': null,
          '\\items\\53': null,
          '\\items\\54': null,
          '\\items\\55': null,
          '\\items\\56': null,
          '\\items\\57': null,
          '\\items\\0\\name': 'action',
          '\\items\\1\\name': 'action_rpg',
          '\\items\\2\\name': 'adventure',
          '\\items\\3\\name': 'adventure_rpg',
          '\\items\\4\\name': 'all_sports',
          '\\items\\5\\name': 'arcade_rhythm',
          '\\items\\6\\name': 'building_automation',
          '\\items\\7\\name': 'card_board',
          '\\items\\8\\name': 'casino',
          '\\items\\9\\name': 'casual',
          '\\items\\0\\title': '[[t:GenreAction]]',
          '\\items\\10\\name': 'chess',
          '\\items\\11\\name': 'children_games',
          '\\items\\12\\name': 'city_settlement',
          '\\items\\13\\name': 'dating',
          '\\items\\14\\name': 'farming_crafting',
          '\\items\\15\\name': 'fighting_martial_arts',
          '\\items\\16\\name': 'first_person_shooter',
          '\\items\\17\\name': 'fishing_hunting',
          '\\items\\18\\name': 'grand',
          '\\items\\19\\name': 'hack_slash',
          '\\items\\1\\title': '[[t:GenreActionRPG]]',
          '\\items\\20\\name': 'hidden_object',
          '\\items\\21\\name': 'hobby_job',
          '\\items\\22\\name': 'horror',
          '\\items\\23\\name': 'individual_sports',
          '\\items\\24\\name': 'jrpg',
          '\\items\\25\\name': 'life_immersive',
          '\\items\\26\\name': 'logic',
          '\\items\\27\\name': 'metroidvania',
          '\\items\\28\\name': 'military',
          '\\items\\29\\name': 'mmog',
          '\\items\\2\\title': '[[t:GenreAdventure]]',
          '\\items\\30\\name': 'moba',
          '\\items\\31\\name': 'party_based',
          '\\items\\32\\name': 'platformer_runner',
          '\\items\\33\\name': 'puzzle',
          '\\items\\34\\name': 'quests',
          '\\items\\35\\name': 'racing',
          '\\items\\36\\name': 'racing_sim',
          '\\items\\37\\name': 'real_time_strategy',
          '\\items\\38\\name': 'rogue_like',
          '\\items\\39\\name': 'role_playing',
          '\\items\\3\\title': '[[t:GenreAdventureRPG]]',
          '\\items\\40\\name': 'sandbox_physics',
          '\\items\\41\\name': 'shmup',
          '\\items\\42\\name': 'simulation',
          '\\items\\43\\name': 'simulators',
          '\\items\\44\\name': 'space_flight',
          '\\items\\45\\name': 'sports_racing',
          '\\items\\46\\name': 'sports_sim',
          '\\items\\47\\name': 'story_rich',
          '\\items\\48\\name': 'strategy',
          '\\items\\49\\name': 'strategy_rpg',
          '\\items\\4\\title': '[[t:GenreAllSports]]',
          '\\items\\50\\name': 'survival',
          '\\items\\51\\name': 'tabletop',
          '\\items\\52\\name': 'team_sports',
          '\\items\\53\\name': 'third_person_shooter',
          '\\items\\54\\name': 'tower_defense',
          '\\items\\55\\name': 'turn_based',
          '\\items\\56\\name': 'turn_based_strategy',
          '\\items\\57\\name': 'visual_novel',
          '\\items\\5\\title': '[[t:GenreArcadeRhythm]]',
          '\\items\\6\\title': '[[t:GenreBuildingAutomation]]',
          '\\items\\7\\title': '[[t:GenreCardBoard]]',
          '\\items\\8\\title': '[[t:GenreCasino]]',
          '\\items\\9\\title': '[[t:GenreCasual]]',
          '\\items\\10\\title': '[[t:GenreChess]]',
          '\\items\\11\\title': '[[t:GenreChildrenGames]]',
          '\\items\\12\\title': '[[t:GenreCitySettlement]]',
          '\\items\\13\\title': '[[t:GenreDating]]',
          '\\items\\14\\title': '[[t:GenreFarmingCrafting]]',
          '\\items\\15\\title': '[[t:GenreFightingMartialArts]]',
          '\\items\\16\\title': '[[t:GenreFirstPersonShooter]]',
          '\\items\\17\\title': '[[t:GenreFishingHunting]]',
          '\\items\\18\\title': '[[t:GenreGrand]]',
          '\\items\\19\\title': '[[t:GenreHackSlash]]',
          '\\items\\20\\title': '[[t:GenreHiddenObject]]',
          '\\items\\21\\title': '[[t:GenreHobbyJob]]',
          '\\items\\22\\title': '[[t:GenreHorror]]',
          '\\items\\23\\title': '[[t:GenreIndividualSports]]',
          '\\items\\24\\title': '[[t:GenreJRPG]]',
          '\\items\\25\\title': '[[t:GenreLifeImmersive]]',
          '\\items\\26\\title': '[[t:GenreLogic]]',
          '\\items\\27\\title': '[[t:GenreMetroidvania]]',
          '\\items\\28\\title': '[[t:GenreMilitary]]',
          '\\items\\29\\title': '[[t:GenreMMOG]]',
          '\\items\\30\\title': '[[t:GenreMOBA]]',
          '\\items\\31\\title': '[[t:GenrePartyBased]]',
          '\\items\\32\\title': '[[t:GenrePlatformerRunner]]',
          '\\items\\33\\title': '[[t:GenrePuzzle]]',
          '\\items\\34\\title': '[[t:GenreQuests]]',
          '\\items\\35\\title': '[[t:GenreRacing]]',
          '\\items\\36\\title': '[[t:GenreRacingSim]]',
          '\\items\\37\\title': '[[t:GenreRealTimeStrategy]]',
          '\\items\\38\\title': '[[t:GenreRogueLike]]',
          '\\items\\39\\title': '[[t:GenreRolePlaying]]',
          '\\items\\40\\title': '[[t:GenreSandboxPhysics]]',
          '\\items\\41\\title': '[[t:GenreShmup]]',
          '\\items\\42\\title': '[[t:GenreSimulation]]',
          '\\items\\43\\title': '[[t:GenreSimulators]]',
          '\\items\\44\\title': '[[t:GenreSpaceFlight]]',
          '\\items\\45\\title': '[[t:GenreSportsRacing]]',
          '\\items\\46\\title': '[[t:GenreSportsSim]]',
          '\\items\\47\\title': '[[t:GenreStoryRich]]',
          '\\items\\48\\title': '[[t:GenreStrategy]]',
          '\\items\\49\\title': '[[t:GenreStrategyRPG]]',
          '\\items\\50\\title': '[[t:GenreSurvival]]',
          '\\items\\51\\title': '[[t:GenreTabletop]]',
          '\\items\\52\\title': '[[t:GenreTeamSports]]',
          '\\items\\53\\title': '[[t:GenreThirdPersonShooter]]',
          '\\items\\54\\title': '[[t:GenreTowerDefense]]',
          '\\items\\55\\title': '[[t:GenreTurnBased]]',
          '\\items\\56\\title': '[[t:GenreTurnBasedStrategy]]',
          '\\items\\57\\title': '[[t:GenreVisualNovel]]',
        }),
      ],
    );

    await queryRunner.query(
      `
                          UPDATE asset_blocks
                          SET props = $1
                          WHERE asset_id = $2 AND project_id = 1 AND block_key = 1
                      `,
      [
        JSON.stringify({
          '\\genre': [],
          '\\developer': null,
          '\\coverimage': null,
          'index\\genre': 1,
          'title\\genre': '[[t:Genre]]',
          '\\releasedate': null,
          '\\description': null,
          'index\\developer': 2,
          'title\\developer': '[[t:Developer]]',
          'field\\genre\\type': 'enum',
          'index\\coverimage': 4,
          'title\\coverimage': '[[t:CoverImage]]',
          'index\\releasedate': 3,
          'title\\releasedate': '[[t:ReleaseDate]]',
          'index\\description': 5,
          'title\\description': '[[t:Description]]',
          'field\\genre\\multiple': true,
          'field\\coverimage\\hint': '[[t:AttributeCoverImageHint]]',
          'field\\coverimage\\type': 'attachment',
          'field\\releasedate\\type': 'date',
          'field\\genre\\params\\type': {
            name: null,
            title: 'Genres',
            assetId: '00000000-0000-0000-0000-100000000004',
          },
          'field\\description\\type': 'string',
        }),
        '00000000-0000-0000-0000-100000000000',
      ],
    );
    await queryRunner.query(
      `
                          UPDATE asset_block_comps
                          SET computed_at = NULL
                          WHERE asset_id = '00000000-0000-0000-0000-100000000000' AND project_id = 1 AND block_key = 1
                      `,
    );

    await queryRunner.query(`
            INSERT INTO asset_comps(project_id, id)
            SELECT project_id, id
            FROM assets
            ON CONFLICT DO NOTHING;
            INSERT INTO asset_block_comps(project_id, asset_id, block_key)
            SELECT project_id, asset_id, block_key
            FROM asset_blocks
            ON CONFLICT DO NOTHING;
        `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `
                          UPDATE asset_blocks
                          SET props = $1
                          WHERE asset_id = $2 AND project_id = 1 AND block_key = 1
                      `,
      [
        JSON.stringify({
          '\\genre': null,
          '\\developer': null,
          '\\coverimage': null,
          'index\\genre': 1,
          'title\\genre': '[[t:Genre]]',
          '\\releasedate': null,
          'index\\developer': 2,
          'title\\developer': '[[t:Developer]]',
          'index\\coverimage': 4,
          'title\\coverimage': '[[t:CoverImage]]',
          'index\\releasedate': 3,
          'title\\releasedate': '[[t:ReleaseDate]]',
          'field\\coverimage\\type': 'attachment',
          'field\\coverimage\\hint': '[[t:AttributeCoverImageHint]]',
        }),
        '00000000-0000-0000-0000-100000000000',
      ],
    );
    await queryRunner.query(
      `
                          UPDATE asset_block_comps
                          SET computed_at = NULL
                          WHERE asset_id = '00000000-0000-0000-0000-100000000000' AND project_id = 1 AND block_key = 1
                      `,
    );

    await queryRunner.query(`
              DELETE FROM public.assets 
              WHERE id = '00000000-0000-0000-0000-100000000004'
          `);
  }
}
