{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Pine Property Services operating model v1",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "slug": {
      "type": "string"
    },
    "title": {
      "type": "string"
    },
    "industry": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "workflows": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "asOf": {
      "type": "string"
    },
    "source": {
      "type": "string"
    },
    "tables": {
      "type": "object",
      "properties": {
        "properties": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {}
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "property_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "name": {
                    "type": "string"
                  },
                  "kind": {
                    "type": "string"
                  },
                  "manager": {
                    "type": "string"
                  }
                },
                "required": [
                  "property_id",
                  "name",
                  "kind",
                  "manager"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "work_orders": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "work_order_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "property_id": {
                    "type": "string"
                  },
                  "date": {
                    "type": "string"
                  },
                  "task": {
                    "type": "string"
                  },
                  "status": {
                    "type": "string"
                  }
                },
                "required": [
                  "work_order_id",
                  "property_id",
                  "date",
                  "task",
                  "status"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "job_costs": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "cost_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "work_order_id": {
                    "type": "string"
                  },
                  "labor_hours": {
                    "type": "number"
                  },
                  "labor_rate": {
                    "type": "number"
                  },
                  "materials": {
                    "type": "number"
                  },
                  "labor_cost": {
                    "type": "number"
                  },
                  "total": {
                    "type": "number"
                  },
                  "currency": {
                    "type": "string"
                  }
                },
                "required": [
                  "cost_id",
                  "work_order_id",
                  "labor_hours",
                  "labor_rate",
                  "materials",
                  "labor_cost",
                  "total",
                  "currency"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "estimates": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "estimate_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "work_order_id": {
                    "type": "string"
                  },
                  "labor_allowance": {
                    "type": "number"
                  },
                  "material_allowance": {
                    "type": "number"
                  },
                  "estimate_total": {
                    "type": "number"
                  },
                  "currency": {
                    "type": "string"
                  }
                },
                "required": [
                  "estimate_id",
                  "work_order_id",
                  "labor_allowance",
                  "material_allowance",
                  "estimate_total",
                  "currency"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "inspections": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "inspection_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "work_order_id": {
                    "type": "string"
                  },
                  "date": {
                    "type": "string"
                  },
                  "check": {
                    "type": "string"
                  },
                  "result": {
                    "type": "string"
                  },
                  "note": {
                    "type": "string"
                  }
                },
                "required": [
                  "inspection_id",
                  "work_order_id",
                  "date",
                  "check",
                  "result",
                  "note"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "maintenance": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "maintenance_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "property_id": {
                    "type": "string"
                  },
                  "task": {
                    "type": "string"
                  },
                  "last_date": {
                    "type": "string"
                  },
                  "next_date": {
                    "type": "string"
                  },
                  "assigned_team": {
                    "type": "string"
                  }
                },
                "required": [
                  "maintenance_id",
                  "property_id",
                  "task",
                  "last_date",
                  "next_date",
                  "assigned_team"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        }
      },
      "required": [
        "properties",
        "work_orders",
        "job_costs",
        "estimates",
        "inspections",
        "maintenance"
      ],
      "additionalProperties": false
    },
    "metrics": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "value": {
            "type": "number"
          },
          "unit": {
            "type": "string"
          },
          "calculation": {
            "type": "string"
          },
          "formula": {
            "type": "object",
            "properties": {
              "op": {
                "type": "string"
              },
              "table": {
                "type": "string"
              },
              "column": {
                "type": "string"
              }
            },
            "required": [
              "op",
              "table"
            ],
            "additionalProperties": false
          }
        },
        "required": [
          "id",
          "label",
          "value",
          "unit",
          "calculation",
          "formula"
        ],
        "additionalProperties": false
      }
    },
    "chart": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string"
        },
        "unit": {
          "type": "string"
        },
        "points": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "label": {
                "type": "string"
              },
              "value": {
                "type": "number"
              }
            },
            "required": [
              "label",
              "value"
            ],
            "additionalProperties": false
          }
        }
      },
      "required": [
        "title",
        "unit",
        "points"
      ],
      "additionalProperties": false
    },
    "notes": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  },
  "required": [
    "id",
    "slug",
    "title",
    "industry",
    "description",
    "workflows",
    "asOf",
    "source",
    "tables",
    "metrics",
    "chart",
    "notes"
  ],
  "additionalProperties": false,
  "x-relationalRules": [
    {
      "table": "properties",
      "unique": "property_id",
      "foreignKeys": []
    },
    {
      "table": "work_orders",
      "unique": "work_order_id",
      "foreignKeys": [
        {
          "column": "property_id",
          "table": "properties",
          "targetColumn": "property_id"
        }
      ]
    },
    {
      "table": "job_costs",
      "unique": "cost_id",
      "foreignKeys": [
        {
          "column": "work_order_id",
          "table": "work_orders",
          "targetColumn": "work_order_id"
        }
      ]
    },
    {
      "table": "estimates",
      "unique": "estimate_id",
      "foreignKeys": [
        {
          "column": "work_order_id",
          "table": "work_orders",
          "targetColumn": "work_order_id"
        }
      ]
    },
    {
      "table": "inspections",
      "unique": "inspection_id",
      "foreignKeys": [
        {
          "column": "work_order_id",
          "table": "work_orders",
          "targetColumn": "work_order_id"
        }
      ]
    },
    {
      "table": "maintenance",
      "unique": "maintenance_id",
      "foreignKeys": [
        {
          "column": "property_id",
          "table": "properties",
          "targetColumn": "property_id"
        }
      ]
    }
  ]
}
