PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS businesses (
 id INTEGER PRIMARY KEY AUTOINCREMENT,
 organization_id TEXT NOT NULL,
 name TEXT NOT NULL,
 website TEXT NOT NULL DEFAULT '',
 website_domain TEXT NOT NULL DEFAULT '',
 email TEXT NOT NULL DEFAULT '',
 phone TEXT NOT NULL DEFAULT '',
 description TEXT NOT NULL DEFAULT '',
 score INTEGER NOT NULL DEFAULT 0,
 score_version TEXT NOT NULL DEFAULT 'mvp-1',
 score_factors TEXT NOT NULL DEFAULT '[]',
 website_class TEXT NOT NULL DEFAULT 'missing',
 created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_businesses_org ON businesses(organization_id);
CREATE UNIQUE INDEX IF NOT EXISTS uq_business_domain ON businesses(organization_id, website_domain) WHERE website_domain <> '';
CREATE UNIQUE INDEX IF NOT EXISTS uq_business_email ON businesses(organization_id, email) WHERE email <> '';
CREATE UNIQUE INDEX IF NOT EXISTS uq_business_phone ON businesses(organization_id, phone) WHERE phone <> '';
CREATE TABLE IF NOT EXISTS suppressions (
 id INTEGER PRIMARY KEY AUTOINCREMENT,
 organization_id TEXT NOT NULL,
 kind TEXT NOT NULL CHECK(kind IN ('email','domain','phone')),
 value TEXT NOT NULL,
 created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
 UNIQUE(organization_id, kind, value)
);
