SET @smsplanet_sender_mode_exists = (
    SELECT COUNT(*)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'smsplanet_integration_settings'
      AND COLUMN_NAME = 'sender_mode'
);
SET @sql = IF(@smsplanet_sender_mode_exists = 0,
    'ALTER TABLE smsplanet_integration_settings ADD COLUMN sender_mode VARCHAR(16) NOT NULL DEFAULT ''text'' AFTER sender',
    'SELECT ''sender_mode already exists''');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @smsplanet_sender_phone_exists = (
    SELECT COUNT(*)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'smsplanet_integration_settings'
      AND COLUMN_NAME = 'sender_phone'
);
SET @sql = IF(@smsplanet_sender_phone_exists = 0,
    'ALTER TABLE smsplanet_integration_settings ADD COLUMN sender_phone VARCHAR(32) NULL AFTER sender_mode',
    'SELECT ''sender_phone already exists''');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS sms_messages (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    direction VARCHAR(16) NOT NULL,
    provider VARCHAR(32) NOT NULL DEFAULT 'smsplanet',
    order_id BIGINT UNSIGNED NULL,
    from_phone VARCHAR(64) NOT NULL,
    from_phone_normalized VARCHAR(32) NOT NULL,
    to_phone VARCHAR(64) NOT NULL,
    to_phone_normalized VARCHAR(32) NOT NULL,
    body TEXT NOT NULL,
    message_id VARCHAR(128) NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'received',
    raw_payload_json JSON NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    KEY sms_messages_order_created_idx (order_id, created_at),
    KEY sms_messages_from_normalized_idx (from_phone_normalized),
    KEY sms_messages_to_normalized_idx (to_phone_normalized),
    KEY sms_messages_provider_message_idx (provider, message_id),
    CONSTRAINT sms_messages_order_fk
        FOREIGN KEY (order_id) REFERENCES orders(id)
        ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT sms_messages_user_fk
        FOREIGN KEY (created_by) REFERENCES users(id)
        ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS notifications (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    type VARCHAR(64) NOT NULL,
    title VARCHAR(190) NOT NULL,
    body VARCHAR(500) NOT NULL,
    target_url VARCHAR(500) NULL,
    related_order_id BIGINT UNSIGNED NULL,
    related_sms_message_id BIGINT UNSIGNED NULL,
    read_at DATETIME NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY notifications_unread_created_idx (read_at, created_at),
    KEY notifications_order_idx (related_order_id),
    KEY notifications_sms_message_idx (related_sms_message_id),
    CONSTRAINT notifications_order_fk
        FOREIGN KEY (related_order_id) REFERENCES orders(id)
        ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT notifications_sms_message_fk
        FOREIGN KEY (related_sms_message_id) REFERENCES sms_messages(id)
        ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
