CREATE TABLE IF NOT EXISTS tenant_market_profiles (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    blueprint_key VARCHAR(80) NOT NULL DEFAULT 'professional_services',
    segment_key VARCHAR(80) NOT NULL DEFAULT 'smb',
    icp_summary VARCHAR(255) NULL,
    target_channels_json JSON NULL,
    ai_mode ENUM('assist', 'copilot') NOT NULL DEFAULT 'assist',
    partner_tier ENUM('direct', 'partner', 'white_label') NOT NULL DEFAULT 'direct',
    created_by BIGINT UNSIGNED NULL,
    updated_by BIGINT UNSIGNED NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uq_tenant_market_profiles_tenant (tenant_id),
    CONSTRAINT fk_tenant_market_profiles_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    CONSTRAINT fk_tenant_market_profiles_created_by FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL,
    CONSTRAINT fk_tenant_market_profiles_updated_by FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE contact_import_jobs
    MODIFY COLUMN status ENUM('preview', 'running', 'finished', 'finished_with_errors', 'failed') NOT NULL DEFAULT 'running';

ALTER TABLE contact_import_rows
    MODIFY COLUMN status ENUM('preview_valid', 'preview_error', 'preview_skipped', 'imported', 'failed', 'skipped') NOT NULL DEFAULT 'imported';

INSERT INTO tenant_market_profiles (tenant_id, blueprint_key, segment_key, icp_summary, target_channels_json, ai_mode, partner_tier)
SELECT
    t.id,
    'professional_services',
    'smb',
    'Empresas que venden servicios consultivos con seguimiento comercial y operativo.',
    JSON_ARRAY('email', 'whatsapp', 'calendar'),
    'assist',
    'direct'
FROM tenants t
WHERE t.slug = 'demo'
ON DUPLICATE KEY UPDATE
    blueprint_key = VALUES(blueprint_key),
    segment_key = VALUES(segment_key),
    icp_summary = VALUES(icp_summary),
    target_channels_json = VALUES(target_channels_json),
    ai_mode = VALUES(ai_mode),
    partner_tier = VALUES(partner_tier),
    updated_at = NOW();
