{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://maestrogtm.com/resources/schemas/cac-record-1.0.json",
  "$comment": "Date formats are asserted. artifacts/cac-engine.mjs is mandatory for cross-field semantic validation before import or review-ready acceptance.",
  "title": "Maestro B2B CAC record",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "record_id",
    "label",
    "scope",
    "costs",
    "customers",
    "coverage",
    "requests",
    "calculation",
    "provenance"
  ],
  "properties": {
    "schema_version": {
      "const": "maestro-cac-record/1.0"
    },
    "record_id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{2,80}$"
    },
    "label": {
      "type": "string",
      "minLength": 1
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "currency",
        "motion",
        "view",
        "cohort_id",
        "cost_window_start",
        "cost_window_end",
        "follow_up_cutoff",
        "maturity_rule",
        "maturity_state",
        "customer_definition",
        "evaluated_at"
      ],
      "properties": {
        "currency": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^[A-Z]{3}$"
        },
        "motion": {
          "type": [
            "string",
            "null"
          ]
        },
        "view": {
          "enum": [
            "origin_cohort",
            "calendar_close",
            "rolling",
            "Unknown"
          ]
        },
        "cohort_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "cost_window_start": {
          "type": [
            "string",
            "null"
          ],
          "format": "date"
        },
        "cost_window_end": {
          "type": [
            "string",
            "null"
          ],
          "format": "date"
        },
        "follow_up_cutoff": {
          "type": [
            "string",
            "null"
          ],
          "format": "date"
        },
        "maturity_rule": {
          "type": [
            "string",
            "null"
          ]
        },
        "maturity_state": {
          "enum": [
            "mature",
            "immature",
            "Unknown"
          ]
        },
        "customer_definition": {
          "type": [
            "string",
            "null"
          ]
        },
        "evaluated_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        }
      }
    },
    "costs": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "cost_id",
          "category",
          "description",
          "amount",
          "value_state",
          "decision",
          "source_ref",
          "owner",
          "allocation"
        ],
        "properties": {
          "cost_id": {
            "type": "string",
            "pattern": "^cost-[a-z0-9-]+$"
          },
          "category": {
            "enum": [
              "media",
              "marketing",
              "sales",
              "agency",
              "creative",
              "software",
              "event",
              "shared",
              "post_acquisition",
              "other"
            ]
          },
          "description": {
            "type": "string",
            "minLength": 1
          },
          "amount": {
            "type": [
              "number",
              "null"
            ],
            "minimum": 0
          },
          "value_state": {
            "enum": [
              "known",
              "observed_zero",
              "Unknown"
            ]
          },
          "decision": {
            "enum": [
              "include",
              "exclude",
              "allocate",
              "pending"
            ]
          },
          "source_ref": {
            "type": [
              "string",
              "null"
            ]
          },
          "owner": {
            "type": [
              "string",
              "null"
            ]
          },
          "allocation": {
            "oneOf": [
              {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "original_expense_id",
                  "allocation_group_id",
                  "target_motion",
                  "set_complete",
                  "method",
                  "percent",
                  "included_amount",
                  "unassigned_amount"
                ],
                "properties": {
                  "original_expense_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "allocation_group_id": {
                    "type": "string",
                    "pattern": "^allocation-[a-z0-9-]+$"
                  },
                  "target_motion": {
                    "type": "string",
                    "minLength": 1
                  },
                  "set_complete": {
                    "type": "boolean"
                  },
                  "method": {
                    "type": "string",
                    "minLength": 1
                  },
                  "percent": {
                    "type": "number",
                    "minimum": 0,
                    "maximum": 100
                  },
                  "included_amount": {
                    "type": "number",
                    "minimum": 0
                  },
                  "unassigned_amount": {
                    "type": "number",
                    "minimum": 0
                  }
                }
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "value_state": {
                  "const": "Unknown"
                }
              },
              "required": [
                "value_state"
              ]
            },
            "then": {
              "properties": {
                "amount": {
                  "type": "null"
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "value_state": {
                  "const": "observed_zero"
                }
              },
              "required": [
                "value_state"
              ]
            },
            "then": {
              "properties": {
                "amount": {
                  "const": 0
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "value_state": {
                  "const": "known"
                }
              },
              "required": [
                "value_state"
              ]
            },
            "then": {
              "properties": {
                "amount": {
                  "type": "number",
                  "exclusiveMinimum": 0
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "decision": {
                  "const": "allocate"
                }
              },
              "required": [
                "decision"
              ]
            },
            "then": {
              "properties": {
                "allocation": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "original_expense_id",
                    "allocation_group_id",
                    "target_motion",
                    "set_complete",
                    "method",
                    "percent",
                    "included_amount",
                    "unassigned_amount"
                  ],
                  "properties": {
                    "original_expense_id": {
                      "type": "string",
                      "minLength": 1
                    },
                    "allocation_group_id": {
                      "type": "string",
                      "pattern": "^allocation-[a-z0-9-]+$"
                    },
                    "target_motion": {
                      "type": "string",
                      "minLength": 1
                    },
                    "set_complete": {
                      "type": "boolean"
                    },
                    "method": {
                      "type": "string",
                      "minLength": 1
                    },
                    "percent": {
                      "type": "number",
                      "minimum": 0,
                      "maximum": 100
                    },
                    "included_amount": {
                      "type": "number",
                      "minimum": 0
                    },
                    "unassigned_amount": {
                      "type": "number",
                      "minimum": 0
                    }
                  }
                }
              }
            },
            "else": {
              "properties": {
                "allocation": {
                  "type": "null"
                }
              }
            }
          }
        ]
      }
    },
    "customers": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "record_id",
          "customer_id",
          "event_type",
          "event_date",
          "included",
          "reason",
          "source_ref",
          "cohort_assignment"
        ],
        "properties": {
          "record_id": {
            "type": "string",
            "pattern": "^customer-record-[a-z0-9-]+$"
          },
          "customer_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "event_type": {
            "enum": [
              "new_paying_customer",
              "duplicate",
              "renewal",
              "upsell",
              "repeat_purchase",
              "reactivation",
              "Unknown"
            ]
          },
          "event_date": {
            "type": [
              "string",
              "null"
            ],
            "format": "date"
          },
          "included": {
            "type": [
              "boolean",
              "null"
            ]
          },
          "reason": {
            "type": [
              "string",
              "null"
            ]
          },
          "source_ref": {
            "type": [
              "string",
              "null"
            ]
          },
          "cohort_assignment": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "origin_cohort_id",
              "qualifying_event",
              "qualifying_date",
              "assignment_policy",
              "source_ref",
              "eligible"
            ],
            "properties": {
              "origin_cohort_id": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "qualifying_event": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "qualifying_date": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "date"
              },
              "assignment_policy": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "source_ref": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "eligible": {
                "type": [
                  "boolean",
                  "null"
                ]
              }
            }
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "included": {
                  "const": true
                }
              },
              "required": [
                "included"
              ]
            },
            "then": {
              "properties": {
                "event_type": {
                  "const": "new_paying_customer"
                },
                "customer_id": {
                  "type": "string",
                  "minLength": 1
                },
                "event_date": {
                  "type": "string",
                  "format": "date"
                },
                "source_ref": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "event_type": {
                  "enum": [
                    "duplicate",
                    "renewal",
                    "upsell",
                    "repeat_purchase"
                  ]
                }
              },
              "required": [
                "event_type"
              ]
            },
            "then": {
              "properties": {
                "included": {
                  "const": false
                }
              }
            }
          }
        ]
      }
    },
    "coverage": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "required_sources",
        "confirmed_sources",
        "missing_sources",
        "status"
      ],
      "properties": {
        "required_sources": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "confirmed_sources": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "missing_sources": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "status": {
          "enum": [
            "complete",
            "partial",
            "Unknown"
          ]
        }
      }
    },
    "requests": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "request_id",
          "field_path",
          "owner",
          "reason",
          "status"
        ],
        "properties": {
          "request_id": {
            "type": "string",
            "pattern": "^REQ-[A-Z0-9-]{3,80}$"
          },
          "field_path": {
            "type": "string",
            "minLength": 1
          },
          "owner": {
            "type": [
              "string",
              "null"
            ]
          },
          "reason": {
            "type": "string",
            "minLength": 1
          },
          "status": {
            "enum": [
              "open",
              "resolved"
            ]
          }
        }
      }
    },
    "calculation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "known_included_cost_subtotal",
        "included_acquisition_cost",
        "unique_new_paying_customers",
        "fully_loaded_cac",
        "media_cost",
        "media_only_cost_per_customer",
        "formula"
      ],
      "properties": {
        "status": {
          "enum": [
            "review_ready",
            "provisional",
            "hold"
          ]
        },
        "known_included_cost_subtotal": {
          "type": "number",
          "minimum": 0
        },
        "included_acquisition_cost": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0
        },
        "unique_new_paying_customers": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "fully_loaded_cac": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0
        },
        "media_cost": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0
        },
        "media_only_cost_per_customer": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0
        },
        "formula": {
          "const": "included_acquisition_cost / unique_new_paying_customers"
        }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "synthetic",
        "source_paths",
        "reviewed_at",
        "notes"
      ],
      "properties": {
        "synthetic": {
          "type": "boolean"
        },
        "source_paths": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "reviewed_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "notes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "calculation": {
            "properties": {
              "status": {
                "const": "review_ready"
              }
            },
            "required": [
              "status"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "scope": {
            "properties": {
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              },
              "motion": {
                "type": "string",
                "minLength": 1
              },
              "view": {
                "enum": [
                  "origin_cohort",
                  "calendar_close",
                  "rolling"
                ]
              },
              "cost_window_start": {
                "type": "string",
                "format": "date"
              },
              "cost_window_end": {
                "type": "string",
                "format": "date"
              },
              "follow_up_cutoff": {
                "type": "string",
                "format": "date"
              },
              "maturity_rule": {
                "type": "string",
                "minLength": 1
              },
              "maturity_state": {
                "const": "mature"
              },
              "customer_definition": {
                "type": "string",
                "minLength": 1
              },
              "evaluated_at": {
                "type": "string",
                "format": "date-time"
              }
            }
          },
          "costs": {
            "not": {
              "contains": {
                "properties": {
                  "decision": {
                    "const": "pending"
                  }
                },
                "required": [
                  "decision"
                ]
              }
            }
          },
          "coverage": {
            "properties": {
              "status": {
                "const": "complete"
              },
              "missing_sources": {
                "maxItems": 0
              }
            }
          },
          "requests": {
            "not": {
              "contains": {
                "properties": {
                  "status": {
                    "const": "open"
                  }
                },
                "required": [
                  "status"
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "calculation": {
            "properties": {
              "status": {
                "const": "provisional"
              }
            },
            "required": [
              "status"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "scope": {
            "properties": {
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              },
              "motion": {
                "type": "string",
                "minLength": 1
              },
              "view": {
                "enum": [
                  "origin_cohort",
                  "calendar_close",
                  "rolling"
                ]
              },
              "cost_window_start": {
                "type": "string",
                "format": "date"
              },
              "cost_window_end": {
                "type": "string",
                "format": "date"
              },
              "follow_up_cutoff": {
                "type": "string",
                "format": "date"
              },
              "maturity_rule": {
                "type": "string",
                "minLength": 1
              },
              "maturity_state": {
                "const": "immature"
              },
              "customer_definition": {
                "type": "string",
                "minLength": 1
              },
              "evaluated_at": {
                "type": "string",
                "format": "date-time"
              }
            }
          },
          "costs": {
            "not": {
              "contains": {
                "properties": {
                  "decision": {
                    "const": "pending"
                  }
                },
                "required": [
                  "decision"
                ]
              }
            }
          },
          "coverage": {
            "properties": {
              "status": {
                "const": "complete"
              },
              "missing_sources": {
                "maxItems": 0
              }
            }
          },
          "requests": {
            "not": {
              "contains": {
                "properties": {
                  "status": {
                    "const": "open"
                  }
                },
                "required": [
                  "status"
                ]
              }
            }
          }
        }
      }
    }
  ]
}
