-- ============================================================
--  Break the Locks — Part 8 (Mental Matrix) Schema
--  Run once in phpMyAdmin on the admin_btlc database.
--
--  ONE self-referencing tree table holds every node:
--    - seed nodes have parent_id IS NULL and a seed_type/seed_ref
--      pointing back to the original record they were spawned from
--    - belief nodes (positive/negative/neutral/origin) have a
--      parent_id and inherit seed_type from their root
-- ============================================================

CREATE TABLE IF NOT EXISTS `user_matrix_nodes` (
  `id`         INT AUTO_INCREMENT PRIMARY KEY,
  `user_id`    INT NOT NULL,
  `parent_id`  INT NULL,
  `seed_type`  VARCHAR(32) NOT NULL DEFAULT 'custom',
   -- purpose | goal | calling | need | desire | lock | habit | custom
  `seed_ref`   VARCHAR(64) NULL,
   -- pointer back to the source row (e.g. lock id, "a0n2" for
   -- alignment 0 / need 2, habit id, etc.). NULL for derived nodes
   -- and custom seeds. Used to avoid re-seeding the same item.
  `tone`       ENUM('seed','positive','negative','neutral','origin')
               NOT NULL DEFAULT 'neutral',
  `text`       VARCHAR(800) NOT NULL,
  `is_origin`  TINYINT(1) NOT NULL DEFAULT 0,
   -- user-marked "this is where the thought came from"
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX `idx_user`        (`user_id`),
  INDEX `idx_user_parent` (`user_id`, `parent_id`),
  INDEX `idx_user_type`   (`user_id`, `seed_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
