/**
 * FIX-19: categories parent/child type.
 * Adds is_parent flag (container category). A CHILD is a leaf and cannot
 * contain children; only PARENT categories can contain other categories.
 */

/** @param {import('node-pg-migrate'). MigrationBuilder} pgm */
export const up = (pgm) => {
  pgm.sql(`ALTER TABLE categories_categories ADD COLUMN is_parent boolean NOT NULL DEFAULT false`);
  // Migration rule: categories that already have children become parents.
  pgm.sql(`
    UPDATE categories_categories c
    SET is_parent = true
    WHERE EXISTS (
      SELECT 1 FROM categories_categories ch WHERE ch.parent_id = c.id
    )
  `);
};

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const down = (pgm) => {
  pgm.sql('ALTER TABLE categories_categories DROP COLUMN IF EXISTS is_parent');
};
