{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.3plgrade.org/schema/grade-record.schema.json",
  "title": "3PL Grade record",
  "type": "object",
  "required": [
    "grade_id",
    "standard_version",
    "provider",
    "facility",
    "capability",
    "status",
    "graded_at",
    "checks"
  ],
  "properties": {
    "grade_id": {
      "type": "string",
      "pattern": "^3PLG-[0-9]{4}-[0-9]{6}$"
    },
    "standard_version": {
      "type": "string",
      "examples": [
        "1.0.0"
      ]
    },
    "provider": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "legal_name": {
          "type": "string"
        },
        "website": {
          "type": "string",
          "format": "uri"
        },
        "registry_id": {
          "type": "string"
        },
        "registry_state": {
          "type": "string"
        }
      }
    },
    "facility": {
      "type": "object",
      "required": [
        "address"
      ],
      "properties": {
        "facility_id": {
          "type": "string"
        },
        "address": {
          "type": "object",
          "properties": {
            "street": {
              "type": "string"
            },
            "city": {
              "type": "string"
            },
            "state": {
              "type": "string",
              "pattern": "^[A-Z]{2}$"
            },
            "postal_code": {
              "type": "string"
            },
            "country": {
              "const": "US"
            }
          }
        },
        "geo": {
          "type": "object",
          "properties": {
            "lat": {
              "type": "number"
            },
            "lng": {
              "type": "number"
            }
          }
        },
        "square_feet": {
          "type": "integer"
        }
      }
    },
    "capability": {
      "enum": [
        "ECOM",
        "B2B",
        "FBA",
        "COLD",
        "HAZ",
        "PHARMA",
        "RET",
        "KIT"
      ]
    },
    "status": {
      "enum": [
        "graded",
        "not_graded",
        "hold",
        "under_appeal",
        "expired",
        "withdrawn"
      ]
    },
    "score": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 0,
      "maximum": 100
    },
    "raw_score": {
      "type": [
        "number",
        "null"
      ]
    },
    "letter": {
      "enum": [
        "A+",
        "A",
        "B+",
        "B",
        "C+",
        "C",
        null
      ]
    },
    "coverage": {
      "type": "number",
      "minimum": 0,
      "maximum": 1
    },
    "pillars": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "applicable_max",
          "earned",
          "score"
        ],
        "properties": {
          "id": {
            "enum": [
              "P1",
              "P2",
              "P3",
              "P4",
              "P5",
              "P6"
            ]
          },
          "applicable_max": {
            "type": "number"
          },
          "earned": {
            "type": "number"
          },
          "score": {
            "type": "number"
          }
        }
      }
    },
    "checks": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "applicable",
          "achievement",
          "evidence_level"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "applicable": {
            "type": "boolean"
          },
          "achievement": {
            "type": "number",
            "enum": [
              0,
              0.5,
              0.75,
              1
            ]
          },
          "evidence_level": {
            "enum": [
              "IV",
              "DP",
              "PC",
              "NONE"
            ]
          },
          "points_earned": {
            "type": "number"
          },
          "note": {
            "type": "string"
          },
          "evidence": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "type",
                "checked_at"
              ],
              "properties": {
                "type": {
                  "enum": [
                    "registry",
                    "certificate_db",
                    "live_report",
                    "reference_call",
                    "review_platform",
                    "public_record",
                    "walkthrough",
                    "document",
                    "website"
                  ]
                },
                "source": {
                  "type": "string"
                },
                "url": {
                  "type": "string"
                },
                "file_ref": {
                  "type": "string"
                },
                "fact_date": {
                  "type": "string",
                  "format": "date"
                },
                "checked_at": {
                  "type": "string",
                  "format": "date-time"
                },
                "checked_by": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
    "caps_applied": {
      "type": "array",
      "items": {
        "enum": [
          "CAP-1",
          "CAP-2",
          "CAP-3",
          "CAP-4"
        ]
      }
    },
    "red_flags": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "enum": [
              "RF-1",
              "RF-2",
              "RF-3",
              "RF-4",
              "RF-5",
              "RF-6"
            ]
          },
          "opened_at": {
            "type": "string",
            "format": "date"
          },
          "source": {
            "type": "string"
          },
          "resolved_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date"
          }
        }
      }
    },
    "graded_at": {
      "type": "string",
      "format": "date"
    },
    "valid_until": {
      "type": "string",
      "format": "date"
    },
    "grader_id": {
      "type": "string"
    },
    "reviewer_id": {
      "type": [
        "string",
        "null"
      ]
    },
    "payment_status_visible_to_grader": {
      "const": false
    },
    "verify_url": {
      "type": "string",
      "format": "uri"
    },
    "history": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "at": {
            "type": "string",
            "format": "date-time"
          },
          "by": {
            "type": "string"
          },
          "change": {
            "type": "string"
          },
          "reason": {
            "type": "string"
          }
        }
      }
    }
  }
}
