/**
 * PostgreSQL full-text search support for catalog products.
 */

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const up = (pgm) => {
  pgm.sql(`
    CREATE INDEX catalog_products_fts_idx
    ON catalog_products USING gin (
      (
        setweight(to_tsvector('spanish', COALESCE(name, '')), 'A') ||
        setweight(to_tsvector('spanish', COALESCE(description, '')), 'B') ||
        setweight(to_tsvector('spanish', COALESCE(seo_title, '')), 'B') ||
        setweight(to_tsvector('spanish', COALESCE(seo_description, '')), 'C')
      )
    )
  `);
  pgm.sql(`
    CREATE INDEX brands_brands_fts_idx
    ON brands_brands USING gin (
      (
        setweight(to_tsvector('spanish', COALESCE(name, '')), 'A') ||
        setweight(to_tsvector('spanish', COALESCE(slug, '')), 'B') ||
        setweight(to_tsvector('spanish', COALESCE(seo_title, '')), 'B') ||
        setweight(to_tsvector('spanish', COALESCE(seo_description, '')), 'C')
      )
    )
  `);
  pgm.sql(`
    CREATE INDEX categories_categories_fts_idx
    ON categories_categories USING gin (
      (
        setweight(to_tsvector('spanish', COALESCE(name, '')), 'A') ||
        setweight(to_tsvector('spanish', COALESCE(slug, '')), 'B') ||
        setweight(to_tsvector('spanish', COALESCE(seo_title, '')), 'B') ||
        setweight(to_tsvector('spanish', COALESCE(seo_description, '')), 'C')
      )
    )
  `);
};

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const down = (pgm) => {
  pgm.sql('DROP INDEX IF EXISTS categories_categories_fts_idx');
  pgm.sql('DROP INDEX IF EXISTS brands_brands_fts_idx');
  pgm.sql('DROP INDEX IF EXISTS catalog_products_fts_idx');
};
