import { MigrationInterface, QueryRunner } from 'typeorm';

export class AllCategorySeeder1782711296557 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    const existingAllCategory = await queryRunner.query(`
      SELECT category_id
      FROM category_translations
      WHERE slug = 'all'
        AND language_code = 'en'
        AND deleted_at IS NULL
      LIMIT 1
    `);

    if (existingAllCategory.length) {
      return;
    }

    const allCategory = await queryRunner.query(`
      INSERT INTO categories (
        is_active,
        is_recommended,
        is_hidden
      )
      VALUES (true, true, true)
      RETURNING id
    `);

    await queryRunner.query(`
      INSERT INTO category_translations (
        category_id,
        language_code,
        title,
        display_title,
        slug
      )
      VALUES (
        '${allCategory[0].id}',
        'en',
        'All',
        'All',
        'all'
      )
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      DELETE FROM categories
      WHERE id IN (
        SELECT category_id
        FROM category_translations
        WHERE slug = 'all'
          AND language_code = 'en'
      )
    `);
  }
}
