CREATE TABLE IF NOT EXISTS tenants (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  tenant_id TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  business_type TEXT,
  industry TEXT,
  country TEXT,
  city TEXT,
  timezone TEXT NOT NULL DEFAULT 'Asia/Almaty',
  language_preferences_json TEXT NOT NULL DEFAULT '["ru"]',
  default_currency TEXT NOT NULL DEFAULT 'KZT',
  subscription_plan TEXT,
  is_active BOOLEAN NOT NULL DEFAULT 1,
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS ix_tenants_tenant_id ON tenants(tenant_id);
CREATE INDEX IF NOT EXISTS ix_tenants_is_active ON tenants(is_active);
CREATE INDEX IF NOT EXISTS ix_tenants_country ON tenants(country);
CREATE INDEX IF NOT EXISTS ix_tenants_city ON tenants(city);
CREATE INDEX IF NOT EXISTS ix_tenants_default_currency ON tenants(default_currency);

CREATE TABLE IF NOT EXISTS tenant_sales_settings (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  tenant_id TEXT NOT NULL UNIQUE,
  default_pipeline_id TEXT,
  default_language TEXT NOT NULL DEFAULT 'ru',
  default_currency TEXT NOT NULL DEFAULT 'KZT',
  enabled_text_channels_json TEXT NOT NULL DEFAULT '[]',
  enabled_voice_channels_json TEXT NOT NULL DEFAULT '[]',
  payment_provider_settings_json TEXT NOT NULL DEFAULT '{}',
  document_settings_json TEXT NOT NULL DEFAULT '{}',
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS ix_tenant_sales_settings_tenant_id ON tenant_sales_settings(tenant_id);
CREATE INDEX IF NOT EXISTS ix_tenant_sales_settings_default_pipeline_id ON tenant_sales_settings(default_pipeline_id);

CREATE TABLE IF NOT EXISTS tenant_integrations (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  integration_id TEXT NOT NULL UNIQUE,
  tenant_id TEXT NOT NULL,
  provider_type TEXT NOT NULL,
  provider_name TEXT NOT NULL,
  provider_account_id TEXT,
  external_identifier TEXT,
  settings_json TEXT NOT NULL DEFAULT '{}',
  is_active BOOLEAN NOT NULL DEFAULT 1,
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS ix_tenant_integrations_integration_id ON tenant_integrations(integration_id);
CREATE INDEX IF NOT EXISTS ix_tenant_integrations_tenant_id ON tenant_integrations(tenant_id);
CREATE INDEX IF NOT EXISTS ix_tenant_integrations_provider_type ON tenant_integrations(provider_type);
CREATE INDEX IF NOT EXISTS ix_tenant_integrations_provider_name ON tenant_integrations(provider_name);
CREATE INDEX IF NOT EXISTS ix_tenant_integrations_provider_account_id ON tenant_integrations(provider_account_id);
CREATE INDEX IF NOT EXISTS ix_tenant_integrations_external_identifier ON tenant_integrations(external_identifier);
CREATE INDEX IF NOT EXISTS ix_tenant_integrations_is_active ON tenant_integrations(is_active);
CREATE INDEX IF NOT EXISTS idx_tenant_integrations_provider_account ON tenant_integrations(provider_type, provider_name, provider_account_id);
CREATE INDEX IF NOT EXISTS idx_tenant_integrations_external_identifier ON tenant_integrations(provider_type, provider_name, external_identifier);

CREATE UNIQUE INDEX IF NOT EXISTS idx_sales_payments_tenant_external_payment_unique
  ON sales_payments(tenant_id, payment_provider, external_payment_id)
  WHERE external_payment_id IS NOT NULL;
