{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gentifi.com/standards/profile-0.1.schema.json",
  "title": "Gentifi Business Profile",
  "description": "Draft v0.1. The source of truth for what a small business offers, what is available right now, and what actions an AI agent can take on its behalf. Protocol-neutral: the same profile can be served over MCP, embedded in a web page as structured data, or exposed through agent-to-agent and agent-commerce protocols. Field names follow schema.org where a close equivalent exists.",
  "type": "object",
  "required": ["spec_version", "business", "offerings", "availability", "policies", "actions", "updated_at"],
  "properties": {
    "spec_version": { "const": "0.1" },
    "business": { "$ref": "#/$defs/Business" },
    "offerings": { "type": "array", "items": { "$ref": "#/$defs/Offering" } },
    "availability": { "type": "array", "items": { "$ref": "#/$defs/AvailabilityRecord" } },
    "policies": { "$ref": "#/$defs/Policies" },
    "actions": { "type": "array", "items": { "$ref": "#/$defs/ActionCapability" }, "minItems": 1 },
    "updated_at": { "type": "string", "format": "date-time" },
    "extensions": { "$ref": "#/$defs/Extensions" }
  },
  "additionalProperties": false,

  "$defs": {

    "Id": {
      "description": "Stable identifier, unique within the profile. Business ids are global and prefixed 'gtf_'.",
      "type": "string",
      "pattern": "^[a-z0-9_\\-]{3,64}$"
    },

    "Money": {
      "description": "Amounts in minor units (pence, cents) to avoid floating-point errors.",
      "type": "object",
      "required": ["amount_minor", "currency"],
      "properties": {
        "amount_minor": { "type": "integer", "minimum": 0 },
        "currency": { "type": "string", "pattern": "^[A-Z]{3}$", "description": "ISO 4217 code, e.g. GBP" }
      },
      "additionalProperties": false
    },

    "Freshness": {
      "description": "How much an agent should trust a piece of availability data. This is what lets an agent decide whether to book instantly, request confirmation, or warn the user.",
      "type": "object",
      "required": ["observed_at", "confidence", "source"],
      "properties": {
        "observed_at": { "type": "string", "format": "date-time", "description": "When this fact was last true to our knowledge." },
        "valid_until": { "type": "string", "format": "date-time", "description": "After this time, treat the record as stale." },
        "confidence": {
          "type": "string",
          "enum": ["confirmed", "likely", "typical", "unknown"],
          "description": "confirmed = owner or live system verified; likely = recent but unverified; typical = recurring pattern, not a promise; unknown = no reliable signal."
        },
        "source": {
          "type": "string",
          "enum": ["owner", "integration", "message_parse", "pattern", "agent_report"],
          "description": "owner = owner confirmed directly; integration = synced from a booking/POS system; message_parse = extracted by AI from WhatsApp/SMS/photo; pattern = inferred from history; agent_report = reported back after a completed transaction."
        },
        "source_ref": { "type": "string", "description": "Optional pointer to the integration or message the fact came from." }
      },
      "additionalProperties": false
    },

    "Address": {
      "type": "object",
      "required": ["country"],
      "properties": {
        "line1": { "type": "string" },
        "line2": { "type": "string" },
        "locality": { "type": "string" },
        "region": { "type": "string" },
        "postal_code": { "type": "string" },
        "country": { "type": "string", "pattern": "^[A-Z]{2}$", "description": "ISO 3166-1 alpha-2" }
      },
      "additionalProperties": false
    },

    "Geo": {
      "type": "object",
      "required": ["lat", "lng"],
      "properties": {
        "lat": { "type": "number", "minimum": -90, "maximum": 90 },
        "lng": { "type": "number", "minimum": -180, "maximum": 180 }
      },
      "additionalProperties": false
    },

    "ServiceArea": {
      "description": "Where the business serves customers away from its own premises.",
      "oneOf": [
        {
          "type": "object",
          "required": ["type", "centre", "radius_km"],
          "properties": {
            "type": { "const": "radius" },
            "centre": { "$ref": "#/$defs/Geo" },
            "radius_km": { "type": "number", "exclusiveMinimum": 0 }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["type", "postcode_prefixes"],
          "properties": {
            "type": { "const": "postcodes" },
            "postcode_prefixes": { "type": "array", "items": { "type": "string" }, "minItems": 1 }
          },
          "additionalProperties": false
        }
      ]
    },

    "OpeningHours": {
      "description": "Regular opening hours, following schema.org OpeningHoursSpecification.",
      "type": "object",
      "required": ["days", "opens", "closes"],
      "properties": {
        "days": {
          "type": "array",
          "items": { "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] },
          "minItems": 1,
          "uniqueItems": true
        },
        "opens": { "type": "string", "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
        "closes": { "type": "string", "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
        "valid_from": { "type": "string", "format": "date" },
        "valid_through": { "type": "string", "format": "date" }
      },
      "additionalProperties": false
    },

    "Verification": {
      "description": "Whether the business is real and run by the person who controls this profile. Feeds the discovery and trust registry.",
      "type": "object",
      "required": ["status"],
      "properties": {
        "status": { "type": "string", "enum": ["unverified", "claimed", "verified", "suspended"] },
        "methods": {
          "type": "array",
          "items": { "enum": ["phone", "email_domain", "postcard", "companies_house", "google_business_profile", "in_person", "payment_account"] },
          "uniqueItems": true
        },
        "verified_at": { "type": "string", "format": "date-time" },
        "verified_by": { "type": "string", "description": "The registry or organisation that performed the verification." }
      },
      "additionalProperties": false
    },

    "Business": {
      "type": "object",
      "required": ["id", "name", "categories", "location", "verification", "time_zone"],
      "properties": {
        "id": { "type": "string", "pattern": "^gtf_[a-z0-9]{8,32}$" },
        "name": { "type": "string", "minLength": 1 },
        "legal_name": { "type": "string" },
        "description": { "type": "string", "maxLength": 1000 },
        "categories": {
          "description": "schema.org LocalBusiness subtypes where possible (e.g. 'FarmShop' is not one, so use 'Store' plus a tag).",
          "type": "array",
          "items": { "type": "string" },
          "minItems": 1
        },
        "tags": { "type": "array", "items": { "type": "string" } },
        "location": {
          "type": "object",
          "properties": {
            "address": { "$ref": "#/$defs/Address" },
            "geo": { "$ref": "#/$defs/Geo" },
            "service_area": { "$ref": "#/$defs/ServiceArea" },
            "premises_open_to_public": { "type": "boolean" }
          },
          "anyOf": [ { "required": ["address"] }, { "required": ["service_area"] } ],
          "additionalProperties": false
        },
        "time_zone": { "type": "string", "description": "IANA time zone, e.g. Europe/London" },
        "opening_hours": { "type": "array", "items": { "$ref": "#/$defs/OpeningHours" } },
        "contact": {
          "type": "object",
          "properties": {
            "phone": { "type": "string" },
            "email": { "type": "string", "format": "email" },
            "website": { "type": "string", "format": "uri" }
          },
          "additionalProperties": false
        },
        "languages": { "type": "array", "items": { "type": "string" }, "description": "BCP 47 tags" },
        "verification": { "$ref": "#/$defs/Verification" },
        "identifiers": {
          "description": "External ids that help agents match this business with other sources.",
          "type": "object",
          "properties": {
            "companies_house": { "type": "string" },
            "google_place_id": { "type": "string" },
            "osm_id": { "type": "string" }
          },
          "additionalProperties": { "type": "string" }
        }
      },
      "additionalProperties": false
    },

    "Price": {
      "type": "object",
      "required": ["type"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["fixed", "from", "range", "quote"],
          "description": "quote = price only on enquiry; agents should use the 'enquire' action."
        },
        "amount": { "$ref": "#/$defs/Money" },
        "max_amount": { "$ref": "#/$defs/Money" },
        "per": { "type": "string", "description": "Pricing unit, e.g. 'each', 'kg', 'dozen', 'hour', 'person'." },
        "includes_tax": { "type": "boolean", "default": true }
      },
      "allOf": [
        { "if": { "properties": { "type": { "enum": ["fixed", "from"] } } }, "then": { "required": ["amount"] } },
        { "if": { "properties": { "type": { "const": "range" } } }, "then": { "required": ["amount", "max_amount"] } }
      ],
      "additionalProperties": false
    },

    "Variant": {
      "type": "object",
      "required": ["id", "name"],
      "properties": {
        "id": { "$ref": "#/$defs/Id" },
        "name": { "type": "string" },
        "price": { "$ref": "#/$defs/Price" },
        "attributes": { "type": "object", "additionalProperties": { "type": ["string", "number", "boolean"] } }
      },
      "additionalProperties": false
    },

    "Offering": {
      "type": "object",
      "required": ["id", "kind", "name", "price", "fulfilment"],
      "properties": {
        "id": { "$ref": "#/$defs/Id" },
        "kind": {
          "type": "string",
          "enum": ["product", "service", "bookable_service", "rental", "event"],
          "description": "product = stock-based; bookable_service/rental/event = slot-based; service = quoted or ad-hoc work (e.g. trades)."
        },
        "name": { "type": "string", "minLength": 1 },
        "description": { "type": "string", "maxLength": 2000 },
        "price": { "$ref": "#/$defs/Price" },
        "variants": { "type": "array", "items": { "$ref": "#/$defs/Variant" } },
        "duration_minutes": { "type": "integer", "minimum": 1, "description": "For slot-based offerings." },
        "fulfilment": {
          "type": "array",
          "items": { "enum": ["collection", "delivery", "on_premises", "at_customer_location", "remote"] },
          "minItems": 1,
          "uniqueItems": true
        },
        "lead_time_minutes": { "type": "integer", "minimum": 0, "description": "Minimum notice needed, overriding the business-wide policy." },
        "attributes": {
          "description": "Facts agents filter on, e.g. dietary ('vegetarian'), allergens, certifications ('organic'), origin.",
          "type": "object",
          "additionalProperties": { "type": ["string", "number", "boolean", "array"] }
        },
        "seasonal": {
          "type": "object",
          "properties": {
            "months": { "type": "array", "items": { "type": "integer", "minimum": 1, "maximum": 12 }, "uniqueItems": true }
          },
          "additionalProperties": false
        },
        "images": { "type": "array", "items": { "type": "string", "format": "uri" } },
        "tags": { "type": "array", "items": { "type": "string" } }
      },
      "allOf": [
        {
          "if": { "properties": { "kind": { "enum": ["bookable_service", "rental", "event"] } } },
          "then": { "required": ["duration_minutes"] }
        }
      ],
      "additionalProperties": false
    },

    "AvailabilityRecord": {
      "description": "One fact about what can be had and when. Each record carries its own freshness information so agents can judge how far to rely on it.",
      "oneOf": [
        { "$ref": "#/$defs/StockAvailability" },
        { "$ref": "#/$defs/SlotAvailability" },
        { "$ref": "#/$defs/PatternAvailability" }
      ]
    },

    "StockAvailability": {
      "type": "object",
      "required": ["type", "offering_id", "status", "freshness"],
      "properties": {
        "type": { "const": "stock" },
        "offering_id": { "$ref": "#/$defs/Id" },
        "variant_id": { "$ref": "#/$defs/Id" },
        "status": { "type": "string", "enum": ["in_stock", "low", "out_of_stock", "preorder", "out_of_season"] },
        "quantity": { "type": "number", "minimum": 0 },
        "quantity_unit": { "type": "string" },
        "restock_expected_at": { "type": "string", "format": "date-time" },
        "freshness": { "$ref": "#/$defs/Freshness" }
      },
      "additionalProperties": false
    },

    "SlotAvailability": {
      "type": "object",
      "required": ["type", "slot_id", "offering_id", "start", "end", "remaining", "freshness"],
      "properties": {
        "type": { "const": "slot" },
        "slot_id": { "$ref": "#/$defs/Id" },
        "offering_id": { "$ref": "#/$defs/Id" },
        "start": { "type": "string", "format": "date-time" },
        "end": { "type": "string", "format": "date-time" },
        "capacity": { "type": "integer", "minimum": 1 },
        "remaining": { "type": "integer", "minimum": 0 },
        "freshness": { "$ref": "#/$defs/Freshness" }
      },
      "additionalProperties": false
    },

    "PatternAvailability": {
      "description": "A recurring pattern ('usually Saturdays 9-1'), not a promise. Agents should use request_to_confirm against it.",
      "type": "object",
      "required": ["type", "offering_id", "days", "freshness"],
      "properties": {
        "type": { "const": "pattern" },
        "offering_id": { "$ref": "#/$defs/Id" },
        "days": {
          "type": "array",
          "items": { "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] },
          "minItems": 1,
          "uniqueItems": true
        },
        "from_time": { "type": "string", "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
        "to_time": { "type": "string", "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
        "note": { "type": "string" },
        "freshness": { "$ref": "#/$defs/Freshness" }
      },
      "additionalProperties": false
    },

    "Policies": {
      "type": "object",
      "properties": {
        "min_lead_time_minutes": { "type": "integer", "minimum": 0 },
        "max_advance_days": { "type": "integer", "minimum": 0 },
        "cancellation": {
          "type": "object",
          "properties": {
            "free_until_minutes_before": { "type": "integer", "minimum": 0 },
            "late_fee": { "$ref": "#/$defs/Money" },
            "no_show_fee": { "$ref": "#/$defs/Money" },
            "note": { "type": "string" }
          },
          "additionalProperties": false
        },
        "deposit": {
          "type": "object",
          "required": ["required"],
          "properties": {
            "required": { "type": "boolean" },
            "amount": { "$ref": "#/$defs/Money" },
            "percent": { "type": "number", "minimum": 0, "maximum": 100 }
          },
          "additionalProperties": false
        },
        "delivery": {
          "type": "object",
          "properties": {
            "area": { "$ref": "#/$defs/ServiceArea" },
            "fee": { "$ref": "#/$defs/Money" },
            "free_over": { "$ref": "#/$defs/Money" },
            "minimum_order": { "$ref": "#/$defs/Money" },
            "days": { "type": "array", "items": { "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] }, "uniqueItems": true }
          },
          "additionalProperties": false
        },
        "payment_methods": {
          "type": "array",
          "items": { "enum": ["card_online", "card_in_person", "cash", "bank_transfer", "invoice", "lightning", "other"] },
          "uniqueItems": true
        },
        "age_restricted": { "type": "boolean", "default": false },
        "notes": { "type": "string", "maxLength": 1000 }
      },
      "additionalProperties": false
    },

    "ActionCapability": {
      "description": "Declares what an agent may do and how the business responds.",
      "type": "object",
      "required": ["action", "mode"],
      "properties": {
        "action": { "type": "string", "enum": ["hold", "book", "order", "cancel", "check_status", "enquire"] },
        "mode": {
          "type": "string",
          "enum": ["instant", "request_to_confirm"],
          "description": "instant = confirmed immediately against live data; request_to_confirm = owner confirms (e.g. one tap on WhatsApp) within confirmation_sla_minutes."
        },
        "applies_to": {
          "description": "Offering ids this capability covers. Omit to apply to all.",
          "type": "array",
          "items": { "$ref": "#/$defs/Id" }
        },
        "confirmation_sla_minutes": { "type": "integer", "minimum": 1 },
        "hold_duration_minutes": { "type": "integer", "minimum": 1 },
        "required_customer_fields": {
          "type": "array",
          "items": { "enum": ["name", "contact", "address", "party_size", "notes"] },
          "uniqueItems": true
        },
        "requires_payment": { "type": "boolean", "default": false }
      },
      "allOf": [
        { "if": { "properties": { "mode": { "const": "request_to_confirm" } } }, "then": { "required": ["confirmation_sla_minutes"] } },
        { "if": { "properties": { "action": { "const": "hold" } } }, "then": { "required": ["hold_duration_minutes"] } }
      ],
      "additionalProperties": false
    },

    "ActionRequest": {
      "description": "Runtime message: sent by an agent to perform an action. Not part of the published profile.",
      "type": "object",
      "required": ["request_id", "business_id", "action", "created_at", "agent"],
      "properties": {
        "request_id": { "type": "string", "minLength": 8, "description": "Idempotency key chosen by the agent. Retrying with the same id must not create a second booking." },
        "business_id": { "type": "string", "pattern": "^gtf_[a-z0-9]{8,32}$" },
        "action": { "type": "string", "enum": ["hold", "book", "order", "cancel", "check_status", "enquire"] },
        "reference": { "type": "string", "description": "Existing booking reference, for cancel and check_status." },
        "customer_confirmation": {
          "description": "Evidence that the customer agreed to exactly this request (items, time, price) before it was sent. Required for hold, book and order: an agent never commits a customer on its own.",
          "type": "object",
          "required": ["method", "confirmed_at"],
          "properties": {
            "method": {
              "type": "string",
              "enum": ["confirmation_page", "in_app"],
              "description": "confirmation_page: the customer tapped Confirm on a page that showed this order (give its reference). in_app: the customer approved it in their AI app, in a confirmation dialog or by an explicit yes to a summary of this request."
            },
            "confirmed_at": { "type": "string", "format": "date-time" },
            "reference": { "type": "string", "description": "The confirmation page's reference, such as a Gentifi order reference." }
          },
          "allOf": [{ "if": { "properties": { "method": { "const": "confirmation_page" } } }, "then": { "required": ["reference"] } }],
          "additionalProperties": false
        },
        "items": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["offering_id"],
            "properties": {
              "offering_id": { "$ref": "#/$defs/Id" },
              "variant_id": { "$ref": "#/$defs/Id" },
              "slot_id": { "$ref": "#/$defs/Id" },
              "quantity": { "type": "number", "exclusiveMinimum": 0 },
              "requested_time": { "type": "string", "format": "date-time", "description": "For request_to_confirm against a pattern." }
            },
            "additionalProperties": false
          }
        },
        "fulfilment": { "type": "string", "enum": ["collection", "delivery", "on_premises", "at_customer_location", "remote"] },
        "customer": {
          "description": "Minimum needed to fulfil. Prefer a relay token over raw contact details so the customer's details are not exposed to the business until needed.",
          "type": "object",
          "properties": {
            "name": { "type": "string" },
            "contact_relay_token": { "type": "string" },
            "phone": { "type": "string" },
            "email": { "type": "string", "format": "email" },
            "address": { "$ref": "#/$defs/Address" },
            "party_size": { "type": "integer", "minimum": 1 }
          },
          "additionalProperties": false
        },
        "notes": { "type": "string", "maxLength": 500 },
        "agent": {
          "type": "object",
          "required": ["name"],
          "properties": {
            "name": { "type": "string", "description": "e.g. 'Claude', 'ChatGPT', 'Gemini', 'Muse'" },
            "platform": { "type": "string" },
            "on_behalf_of_verified_user": { "type": "boolean" }
          },
          "additionalProperties": false
        },
        "created_at": { "type": "string", "format": "date-time" }
      },
      "allOf": [
        { "if": { "properties": { "action": { "enum": ["hold", "book", "order"] } } }, "then": { "required": ["items", "customer_confirmation"] } },
        { "if": { "properties": { "action": { "enum": ["cancel", "check_status"] } } }, "then": { "required": ["reference"] } }
      ],
      "additionalProperties": false
    },

    "Receipt": {
      "description": "Runtime message: the business's (or Gentifi's) answer to an ActionRequest.",
      "type": "object",
      "required": ["request_id", "status", "issued_at"],
      "properties": {
        "request_id": { "type": "string" },
        "status": {
          "type": "string",
          "enum": ["pending_confirmation", "held", "confirmed", "declined", "expired", "cancelled", "failed"]
        },
        "reference": { "type": "string", "description": "Booking/order reference shown to the customer." },
        "confirm_by": { "type": "string", "format": "date-time", "description": "For pending_confirmation: when the agent will get a final answer." },
        "hold_expires_at": { "type": "string", "format": "date-time" },
        "total": { "$ref": "#/$defs/Money" },
        "payment": {
          "type": "object",
          "properties": {
            "status": { "enum": ["not_required", "due_on_fulfilment", "awaiting", "paid", "refunded"] },
            "checkout_url": { "type": "string", "format": "uri" }
          },
          "additionalProperties": false
        },
        "message": { "type": "string", "description": "Short human-readable note to relay to the customer." },
        "decline_reason": {
          "type": "string",
          "enum": ["unavailable", "outside_service_area", "insufficient_notice", "closed", "policy", "other"]
        },
        "alternatives": {
          "description": "Offered when declined, so the agent can propose another option.",
          "type": "array",
          "items": { "$ref": "#/$defs/AvailabilityRecord" }
        },
        "issued_at": { "type": "string", "format": "date-time" }
      },
      "allOf": [
        { "if": { "properties": { "status": { "const": "pending_confirmation" } } }, "then": { "required": ["confirm_by"] } },
        { "if": { "properties": { "status": { "const": "held" } } }, "then": { "required": ["hold_expires_at", "reference"] } },
        { "if": { "properties": { "status": { "const": "confirmed" } } }, "then": { "required": ["reference"] } },
        { "if": { "properties": { "status": { "const": "declined" } } }, "then": { "required": ["decline_reason"] } }
      ],
      "additionalProperties": false
    },

    "Extensions": {
      "description": "Namespaced vertical-specific fields, e.g. {\"com.gentifi.farm\": {...}}. Keys must be reverse-domain names so extensions from different parties don't collide.",
      "type": "object",
      "propertyNames": { "pattern": "^[a-z0-9]+(\\.[a-z0-9\\-]+)+$" }
    }
  }
}
