{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://github.com/beckn/DEG/blob/main/specification/schema/ElectricityCredential/v1.1/schema.json",
    "title": "ElectricityCredential v1.1",
    "description": "Bundled JSON Schema for ElectricityCredential v1.1. EnergyResource has top-level id/type/subResources/parentResources; all other attributes live in the 'attributes' bag.",
    "type": "object",
    "required": ["@context", "id", "type", "issuer", "validFrom", "credentialSubject"],
    "properties": {
        "@context": {
            "type": "array",
            "items": { "type": "string" },
            "contains": { "const": "https://www.w3.org/ns/credentials/v2" },
            "minItems": 2
        },
        "id": {
            "type": "string",
            "format": "uri",
            "pattern": "^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
        },
        "type": {
            "type": "array",
            "items": { "type": "string" },
            "contains": { "const": "ElectricityCredential" },
            "minItems": 2
        },
        "issuer": {
            "type": "object",
            "required": ["id", "name"],
            "properties": {
                "id": { "type": "string", "format": "uri" },
                "name": { "type": "string", "minLength": 1 },
                "idRef": { "$ref": "#/$defs/IdRef" }
            }
        },
        "validFrom": { "type": "string", "format": "date-time" },
        "validUntil": { "type": "string", "format": "date-time" },
        "credentialStatus": {
            "type": "object",
            "required": ["id", "type", "statusPurpose", "statusListCredential"],
            "properties": {
                "id": { "type": "string", "format": "uri" },
                "type": { "type": "string", "const": "dediregistry" },
                "statusPurpose": { "type": "string", "enum": ["revocation", "suspension"] },
                "statusListCredential": { "type": "string", "format": "uri" }
            }
        },
        "credentialSubject": {
            "type": "object",
            "required": ["customerProfile"],
            "properties": {
                "id": { "type": "string", "format": "uri" },
                "customerProfile": { "$ref": "#/$defs/CustomerProfile" },
                "customerDetails": { "$ref": "#/$defs/CustomerDetails" }
            }
        },
        "proof": {
            "type": "object",
            "required": ["type", "created", "verificationMethod", "proofPurpose", "proofValue"],
            "properties": {
                "type": { "type": "string" },
                "created": { "type": "string", "format": "date-time" },
                "verificationMethod": { "type": "string", "format": "uri" },
                "proofPurpose": { "type": "string", "enum": ["assertionMethod", "authentication"] },
                "proofValue": { "type": "string", "minLength": 1 }
            }
        }
    },
    "$defs": {
        "IdRef": {
            "type": "object",
            "required": ["issuedBy", "subjectId"],
            "properties": {
                "issuedBy": { "type": "string", "format": "uri" },
                "subjectId": { "type": "string", "pattern": "^[A-Za-z0-9._-]+:[A-Za-z0-9X_-]+$" }
            }
        },
        "CustomerProfile": {
            "type": "object",
            "description": "Non-PII customer identity, physical assets, and tariff profiles.",
            "required": ["customerNumber", "energyResources"],
            "properties": {
                "customerNumber": { "type": "string", "minLength": 1 },
                "idRef": { "$ref": "#/$defs/IdRef" },
                "energyResources": {
                    "type": "array",
                    "minItems": 1,
                    "items": { "$ref": "#/$defs/EnergyResource" }
                },
                "consumptionProfiles": {
                    "type": "array",
                    "minItems": 1,
                    "items": { "$ref": "#/$defs/ConsumptionProfile" }
                }
            }
        },
        "ConsumptionProfile": {
            "type": "object",
            "description": "Tariff and load characteristics for one meter. meterId matches the id of a METER entry in energyResources[].",
            "required": ["meterId", "sanctionedLoadKw", "tariffCategoryCode"],
            "properties": {
                "meterId": { "type": "string", "minLength": 1 },
                "sanctionedLoadKw": { "type": "number", "minimum": 0.5, "maximum": 10000 },
                "contractMaxDemandKw": { "type": "number", "minimum": 0 },
                "sanctionedExportLoadKw": { "type": "number", "minimum": 0, "description": "Sanctioned/approved grid export limit in kW." },
                "tariffCategoryCode": { "type": "string", "minLength": 1 },
                "premisesType": { "type": "string", "enum": ["Residential", "Commercial", "Industrial", "Agricultural"] },
                "connectionType": { "type": "string", "enum": ["Single-phase", "Three-phase"] },
                "paymentMode": { "type": "string", "enum": ["PREPAID", "POSTPAID"], "description": "Indicates if the connection is prepaid or postpaid." },
                "billingCycleDay": { "type": "integer", "minimum": 1, "maximum": 31, "description": "Day of month on which the billing cycle resets." }
            }
        },
        "CustomerDetails": {
            "type": "object",
            "description": "PII — fullName is only here.",
            "required": ["fullName", "installationAddress", "serviceConnectionDate"],
            "properties": {
                "fullName": { "type": "string", "minLength": 1, "maxLength": 200 },
                "careOf": { "type": "object", "description": "Optional care-of (c/o) reference person used to uniquely identify / disambiguate a customer sharing the same fullName within a locality.", "required": ["name"], "additionalProperties": false, "properties": { "name": { "type": "string", "minLength": 1, "maxLength": 200 }, "relationship": { "type": "string", "enum": ["parent", "spouse", "guardian", "sibling", "child", "other"] } } },
                "installationAddress": { "$ref": "#/$defs/Location" },
                "serviceConnectionDate": { "type": "string", "format": "date-time" }
            }
        },
        "EnergyResource": {
            "type": "object",
            "description": "Inlined from EnergyResource/v2.0. Top-level: id, type, attributes, subResources, parentResources.",
            "additionalProperties": false,
            "properties": {
                "id": { "type": "string" },
                "type": {
                    "type": "string",
                    "enum": ["METER","DT","BUS","FEEDER","SOLAR","SOLAR_PV","WIND","HYDRO","BIOGAS","CHP","FUEL_CELL","BATTERY","BESS","EV_CHARGER","EV_V2G","SMART_HVAC","SMART_WATER_HEATER","CONTROLLABLE_LOAD","MICROGRID"]
                },
                "attributes": {
                    "type": "object",
                    "additionalProperties": true,
                    "description": "All non-topological attributes. CommonResourceAttributes (make, model, ratedPowerKw, energyCapacityKwh, telemetryProvider) plus type-specific fields (meterType, gps, location, feeder, bus, commissioningDate, storageType, …).",
                    "properties": {
                        "make": { "type": "string" },
                        "model": { "type": "string" },
                        "ratedPowerKw": { "type": "number", "minimum": 0 },
                        "energyCapacityKwh": { "type": "number", "minimum": 0 },
                        "telemetryProvider": { "type": "string" },
                        "meterType": { "$ref": "#/$defs/MeterType" },
                        "location": { "$ref": "#/$defs/Location" },
                        "commissioningDate": { "type": "string", "format": "date-time" },
                        "storageType": { "type": "string" }
                    }
                },
                "subResources": {
                    "type": "array",
                    "items": {
                        "oneOf": [
                            { "type": "string" },
                            { "$ref": "#/$defs/EnergyResource" }
                        ]
                    }
                },
                "parentResources": {
                    "type": "array",
                    "items": { "type": "string" }
                }
            }
        },
        "Location": {
            "type": "object",
            "description": "Physical location. Conforms to beckn Location/v2.0: geo (GeoJSONGeometry, required) + address (Address, optional).",
            "required": ["geo"],
            "properties": {
                "geo": { "$ref": "#/$defs/GeoJSONGeometry" },
                "address": { "$ref": "#/$defs/Address" }
            }
        },
        "Address": {
            "type": "object",
            "description": "Postal address per beckn Address/v2.0 and schema.org PostalAddress. All fields optional.",
            "properties": {
                "streetAddress":   { "type": "string", "description": "Building name/number and street." },
                "extendedAddress": { "type": "string", "description": "Apt, suite, floor, or C/O." },
                "addressLocality": { "type": "string", "description": "City or locality." },
                "addressRegion":   { "type": "string", "description": "State, region or province." },
                "addressCountry":  { "type": "string", "pattern": "^[A-Z]{2}$", "description": "ISO 3166-1 alpha-2 country code." },
                "postalCode":      { "type": "string", "description": "Postal or ZIP code." }
            },
            "additionalProperties": false
        },
        "GeoJSONGeometry": {
            "type": "object",
            "description": "GeoJSON geometry per RFC 7946 / beckn GeoJSONGeometry/v2.0. Coordinates are [longitude, latitude, (altitude?)] in EPSG:4326 (WGS-84). For a point: {type: 'Point', coordinates: [lon, lat]}.",
            "required": ["type"],
            "properties": {
                "type": {
                    "type": "string",
                    "enum": ["Point", "LineString", "Polygon", "MultiPoint", "MultiLineString", "MultiPolygon", "GeometryCollection"]
                },
                "coordinates": {
                    "type": "array",
                    "description": "Coordinates per RFC 7946. For a Point: [longitude, latitude]."
                },
                "geometries": {
                    "type": "array",
                    "items": { "$ref": "#/$defs/GeoJSONGeometry" },
                    "description": "Member geometries when type is GeometryCollection."
                },
                "bbox": {
                    "type": "array",
                    "minItems": 4, "maxItems": 4,
                    "items": { "type": "number" },
                    "description": "Bounding box [west, south, east, north] in degrees."
                }
            },
            "additionalProperties": true
        },
        "MeterType": {
            "type": "string",
            "description": "Type of electricity meter. Shared with MeterData schema.",
            "enum": ["AMR", "AMI", "Electromechanical", "Forward", "Reverse", "Bidirectional", "Prepaid", "NetMeter", "Other"]
        }
    }
}
