CREATE TABLE IF NOT EXISTS system_configurations (
    setting_key VARCHAR(100) NOT NULL PRIMARY KEY,
    setting_value LONGTEXT NULL,
    value_type VARCHAR(20) NOT NULL DEFAULT 'string',
    source VARCHAR(30) NOT NULL DEFAULT 'system',
    description VARCHAR(255) DEFAULT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    KEY idx_system_configurations_source (source)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE IF NOT EXISTS mra_eis_configurations (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    terminal_local_id INT NOT NULL,
    global_config_version INT NOT NULL DEFAULT 1,
    taxpayer_config_version INT NOT NULL DEFAULT 1,
    terminal_config_version INT NOT NULL DEFAULT 1,
    standard_vat_percentage DECIMAL(7,4) DEFAULT NULL,
    global_configuration_json LONGTEXT NOT NULL,
    taxpayer_configuration_json LONGTEXT NOT NULL,
    terminal_configuration_json LONGTEXT NOT NULL,
    tax_rates_json LONGTEXT NOT NULL,
    levy_rates_json LONGTEXT NOT NULL,
    source_response_json LONGTEXT NOT NULL,
    source_hash CHAR(64) NOT NULL,
    mra_synced_at DATETIME NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uniq_mra_eis_configuration_terminal (terminal_local_id),
    KEY idx_mra_eis_configuration_vat (standard_vat_percentage),
    KEY idx_mra_eis_configuration_synced (mra_synced_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
