CREATE TABLE portability_rules (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    bank_id INT UNSIGNED NULL,
    bank_name VARCHAR(150) NOT NULL,
    bank_aliases JSON NULL,
    product_type VARCHAR(40) NOT NULL,
    rule_group VARCHAR(60) NOT NULL,
    context_key VARCHAR(80) NOT NULL,
    comparator VARCHAR(30) NOT NULL DEFAULT 'range',
    min_value DECIMAL(13,2) NULL,
    max_value DECIMAL(13,2) NULL,
    text_value VARCHAR(255) NULL,
    installment_min_value DECIMAL(13,2) NULL,
    debt_balance_min DECIMAL(13,2) NULL,
    extra_payload JSON NULL,
    status ENUM('allowed','manual','blocked') NOT NULL DEFAULT 'allowed',
    message VARCHAR(255) NULL,
    notes TEXT NULL,
    priority INT NOT NULL DEFAULT 100,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_by INT UNSIGNED NULL,
    updated_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_portability_rules_bank FOREIGN KEY (bank_id) REFERENCES bank_catalog (id) ON DELETE SET NULL,
    CONSTRAINT fk_portability_rules_created_by FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL,
    CONSTRAINT fk_portability_rules_updated_by FOREIGN KEY (updated_by) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
