{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://eep.dev/schemas/v0.1/commerce.negotiation.json",
    "title": "EEP Commerce Negotiation",
    "description": "Schema for commerce messages exchanged over EEP Network Pulse (WebSocket). Commerce messages enable bidirectional price negotiation between agents and entities. Pricing models are extensible; the protocol defines standard models but implementers can add custom ones via x- prefix.",
    "type": "object",
    "required": [
        "negotiation_id",
        "service"
    ],
    "additionalProperties": false,
    "properties": {
        "negotiation_id": {
            "type": "string",
            "description": "Unique identifier for this negotiation session. Generated by the party that sends the initial offer.",
            "pattern": "^neg_[a-zA-Z0-9]{8,32}$",
            "examples": [
                "neg_01abc2def3"
            ]
        },
        "service": {
            "type": "string",
            "description": "Identifier or name of the service being negotiated. Can be a service listing ID or a freeform description.",
            "minLength": 1,
            "maxLength": 256,
            "examples": [
                "svc_consultation_30",
                "data_feed.premium",
                "profile.full_access"
            ]
        },
        "pricing": {
            "$ref": "#/definitions/pricing",
            "description": "The pricing terms being proposed in this message."
        },
        "terms": {
            "$ref": "#/definitions/terms",
            "description": "Additional terms and conditions for this negotiation."
        },
        "reason": {
            "type": "string",
            "description": "Optional reason (used with reject or counter actions).",
            "maxLength": 1024,
            "examples": [
                "Price too high for this volume",
                "Service unavailable at requested time"
            ]
        },
        "invoice": {
            "$ref": "#/definitions/invoice",
            "description": "Invoice details (used with the 'invoice' action after service delivery)."
        },
        "receipt": {
            "$ref": "#/definitions/receipt",
            "description": "Payment receipt (used with the 'receipt' action to confirm payment)."
        },
        "metadata": {
            "type": "object",
            "description": "Optional party-defined metadata.",
            "additionalProperties": true,
            "maxProperties": 20
        },
        "pricing_mode": {
            "type": "string",
            "description": "Pricing discovery mode. fixed: static price list; negotiable: bilateral counter-offer; auction: open RFP auction. (§7.3, G19).",
            "enum": [
                "fixed",
                "negotiable",
                "auction"
            ],
            "default": "fixed"
        },
        "auction": {
            "type": "object",
            "description": "Auction configuration — only present when pricing_mode is 'auction'.",
            "required": [
                "mechanism",
                "close_time",
                "currency"
            ],
            "additionalProperties": false,
            "properties": {
                "mechanism": {
                    "type": "string",
                    "description": "Auction mechanism type. first_price: highest bidder wins at their price; vickrey: highest bidder wins at second price; reverse: lowest offer wins.",
                    "enum": [
                        "first_price",
                        "vickrey",
                        "reverse"
                    ]
                },
                "close_time": {
                    "type": "string",
                    "format": "date-time",
                    "description": "ISO 8601 datetime when bidding closes."
                },
                "reserve_price": {
                    "type": "number",
                    "description": "Minimum acceptable price (for first_price/vickrey) or maximum acceptable cost (for reverse). Optional.",
                    "minimum": 0
                },
                "currency": {
                    "type": "string",
                    "description": "ISO 4217 currency code.",
                    "pattern": "^[a-z]{3}$"
                },
                "rfp_id": {
                    "type": "string",
                    "description": "Unique RFP identifier. Carried in commerce.rfp.* CloudEvents.",
                    "pattern": "^rfp_[a-zA-Z0-9]{8,32}$"
                }
            }
        },
        "allocation_receipt": {
            "type": "object",
            "description": "W3C VC-structured Allocation Receipt issued to auction winner. Only present in commerce.rfp.closed messages. (§7.3 AllocationReceipt, G19).",
            "required": [
                "@context",
                "type",
                "issuer",
                "issuanceDate",
                "credentialSubject"
            ],
            "additionalProperties": true,
            "properties": {
                "@context": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                },
                "type": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                },
                "issuer": {
                    "type": "string",
                    "pattern": "^did:[a-z0-9]+:.+$"
                },
                "issuanceDate": {
                    "type": "string",
                    "format": "date-time"
                },
                "credentialSubject": {
                    "type": "object",
                    "required": [
                        "id",
                        "allocation_id",
                        "winning_bid",
                        "currency",
                        "valid_from"
                    ],
                    "properties": {
                        "id": {
                            "type": "string"
                        },
                        "allocation_id": {
                            "type": "string"
                        },
                        "winning_bid": {
                            "type": "number",
                            "minimum": 0
                        },
                        "currency": {
                            "type": "string",
                            "pattern": "^[a-z]{3}$"
                        },
                        "service_id": {
                            "type": "string"
                        },
                        "valid_from": {
                            "type": "string",
                            "format": "date-time"
                        },
                        "valid_until": {
                            "type": "string",
                            "format": "date-time"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "pricing": {
            "type": "object",
            "required": [
                "model",
                "currency"
            ],
            "additionalProperties": false,
            "properties": {
                "model": {
                    "type": "string",
                    "description": "Pricing model. Standard models: fixed, per_request, per_event, subscription, metered, tiered_volume. Custom models via x- prefix.",
                    "pattern": "^(fixed|per_request|per_event|subscription|metered|tiered_volume|x-[a-z][a-z0-9_-]*)$"
                },
                "amount": {
                    "type": "number",
                    "description": "Price amount. Must be non-negative. Zero means free.",
                    "minimum": 0
                },
                "currency": {
                    "type": "string",
                    "description": "ISO 4217 currency code (lowercase).",
                    "pattern": "^[a-z]{3}$",
                    "examples": [
                        "usd",
                        "eur",
                        "gbp"
                    ]
                },
                "period": {
                    "type": "string",
                    "description": "Billing period (for subscription model).",
                    "enum": [
                        "hour",
                        "day",
                        "week",
                        "month",
                        "year"
                    ]
                },
                "unit": {
                    "type": "string",
                    "description": "The billable unit (for metered model).",
                    "examples": [
                        "token",
                        "request",
                        "event",
                        "byte",
                        "minute"
                    ]
                },
                "rate": {
                    "type": "number",
                    "description": "Rate per unit (for metered model).",
                    "minimum": 0
                },
                "tiers": {
                    "type": "array",
                    "description": "Volume tiers (for tiered_volume model).",
                    "items": {
                        "type": "object",
                        "required": [
                            "up_to",
                            "rate"
                        ],
                        "properties": {
                            "up_to": {
                                "type": [
                                    "integer",
                                    "string"
                                ],
                                "description": "Upper bound of this tier. Use 'infinity' for the final tier.",
                                "examples": [
                                    1000,
                                    10000,
                                    "infinity"
                                ]
                            },
                            "rate": {
                                "type": "number",
                                "description": "Price per unit in this tier.",
                                "minimum": 0
                            }
                        }
                    }
                },
                "minimum_charge": {
                    "type": "number",
                    "description": "Optional minimum charge regardless of usage.",
                    "minimum": 0
                },
                "maximum_charge": {
                    "type": "number",
                    "description": "Optional maximum charge (price cap).",
                    "minimum": 0
                }
            }
        },
        "terms": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "delivery": {
                    "type": "string",
                    "description": "How the service will be delivered.",
                    "enum": [
                        "realtime",
                        "async",
                        "scheduled",
                        "sse",
                        "webhook",
                        "download"
                    ]
                },
                "expires_in": {
                    "type": "integer",
                    "description": "Seconds until this offer expires. After expiry, the negotiation transitions to 'expired' state.",
                    "minimum": 60,
                    "maximum": 2592000
                },
                "conditions": {
                    "type": "array",
                    "description": "Free-text conditions attached to this offer.",
                    "items": {
                        "type": "string",
                        "maxLength": 256
                    },
                    "maxItems": 10
                },
                "cancellation_policy": {
                    "type": "string",
                    "description": "Cancellation terms.",
                    "enum": [
                        "none",
                        "full_refund",
                        "partial_refund",
                        "no_refund"
                    ],
                    "default": "none"
                },
                "sla": {
                    "type": "object",
                    "description": "Optional service level agreement.",
                    "properties": {
                        "uptime_percent": {
                            "type": "number",
                            "minimum": 0,
                            "maximum": 100
                        },
                        "response_time_ms": {
                            "type": "integer",
                            "minimum": 0
                        },
                        "support_hours": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "invoice": {
            "type": "object",
            "required": [
                "invoice_id",
                "amount",
                "currency"
            ],
            "additionalProperties": false,
            "properties": {
                "invoice_id": {
                    "type": "string",
                    "description": "Unique invoice identifier.",
                    "examples": [
                        "inv_01abc"
                    ]
                },
                "amount": {
                    "type": "number",
                    "description": "Total amount due.",
                    "minimum": 0
                },
                "currency": {
                    "type": "string",
                    "pattern": "^[a-z]{3}$"
                },
                "line_items": {
                    "type": "array",
                    "description": "Breakdown of charges.",
                    "items": {
                        "type": "object",
                        "properties": {
                            "description": {
                                "type": "string"
                            },
                            "quantity": {
                                "type": "number"
                            },
                            "unit_price": {
                                "type": "number"
                            },
                            "total": {
                                "type": "number"
                            }
                        }
                    }
                },
                "payment_methods": {
                    "type": "array",
                    "description": "URLs or identifiers for payment methods.",
                    "items": {
                        "type": "string"
                    }
                },
                "due_by": {
                    "type": "string",
                    "description": "Payment deadline (ISO 8601).",
                    "format": "date-time"
                }
            }
        },
        "receipt": {
            "type": "object",
            "required": [
                "receipt_id",
                "payment_proof"
            ],
            "additionalProperties": false,
            "properties": {
                "receipt_id": {
                    "type": "string",
                    "description": "Unique receipt identifier."
                },
                "invoice_id": {
                    "type": "string",
                    "description": "The invoice this payment is for."
                },
                "payment_proof": {
                    "type": "object",
                    "description": "Proof of payment (provider-specific, opaque to protocol).",
                    "required": [
                        "type",
                        "token"
                    ],
                    "properties": {
                        "type": {
                            "type": "string"
                        },
                        "token": {
                            "type": "string"
                        },
                        "provider": {
                            "type": "string"
                        }
                    }
                },
                "paid_at": {
                    "type": "string",
                    "format": "date-time"
                }
            }
        }
    }
}