-- =============================================================
--  PATCH v1.2.0 — New Field Types + Conditional Logic + Description Blocks
-- =============================================================
--  Run this SQL on your database BEFORE uploading the new files.
--  Safe to run multiple times (IF NOT EXISTS / ADD COLUMN IF NOT EXISTS).
--
--  Covers all schema changes in v1.2.0:
--    • fields.options         : JSON options for select/radio/checkbox
--    • fields.conditions      : JSON conditional display rules
--    • form_text_blocks table : Rich-text description blocks per form page
--    • form_text_blocks.color : Color picker value for each block
-- =============================================================

-- 1. Add `options` column to fields (select/radio/checkbox options as JSON)
ALTER TABLE `fields`
    ADD COLUMN IF NOT EXISTS `options` TEXT NULL DEFAULT NULL;

-- 2. Add `conditions` column to fields (conditional display rules as JSON)
ALTER TABLE `fields`
    ADD COLUMN IF NOT EXISTS `conditions` TEXT NULL DEFAULT NULL;

-- 3. Create form_text_blocks table
CREATE TABLE IF NOT EXISTS `form_text_blocks` (
    `id`         INT AUTO_INCREMENT PRIMARY KEY,
    `form_id`    INT NOT NULL,
    `page_id`    INT NOT NULL,
    `title`      VARCHAR(255) DEFAULT '',
    `content`    MEDIUMTEXT,
    `color`      VARCHAR(30) DEFAULT '#0d6efd',
    `sort_order` INT DEFAULT 0,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_form_text_blocks_form` (`form_id`),
    INDEX `idx_form_text_blocks_page` (`page_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 4. Add color column if form_text_blocks already exists (upgrade from earlier v1.2 beta)
ALTER TABLE `form_text_blocks`
    ADD COLUMN IF NOT EXISTS `color` VARCHAR(30) DEFAULT '#0d6efd';

-- =============================================================
--  Done. No data migration needed — existing records unaffected.
-- =============================================================

-- 5. Add col_width column to form_fields (field width in user fill_form view)
ALTER TABLE `form_fields`
    ADD COLUMN IF NOT EXISTS `col_width` INT NOT NULL DEFAULT 6;
