-- Business profile: legal / contact / healthcare identifiers per business, and per-industry rules for
-- which profile fields are shown.
--   business_profiles            one row per business; the values are one JSON document keyed by field key
--   business_profile_field_rules a tenant admin's override of a field for an industry (rows exist only
--                                where the choice differs from the built-in default in the API)
-- STRUCTURE ONLY - no data. Idempotent - safe to re-run. No USE statement: apply-migrations.sh selects
-- the database. Local: mysql -u root -p db_abos_v0.1 < db/business_profile.sql

CREATE TABLE IF NOT EXISTS business_profiles (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    tenant_id   INT          NOT NULL,
    business_id INT          NOT NULL,
    data_json   LONGTEXT     NULL,
    created_at  TIMESTAMP(6) NULL,
    created_by  INT          NULL,
    updated_at  TIMESTAMP(6) NULL,
    updated_by  INT          NULL,
    is_deleted  TINYINT(1)   NOT NULL DEFAULT 0,
    UNIQUE KEY uq_business_profiles_business (tenant_id, business_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS business_profile_field_rules (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    tenant_id   INT          NOT NULL,
    industry    VARCHAR(100) NOT NULL,
    field_key   VARCHAR(80)  NOT NULL,
    enabled     TINYINT(1)   NOT NULL,
    updated_at  TIMESTAMP(6) NULL,
    updated_by  INT          NULL,
    UNIQUE KEY uq_business_profile_rule (tenant_id, industry, field_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
