defmodule Berrypod.Repo.Migrations.CreateProductsSearch do
  use Ecto.Migration

  def up do
    # Rowid mapping (FTS5 needs integer rowids, products use UUIDs)
    execute """
    CREATE TABLE products_search_map (
      rowid INTEGER PRIMARY KEY AUTOINCREMENT,
      product_id TEXT NOT NULL UNIQUE
    )
    """

    # FTS5 virtual table — stores its own content for simple delete/reindex
    execute """
    CREATE VIRTUAL TABLE products_search USING fts5(
      title,
      category,
      variant_info,
      description,
      tokenize='unicode61 remove_diacritics 2'
    )
    """
  end

  def down do
    execute "DROP TABLE IF EXISTS products_search"
    execute "DROP TABLE IF EXISTS products_search_map"
  end
end
