{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://synor.io/schemas/storage.json",
  "title": "Synor Storage SDK",
  "description": "Decentralized storage, pinning, and content retrieval",

  "$defs": {
    "StorageConfig": {
      "type": "object",
      "allOf": [{ "$ref": "common.json#/$defs/SynorConfig" }],
      "properties": {
        "gateway": {
          "type": "string",
          "format": "uri",
          "description": "IPFS gateway URL"
        },
        "pinningService": {
          "type": "string",
          "format": "uri",
          "description": "Remote pinning service URL"
        },
        "chunkSize": {
          "type": "integer",
          "minimum": 262144,
          "maximum": 1048576,
          "default": 262144,
          "description": "Chunk size in bytes for large files"
        }
      }
    },

    "ContentId": {
      "$ref": "common.json#/$defs/ContentId"
    },

    "UploadOptions": {
      "type": "object",
      "properties": {
        "pin": { "type": "boolean", "default": true },
        "wrapWithDirectory": { "type": "boolean", "default": false },
        "cidVersion": { "type": "integer", "enum": [0, 1], "default": 1 },
        "hashAlgorithm": {
          "type": "string",
          "enum": ["sha2-256", "blake3"],
          "default": "sha2-256"
        },
        "onProgress": {
          "type": "string",
          "description": "Callback function name for progress updates"
        }
      }
    },

    "UploadResponse": {
      "type": "object",
      "properties": {
        "cid": { "$ref": "#/$defs/ContentId" },
        "size": { "type": "integer" },
        "name": { "type": "string" },
        "hash": { "type": "string" }
      },
      "required": ["cid", "size"]
    },

    "DownloadOptions": {
      "type": "object",
      "properties": {
        "offset": { "type": "integer", "minimum": 0 },
        "length": { "type": "integer", "minimum": 1 },
        "onProgress": {
          "type": "string",
          "description": "Callback function name for progress updates"
        }
      }
    },

    "PinStatus": {
      "type": "string",
      "enum": ["queued", "pinning", "pinned", "failed", "unpinned"]
    },

    "Pin": {
      "type": "object",
      "properties": {
        "cid": { "$ref": "#/$defs/ContentId" },
        "status": { "$ref": "#/$defs/PinStatus" },
        "name": { "type": "string" },
        "size": { "type": "integer" },
        "createdAt": { "$ref": "common.json#/$defs/Timestamp" },
        "expiresAt": { "$ref": "common.json#/$defs/Timestamp" },
        "delegates": {
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "required": ["cid", "status"]
    },

    "PinRequest": {
      "type": "object",
      "properties": {
        "cid": { "$ref": "#/$defs/ContentId" },
        "name": { "type": "string" },
        "duration": { "$ref": "common.json#/$defs/Duration" },
        "origins": {
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "required": ["cid"]
    },

    "ListPinsRequest": {
      "type": "object",
      "properties": {
        "status": {
          "type": "array",
          "items": { "$ref": "#/$defs/PinStatus" }
        },
        "match": {
          "type": "string",
          "enum": ["exact", "iexact", "partial", "ipartial"]
        },
        "name": { "type": "string" }
      },
      "allOf": [{ "$ref": "common.json#/$defs/PaginationParams" }]
    },

    "GatewayUrl": {
      "type": "object",
      "properties": {
        "url": { "type": "string", "format": "uri" },
        "cid": { "$ref": "#/$defs/ContentId" },
        "path": { "type": "string" }
      },
      "required": ["url", "cid"]
    },

    "CarFile": {
      "type": "object",
      "properties": {
        "version": { "type": "integer", "enum": [1, 2] },
        "roots": {
          "type": "array",
          "items": { "$ref": "#/$defs/ContentId" }
        },
        "blocks": {
          "type": "array",
          "items": { "$ref": "#/$defs/CarBlock" }
        },
        "size": { "type": "integer" }
      },
      "required": ["version", "roots"]
    },

    "CarBlock": {
      "type": "object",
      "properties": {
        "cid": { "$ref": "#/$defs/ContentId" },
        "data": { "type": "string", "description": "Base64-encoded block data" },
        "size": { "type": "integer" }
      },
      "required": ["cid", "data"]
    },

    "FileEntry": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "content": { "type": "string", "description": "Base64-encoded file content" },
        "cid": { "$ref": "#/$defs/ContentId" }
      },
      "required": ["name"]
    },

    "DirectoryEntry": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "cid": { "$ref": "#/$defs/ContentId" },
        "size": { "type": "integer" },
        "type": {
          "type": "string",
          "enum": ["file", "directory"]
        }
      },
      "required": ["name", "cid", "type"]
    },

    "CreateDirectoryRequest": {
      "type": "object",
      "properties": {
        "files": {
          "type": "array",
          "items": { "$ref": "#/$defs/FileEntry" }
        }
      },
      "required": ["files"]
    },

    "ListDirectoryRequest": {
      "type": "object",
      "properties": {
        "cid": { "$ref": "#/$defs/ContentId" },
        "path": { "type": "string" }
      },
      "required": ["cid"]
    },

    "ListDirectoryResponse": {
      "type": "object",
      "properties": {
        "entries": {
          "type": "array",
          "items": { "$ref": "#/$defs/DirectoryEntry" }
        },
        "cid": { "$ref": "#/$defs/ContentId" }
      },
      "required": ["entries", "cid"]
    },

    "ImportCarRequest": {
      "type": "object",
      "properties": {
        "car": { "type": "string", "description": "Base64-encoded CAR file" },
        "pin": { "type": "boolean", "default": true }
      },
      "required": ["car"]
    },

    "ImportCarResponse": {
      "type": "object",
      "properties": {
        "roots": {
          "type": "array",
          "items": { "$ref": "#/$defs/ContentId" }
        },
        "blocksImported": { "type": "integer" }
      },
      "required": ["roots", "blocksImported"]
    },

    "StorageStats": {
      "type": "object",
      "properties": {
        "totalSize": { "type": "integer" },
        "pinCount": { "type": "integer" },
        "bandwidth": {
          "type": "object",
          "properties": {
            "upload": { "type": "integer" },
            "download": { "type": "integer" }
          }
        }
      },
      "required": ["totalSize", "pinCount"]
    }
  }
}
