-- Migration 019: Firm branding (Fase 5 / G16)
-- Data: 2026-05-29
--
-- Tabella di branding white-label per studi di consulenza.
-- Permette al consulente di personalizzare logo/colori che vedranno i suoi clienti.
--
-- Lookup: per ogni utente loggato → si guarda users.consulting_firm_id →
--         firm_branding.firm_id matching → si applica
--
-- Rollback: DROP TABLE firm_branding;

SET @tbl := (
    SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'firm_branding'
);
SET @sql := IF(@tbl = 0,
    'CREATE TABLE firm_branding (
        firm_id INT NOT NULL PRIMARY KEY,
        logo_url VARCHAR(512) NULL COMMENT ''URL assoluto o relativo al logo (es. /uploads/firms/123/logo.svg)'',
        primary_color CHAR(7) NULL COMMENT ''Hex #RRGGBB del colore primario UI'',
        secondary_color CHAR(7) NULL COMMENT ''Hex #RRGGBB del colore secondario UI'',
        custom_brand_name VARCHAR(120) NULL COMMENT ''Override del nome prodotto in UI (es. "Lo Studio X NIS2 Suite")'',
        custom_domain VARCHAR(255) NULL COMMENT ''Sottodominio dedicato (futuro)'',
        updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        FOREIGN KEY (firm_id) REFERENCES consulting_firms(id) ON DELETE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
      COMMENT=''White-label branding per consulting firm''',
    'SELECT ''firm_branding già presente — skip'' AS info'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SELECT TABLE_NAME, ENGINE, TABLE_COMMENT FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'firm_branding';
