-- Tenant-level model provider configurations (API keys per provider)
CREATE TABLE tenant_model_providers (
    id          BIGINT NOT NULL AUTO_INCREMENT,
    tenant_id   BIGINT NOT NULL,
    name        VARCHAR(128) NOT NULL,
    protocol    VARCHAR(32) NOT NULL COMMENT 'openai|gemini|dashscope|seedance|kling',
    capability  VARCHAR(16) NOT NULL COMMENT 'text|image|video',
    base_url    VARCHAR(512),
    api_key     VARCHAR(512),
    secret_key  VARCHAR(512),
    models_json TEXT COMMENT 'JSON array of {id,name,checked}',
    created_at  DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at  DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    INDEX idx_tenant_cap (tenant_id, capability)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Generic key-value settings per tenant (watermark, etc.)
CREATE TABLE tenant_settings (
    id            BIGINT NOT NULL AUTO_INCREMENT,
    tenant_id     BIGINT NOT NULL,
    setting_key   VARCHAR(64) NOT NULL,
    setting_value TEXT,
    updated_at    DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    UNIQUE KEY uk_tenant_key (tenant_id, setting_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Default model selections per tenant
CREATE TABLE tenant_model_defaults (
    id          BIGINT NOT NULL AUTO_INCREMENT,
    tenant_id   BIGINT NOT NULL,
    capability  VARCHAR(16) NOT NULL COMMENT 'text|image|video',
    provider_id BIGINT NOT NULL,
    model_id    VARCHAR(128) NOT NULL,
    updated_at  DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    UNIQUE KEY uk_tenant_cap (tenant_id, capability)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
