-- ============================================================
--  Break the Locks — Part 10 (Create Boundaries / Life Rules) Schema
--  Run once in phpMyAdmin on the admin_btlc database.
--
--  TWO tables:
--    user_life_rule_suggestions — AI-generated suggestion pairs
--      (positive "I WILL ___" + negative "I WON'T ___" for the same theme)
--    user_life_rules — the rules the user has ADOPTED into their list,
--      either from a suggestion (with style choice) or written from scratch
-- ============================================================

CREATE TABLE IF NOT EXISTS `user_life_rule_suggestions` (
  `id`            INT AUTO_INCREMENT PRIMARY KEY,
  `user_id`       INT NOT NULL,
  `theme`         VARCHAR(300) NOT NULL,
  `positive_text` VARCHAR(800) NOT NULL,
  `negative_text` VARCHAR(800) NOT NULL,
  `sort_order`    INT NOT NULL DEFAULT 0,
  `created_at`    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX `idx_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


CREATE TABLE IF NOT EXISTS `user_life_rules` (
  `id`                   INT AUTO_INCREMENT PRIMARY KEY,
  `user_id`              INT NOT NULL,
  `text`                 VARCHAR(800) NOT NULL,
  `style`                ENUM('positive','negative') NOT NULL DEFAULT 'positive',
  `source`               ENUM('ai','custom') NOT NULL DEFAULT 'custom',
  `source_suggestion_id` INT NULL,
    -- back-ref to user_life_rule_suggestions.id so the UI can un-mark the
    -- corresponding suggestion chip when a rule is removed
  `sort_order`           INT NOT NULL DEFAULT 0,
  `created_at`           TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX `idx_user` (`user_id`),
  INDEX `idx_user_sort` (`user_id`, `sort_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
