BEGIN;

ALTER TABLE platform.tenants
    ADD COLUMN IF NOT EXISTS website_url character varying(255),
    ADD COLUMN IF NOT EXISTS support_email character varying(150),
    ADD COLUMN IF NOT EXISTS industry character varying(100);

ALTER TABLE platform.tenant_brands
    ADD COLUMN IF NOT EXISTS accent_color character varying(20),
    ADD COLUMN IF NOT EXISTS default_theme character varying(20),
    ADD COLUMN IF NOT EXISTS email_header_image_url text;

CREATE TABLE IF NOT EXISTS platform.tenant_business_profiles (
    id bigserial PRIMARY KEY,
    tenant_id uuid NOT NULL UNIQUE,
    registration_number character varying(100),
    tax_identifier character varying(100),
    business_type character varying(50),
    incorporation_country_id character varying(10),
    registered_address text,
    is_active boolean NOT NULL DEFAULT TRUE,
    deleted_at timestamp without time zone NULL,
    deleted_by uuid NULL,
    created_by uuid NULL,
    updated_by uuid NULL,
    created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_tenant_business_profiles_tenant
        FOREIGN KEY (tenant_id) REFERENCES platform.tenants(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_tenant_business_profiles_tenant
    ON platform.tenant_business_profiles (tenant_id)
    WHERE deleted_at IS NULL;

CREATE TABLE IF NOT EXISTS platform.tenant_localization_settings (
    id bigserial PRIMARY KEY,
    tenant_id uuid NOT NULL UNIQUE,
    language_code character varying(20),
    timezone character varying(100),
    currency_code character varying(10),
    date_format character varying(30),
    number_format character varying(30),
    first_day_of_week character varying(20),
    is_active boolean NOT NULL DEFAULT TRUE,
    deleted_at timestamp without time zone NULL,
    deleted_by uuid NULL,
    created_by uuid NULL,
    updated_by uuid NULL,
    created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_tenant_localization_settings_tenant
        FOREIGN KEY (tenant_id) REFERENCES platform.tenants(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_tenant_localization_settings_tenant
    ON platform.tenant_localization_settings (tenant_id)
    WHERE deleted_at IS NULL;

COMMIT;
