{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.sintegrapi.com.br/schemas/regime-tributario-analise-v2.json",
  "title": "Análise de Regime Tributário (Inteligência Sintegrapi)",
  "description": "Respostas 200, 404 e 503 da rota v2 regime-tributario-analise.",
  "type": "object",
  "required": ["request_id", "success", "error"],
  "properties": {
    "request_id": { "type": "string", "format": "uuid" },
    "success": { "type": "boolean" },
    "error": { "type": "boolean" },
    "cnpj": { "type": "string", "pattern": "^[A-Z0-9]{12}[0-9]{2}$" },
    "situacao_rf": { "type": ["string", "null"] },
    "regime_tributario": {
      "oneOf": [
        { "type": "null" },
        {
          "type": "object",
          "required": ["id", "descricao"],
          "oneOf": [
            { "properties": { "id": { "const": 0 }, "descricao": { "const": "MEI" } } },
            { "properties": { "id": { "const": 1 }, "descricao": { "const": "SIMPLES_NACIONAL" } } },
            { "properties": { "id": { "const": 20 }, "descricao": { "const": "IMUNE DO IRPJ" } } },
            { "properties": { "id": { "const": 30 }, "descricao": { "const": "ISENTA DO IRPJ" } } },
            { "properties": { "id": { "const": 40 }, "descricao": { "const": "LUCRO ARBITRADO" } } },
            { "properties": { "id": { "const": 50 }, "descricao": { "const": "LUCRO PRESUMIDO" } } },
            { "properties": { "id": { "const": 60 }, "descricao": { "const": "LUCRO PRESUMIDO/ARBITRADO" } } },
            { "properties": { "id": { "const": 70 }, "descricao": { "const": "LUCRO PRESUMIDO/REAL" } } },
            { "properties": { "id": { "const": 80 }, "descricao": { "const": "LUCRO PRESUMIDO/REAL/ARBITRADO" } } },
            { "properties": { "id": { "const": 90 }, "descricao": { "const": "LUCRO REAL" } } },
            { "properties": { "id": { "const": 100 }, "descricao": { "const": "LUCRO REAL/ARBITRADO" } } }
          ]
        }
      ]
    },
    "probabilidade_regime_tributario": { "title": "Probabilidade", "type": ["integer", "null"], "minimum": 0, "maximum": 100 },
    "regime_regular": {
      "type": "object",
      "description": "Situação do estabelecimento no regime regular de IBS e CBS.",
      "required": ["ibs", "cbs", "periodos_anteriores", "origem", "expira_em"],
      "properties": {
        "ibs": { "$ref": "#/$defs/situacao_regime_regular" },
        "cbs": { "$ref": "#/$defs/situacao_regime_regular" },
        "periodos_anteriores": { "type": ["array", "null"], "items": { "type": "object" } },
        "origem": { "type": "string", "minLength": 1 },
        "expira_em": { "type": "string", "format": "date-time" }
      }
    },
    "simples_nacional": {
      "type": "object",
      "properties": {
        "eventos_futuros": {
          "type": ["array", "null"],
          "items": {
            "type": ["object", "null"],
            "properties": {
              "regime": { "type": ["string", "null"] },
              "descricao": { "type": ["string", "null"] },
              "data_efeito": { "type": ["string", "null"], "description": "Data de efeito informada no formato YYYY-MM-DD." }
            }
          }
        }
      },
      "additionalProperties": false
    },
    "eventos_futuros": false,
    "mensagem": { "type": "string" },
    "tipo_resultado": { "enum": ["CONFIRMADO_NA_BASE", "ESTIMADO", "INDETERMINADO", "NAO_CONSULTADO"] },
    "error_message": {
      "type": "object",
      "required": ["code", "message"],
      "properties": { "code": { "type": "string" }, "message": { "type": "string" } }
    }
  },
  "$defs": {
    "situacao_regime_regular": {
      "type": "object",
      "required": ["optante", "situacao"],
      "properties": {
        "optante": { "type": ["boolean", "null"] },
        "situacao": { "type": ["string", "null"] }
      }
    }
  },
  "dependentRequired": {
    "simples_nacional": ["regime_regular"],
    "regime_regular": ["simples_nacional"]
  },
  "if": { "properties": { "error": { "const": true } } },
  "then": {
    "required": ["error_message"],
    "properties": { "success": { "const": false }, "regime_regular": false, "simples_nacional": false }
  },
  "else": {
    "required": ["cnpj", "situacao_rf", "regime_tributario", "probabilidade_regime_tributario", "mensagem", "tipo_resultado"],
    "allOf": [
      {
        "if": { "properties": { "success": { "const": false } } },
        "then": {
          "required": ["error_message"],
          "properties": { "situacao_rf": { "type": "null" }, "tipo_resultado": { "const": "NAO_CONSULTADO" } }
        },
        "else": { "properties": { "situacao_rf": { "type": "string" } } }
      }
    ],
    "oneOf": [
      {
        "properties": {
          "tipo_resultado": { "enum": ["CONFIRMADO_NA_BASE", "ESTIMADO"] },
          "regime_tributario": { "type": "object" },
          "probabilidade_regime_tributario": { "type": "integer" },
          "mensagem": { "const": "" }
        }
      },
      {
        "properties": {
          "tipo_resultado": { "enum": ["INDETERMINADO", "NAO_CONSULTADO"] },
          "regime_regular": false,
          "simples_nacional": false,
          "regime_tributario": { "type": "null" },
          "probabilidade_regime_tributario": { "type": "null" },
          "mensagem": { "minLength": 1 }
        }
      }
    ]
  }
}
