-- Migration 0012: create_settings_and_integrations
-- Auto-generated from database/schema.sql by database/build_migrations.php
-- Do not hand-edit this file — edit schema.sql and re-run that script instead.

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- =====================================================================
-- SECTION 11 — SETTINGS & INTEGRATIONS
-- =====================================================================

-- Simple, extensible key-value workspace settings (General tab, plus
-- anything future modules need without a schema change).
CREATE TABLE IF NOT EXISTS settings (
    id           BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    setting_key  VARCHAR(120)    NOT NULL,
    setting_value TEXT           NULL,
    updated_by   BIGINT UNSIGNED NULL,
    updated_at   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_settings_key (setting_key),
    CONSTRAINT fk_settings_updated_by FOREIGN KEY (updated_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 integrations (
    id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name          VARCHAR(120)    NOT NULL COMMENT 'QuickBooks Online, Twilio, Slack, Google Calendar...',
    slug          VARCHAR(80)     NOT NULL,
    status        ENUM('connected','not_connected','error') NOT NULL DEFAULT 'not_connected',
    credentials_encrypted TEXT   NULL,
    connected_by  BIGINT UNSIGNED NULL,
    connected_at  DATETIME        NULL,
    created_at    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_integrations_slug (slug),
    CONSTRAINT fk_integrations_connected_by FOREIGN KEY (connected_by) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


SET FOREIGN_KEY_CHECKS = 1;
