{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://synor.io/schemas/mining.json",
  "title": "Synor Mining SDK",
  "description": "Mining pool connection, block templates, and hashrate management",

  "$defs": {
    "MiningConfig": {
      "type": "object",
      "allOf": [{ "$ref": "common.json#/$defs/SynorConfig" }],
      "properties": {
        "stratumUrl": {
          "type": "string",
          "format": "uri",
          "description": "Stratum protocol endpoint"
        },
        "workerName": { "type": "string" }
      }
    },

    "PoolConfig": {
      "type": "object",
      "properties": {
        "url": { "type": "string", "format": "uri" },
        "username": { "type": "string" },
        "password": { "type": "string" },
        "workerName": { "type": "string" },
        "algorithm": { "$ref": "#/$defs/MiningAlgorithm" }
      },
      "required": ["url", "username"]
    },

    "MiningAlgorithm": {
      "type": "string",
      "enum": ["sha256d", "scrypt", "ethash", "randomx", "kawpow", "synor-pow"]
    },

    "StratumConnection": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "poolUrl": { "type": "string" },
        "connected": { "type": "boolean" },
        "difficulty": { "type": "number" },
        "extranonce1": { "type": "string" },
        "extranonce2Size": { "type": "integer" },
        "connectedAt": { "$ref": "common.json#/$defs/Timestamp" }
      },
      "required": ["id", "connected"]
    },

    "BlockTemplate": {
      "type": "object",
      "properties": {
        "previousBlockHash": { "$ref": "common.json#/$defs/BlockHash" },
        "height": { "type": "integer" },
        "version": { "type": "integer" },
        "bits": { "type": "string" },
        "target": { "type": "string" },
        "curTime": { "$ref": "common.json#/$defs/Timestamp" },
        "coinbaseValue": { "$ref": "common.json#/$defs/Amount" },
        "transactions": {
          "type": "array",
          "items": { "$ref": "#/$defs/TemplateTransaction" }
        },
        "merkleRoot": { "type": "string" },
        "nonceRange": { "type": "string" }
      },
      "required": ["previousBlockHash", "height", "target", "coinbaseValue"]
    },

    "TemplateTransaction": {
      "type": "object",
      "properties": {
        "data": { "type": "string" },
        "txid": { "$ref": "common.json#/$defs/TxHash" },
        "fee": { "$ref": "common.json#/$defs/Amount" },
        "weight": { "type": "integer" }
      },
      "required": ["data", "txid", "fee"]
    },

    "MinedWork": {
      "type": "object",
      "properties": {
        "jobId": { "type": "string" },
        "extranonce2": { "type": "string" },
        "nonce": { "type": "string" },
        "nTime": { "type": "string" },
        "hash": { "type": "string" }
      },
      "required": ["jobId", "extranonce2", "nonce", "nTime"]
    },

    "SubmitResult": {
      "type": "object",
      "properties": {
        "accepted": { "type": "boolean" },
        "blockHash": { "$ref": "common.json#/$defs/BlockHash" },
        "difficulty": { "type": "number" },
        "rejectReason": { "type": "string" }
      },
      "required": ["accepted"]
    },

    "Hashrate": {
      "type": "object",
      "properties": {
        "current": { "type": "number", "description": "Hashes per second" },
        "average1h": { "type": "number" },
        "average24h": { "type": "number" },
        "unit": {
          "type": "string",
          "enum": ["H/s", "KH/s", "MH/s", "GH/s", "TH/s", "PH/s"]
        }
      },
      "required": ["current", "unit"]
    },

    "MiningStats": {
      "type": "object",
      "properties": {
        "hashrate": { "$ref": "#/$defs/Hashrate" },
        "sharesAccepted": { "type": "integer" },
        "sharesRejected": { "type": "integer" },
        "blocksFound": { "type": "integer" },
        "uptime": { "$ref": "common.json#/$defs/Duration" },
        "efficiency": { "type": "number", "minimum": 0, "maximum": 100 },
        "lastShareAt": { "$ref": "common.json#/$defs/Timestamp" }
      },
      "required": ["hashrate", "sharesAccepted", "sharesRejected"]
    },

    "TimePeriod": {
      "type": "string",
      "enum": ["hour", "day", "week", "month", "all"]
    },

    "Earnings": {
      "type": "object",
      "properties": {
        "period": { "$ref": "#/$defs/TimePeriod" },
        "amount": { "$ref": "common.json#/$defs/Amount" },
        "blocks": { "type": "integer" },
        "shares": { "type": "integer" },
        "estimatedDaily": { "$ref": "common.json#/$defs/Amount" }
      },
      "required": ["period", "amount"]
    },

    "MiningDevice": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "type": {
          "type": "string",
          "enum": ["cpu", "gpu", "asic", "fpga"]
        },
        "vendor": { "type": "string" },
        "model": { "type": "string" },
        "hashrate": { "$ref": "#/$defs/Hashrate" },
        "temperature": { "type": "number" },
        "fanSpeed": { "type": "integer", "minimum": 0, "maximum": 100 },
        "power": { "type": "number", "description": "Watts" },
        "enabled": { "type": "boolean" }
      },
      "required": ["id", "name", "type"]
    },

    "DeviceConfig": {
      "type": "object",
      "properties": {
        "enabled": { "type": "boolean" },
        "intensity": { "type": "integer", "minimum": 0, "maximum": 100 },
        "powerLimit": { "type": "integer" },
        "coreClockOffset": { "type": "integer" },
        "memoryClockOffset": { "type": "integer" },
        "fanSpeed": { "type": "integer", "minimum": 0, "maximum": 100 }
      }
    },

    "ListDevicesResponse": {
      "type": "object",
      "properties": {
        "devices": {
          "type": "array",
          "items": { "$ref": "#/$defs/MiningDevice" }
        },
        "totalHashrate": { "$ref": "#/$defs/Hashrate" }
      },
      "required": ["devices"]
    }
  }
}
