-- Vários condomínios por tenant (cadastro manual; CNPJ opcional).
SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `tenant_condominiums` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `tenant_id` BIGINT UNSIGNED NOT NULL,
  `display_name` VARCHAR(150) NOT NULL,
  `cnpj` CHAR(14) NULL DEFAULT NULL COMMENT 'Somente dígitos; opcional',
  `legal_name` VARCHAR(200) NULL DEFAULT NULL,
  `trade_name` VARCHAR(200) NULL DEFAULT NULL,
  `company_phone` VARCHAR(30) NULL DEFAULT NULL,
  `company_cep` VARCHAR(10) NULL DEFAULT NULL,
  `company_street` VARCHAR(200) NULL DEFAULT NULL,
  `company_number` VARCHAR(20) NULL DEFAULT NULL,
  `company_complement` VARCHAR(120) NULL DEFAULT NULL,
  `company_neighborhood` VARCHAR(120) NULL DEFAULT NULL,
  `company_city` VARCHAR(120) NULL DEFAULT NULL,
  `company_state` CHAR(2) NULL DEFAULT NULL,
  `is_default` TINYINT(1) NOT NULL DEFAULT 0,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tcondo_tenant_cnpj` (`tenant_id`, `cnpj`),
  KEY `idx_tcondo_tenant` (`tenant_id`),
  KEY `idx_tcondo_tenant_default` (`tenant_id`, `is_default`),
  CONSTRAINT `fk_tcondo_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
