-- ============================================================
-- Migracja 010: Etap 7 - growth (CRM + realizacje)
-- Tartak Dana CMS
-- Kompatybilnosc: MySQL 8.0
-- ============================================================


-- ------------------------------------------------------------
-- CRM-lite: dodatkowe pola obslugi leadow
-- ------------------------------------------------------------
SET @db_name = DATABASE();

SET @sql = (
    SELECT IF(
        EXISTS(
            SELECT 1
            FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = @db_name
              AND TABLE_NAME = 'inquiries'
              AND COLUMN_NAME = 'internal_note'
        ),
        'DO 0',
        'ALTER TABLE `inquiries` ADD COLUMN `internal_note` TEXT NULL AFTER `status`'
    )
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = (
    SELECT IF(
        EXISTS(
            SELECT 1
            FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = @db_name
              AND TABLE_NAME = 'inquiries'
              AND COLUMN_NAME = 'last_status_change_at'
        ),
        'DO 0',
        'ALTER TABLE `inquiries` ADD COLUMN `last_status_change_at` DATETIME NULL AFTER `internal_note`'
    )
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = (
    SELECT IF(
        EXISTS(
            SELECT 1
            FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = @db_name
              AND TABLE_NAME = 'inquiries'
              AND COLUMN_NAME = 'updated_at'
        ),
        'DO 0',
        'ALTER TABLE `inquiries` ADD COLUMN `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `created_at`'
    )
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- Realizacje: osadzona sekcja poradnikowa (bez osobnych wpisow)
-- ------------------------------------------------------------
SET @sql = (
    SELECT IF(
        EXISTS(
            SELECT 1
            FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = @db_name
              AND TABLE_NAME = 'realizations'
              AND COLUMN_NAME = 'tips_html'
        ),
        'DO 0',
        'ALTER TABLE `realizations` ADD COLUMN `tips_html` TEXT NULL AFTER `content_html`'
    )
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
