{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://theuniverse.app/schemas/DASH-schema-v1.json",
  "title": "DASH.md Schema v1",
  "description": "JSON Schema for validating the YAML frontmatter of DASH.md files. All Dash modules must conform to this schema.",
  "type": "object",
  "required": ["meta", "trigger", "skin", "skill", "protocol"],
  "additionalProperties": false,

  "properties": {

    "meta": {
      "type": "object",
      "description": "Identity and discovery metadata for this Dash",
      "required": ["id", "name", "version", "category", "keywords", "author"],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "description": "Reverse-domain unique identifier (e.g. com.example.weather-dash)",
          "pattern": "^[a-z][a-z0-9]*(\\.[a-z][a-z0-9-]*){2,}$",
          "examples": ["com.theuniverse.weather", "io.example.calendar-dash"]
        },
        "name": {
          "type": "string",
          "description": "Human-readable display name",
          "minLength": 1,
          "maxLength": 64,
          "examples": ["Weather Dash", "Calendar Dash"]
        },
        "version": {
          "type": "string",
          "description": "Semantic version (MAJOR.MINOR.PATCH)",
          "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$",
          "examples": ["1.0.0", "2.3.1"]
        },
        "category": {
          "type": "string",
          "description": "Primary category for Dash Store discovery",
          "enum": [
            "utility",
            "communication",
            "productivity",
            "entertainment",
            "health",
            "finance",
            "travel",
            "social",
            "smart_home",
            "education",
            "food",
            "sports"
          ]
        },
        "keywords": {
          "type": "array",
          "description": "Search keywords for Dash Store and Orchestrator discovery",
          "minItems": 1,
          "maxItems": 20,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 32
          },
          "examples": [["날씨", "weather", "기온", "비", "forecast"]]
        },
        "icon": {
          "type": "string",
          "description": "Path to icon resource (relative to res/drawable/)",
          "pattern": "^res/drawable/[a-z][a-z0-9_]*(\\.(png|svg|xml|webp))?$",
          "examples": ["res/drawable/ic_weather.png", "res/drawable/ic_calendar"]
        },
        "author": {
          "type": "string",
          "description": "Author name or email",
          "minLength": 1,
          "maxLength": 128,
          "examples": ["developer@example.com", "The Universe Team"]
        },
        "description": {
          "type": "string",
          "description": "Brief description for store listing (plain text, max 512 chars)",
          "maxLength": 512
        },
        "min_api": {
          "type": "integer",
          "description": "Minimum Android API level required (default: 26)",
          "minimum": 26,
          "maximum": 99,
          "default": 26
        }
      }
    },

    "trigger": {
      "type": "object",
      "description": "Conditions under which the Orchestrator activates this Dash",
      "required": ["conditions"],
      "additionalProperties": false,
      "properties": {
        "conditions": {
          "type": "array",
          "description": "List of trigger conditions (OR logic — any match activates this Dash)",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/TriggerCondition"
          }
        }
      }
    },

    "skin": {
      "type": "object",
      "description": "UI rendering configuration for this Dash",
      "required": ["sizes", "theme_aware", "composable_entry"],
      "additionalProperties": false,
      "properties": {
        "sizes": {
          "type": "array",
          "description": "Supported Dash sizes (must include at least one)",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "enum": ["mini", "standard", "expanded"]
          },
          "examples": [["mini", "standard"], ["mini", "standard", "expanded"]]
        },
        "theme_aware": {
          "type": "boolean",
          "description": "Whether this Dash adapts to system dark/light theme"
        },
        "composable_entry": {
          "type": "string",
          "description": "Fully-qualified name of the root @Composable function",
          "pattern": "^[A-Z][a-zA-Z0-9]*$",
          "examples": ["WeatherDashSkin", "CalendarDashSkin"]
        },
        "default_size": {
          "type": "string",
          "description": "Initial size when first placed (must be in sizes list)",
          "enum": ["mini", "standard", "expanded"],
          "default": "standard"
        },
        "aspect_ratio": {
          "type": "object",
          "description": "Optional width:height ratio hints per size (in grid units)",
          "properties": {
            "mini":     { "type": "string", "pattern": "^\\d+:\\d+$" },
            "standard": { "type": "string", "pattern": "^\\d+:\\d+$" },
            "expanded": { "type": "string", "pattern": "^\\d+:\\d+$" }
          }
        }
      }
    },

    "skill": {
      "type": "object",
      "description": "Business logic configuration and sandboxing constraints",
      "required": ["entry"],
      "additionalProperties": false,
      "properties": {
        "entry": {
          "type": "string",
          "description": "Simple class name of the DashSkill implementation",
          "pattern": "^[A-Z][a-zA-Z0-9]*$",
          "examples": ["WeatherDashSkill", "CalendarDashSkill"]
        },
        "permissions": {
          "type": "array",
          "description": "Android permissions this Dash may request",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "enum": [
              "network",
              "location_coarse",
              "location_fine",
              "contacts",
              "calendar",
              "camera",
              "microphone",
              "storage_read",
              "storage_write",
              "notifications",
              "phone_state",
              "sms",
              "bluetooth",
              "nfc",
              "biometric",
              "health"
            ]
          },
          "examples": [["network", "location_coarse"]]
        },
        "local_state": {
          "type": "boolean",
          "description": "Whether this Dash stores persistent local state via Room",
          "default": false
        },
        "allowed_apis": {
          "type": "array",
          "description": "Platform APIs this Skill may invoke (sandboxed allow-list)",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "enum": [
              "weather_api",
              "location_service",
              "calendar_service",
              "contacts_service",
              "notification_service",
              "audio_service",
              "sensor_service",
              "network_service",
              "storage_service",
              "camera_service",
              "media_service",
              "health_service",
              "payment_service",
              "nfc_service",
              "bluetooth_service",
              "claude_api"
            ]
          }
        },
        "quota": {
          "type": "object",
          "description": "Resource usage limits (Orchestrator enforces these)",
          "additionalProperties": false,
          "properties": {
            "memory_mb": {
              "type": "integer",
              "minimum": 4,
              "maximum": 256,
              "default": 32,
              "description": "Maximum heap memory in MB"
            },
            "network_kb_per_min": {
              "type": "integer",
              "minimum": 0,
              "maximum": 10240,
              "default": 512,
              "description": "Maximum network bytes per minute (0 = offline)"
            },
            "cpu_percent": {
              "type": "integer",
              "minimum": 1,
              "maximum": 50,
              "default": 10,
              "description": "Maximum CPU % this Dash may consume while active"
            }
          }
        }
      }
    },

    "protocol": {
      "type": "object",
      "description": "Inter-Dash communication and Fusion compatibility declarations",
      "additionalProperties": false,
      "properties": {
        "publish": {
          "type": "array",
          "description": "Topics this Dash publishes to the P2P message bus",
          "uniqueItems": true,
          "items": { "$ref": "#/definitions/ProtocolTopic" }
        },
        "subscribe": {
          "type": "array",
          "description": "Topics this Dash subscribes to from the P2P message bus",
          "uniqueItems": true,
          "items": { "$ref": "#/definitions/ProtocolTopic" }
        },
        "fusible_with": {
          "type": "array",
          "description": "Category IDs or Dash IDs this Dash can fuse with",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          },
          "examples": [["map", "travel", "calendar"]]
        },
        "imports": {
          "type": "array",
          "description": "Schema file paths imported from the shared schema registry",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^schemas/[A-Za-z][A-Za-z0-9._/-]*\\.json$"
          },
          "examples": [["schemas/WeatherData.v1.json", "schemas/ScheduleEvent.v1.json"]]
        }
      }
    }
  },

  "definitions": {
    "TriggerCondition": {
      "type": "object",
      "required": ["type"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["always", "time", "location", "topic_match", "orchestrator_call"]
        }
      },
      "allOf": [
        {
          "if": { "properties": { "type": { "const": "always" } } },
          "then": {
            "additionalProperties": false,
            "properties": { "type": {} }
          }
        },
        {
          "if": { "properties": { "type": { "const": "time" } } },
          "then": {
            "required": ["start", "end"],
            "properties": {
              "type": {},
              "start": {
                "type": "string",
                "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$",
                "description": "HH:MM in 24-hour format",
                "examples": ["07:00", "22:30"]
              },
              "end": {
                "type": "string",
                "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$",
                "examples": ["09:00", "23:59"]
              },
              "days": {
                "type": "array",
                "description": "ISO weekday numbers (1=Mon, 7=Sun). Omit for all days.",
                "items": { "type": "integer", "minimum": 1, "maximum": 7 },
                "uniqueItems": true
              }
            }
          }
        },
        {
          "if": { "properties": { "type": { "const": "location" } } },
          "then": {
            "required": ["lat", "lon", "radius_m"],
            "properties": {
              "type": {},
              "lat":      { "type": "number", "minimum": -90,  "maximum": 90  },
              "lon":      { "type": "number", "minimum": -180, "maximum": 180 },
              "radius_m": { "type": "integer", "minimum": 10, "maximum": 50000 },
              "name":     { "type": "string", "description": "Human-readable place name" }
            }
          }
        },
        {
          "if": { "properties": { "type": { "const": "topic_match" } } },
          "then": {
            "required": ["topics"],
            "properties": {
              "type": {},
              "topics": {
                "type": "array",
                "minItems": 1,
                "items": { "type": "string" },
                "description": "P2P bus topics that activate this Dash when a message arrives"
              }
            }
          }
        },
        {
          "if": { "properties": { "type": { "const": "orchestrator_call" } } },
          "then": {
            "required": ["keywords"],
            "properties": {
              "type": {},
              "keywords": {
                "type": "array",
                "minItems": 1,
                "items": { "type": "string" },
                "description": "Keywords the Orchestrator matches against context signals"
              },
              "confidence": {
                "type": "number",
                "minimum": 0.0,
                "maximum": 1.0,
                "default": 0.8,
                "description": "Minimum Orchestrator confidence score to activate (0.0–1.0)"
              }
            }
          }
        }
      ]
    },

    "ProtocolTopic": {
      "type": "object",
      "required": ["topic", "schema"],
      "additionalProperties": false,
      "properties": {
        "topic": {
          "type": "string",
          "description": "Dot-separated topic namespace (e.g. weather.current, schedule.new)",
          "pattern": "^[a-z][a-z0-9]*(\\.[a-z][a-z0-9_]*)+$",
          "examples": ["weather.current", "schedule.new", "location.change"]
        },
        "schema": {
          "type": "string",
          "description": "Schema name and version from the shared registry (Name.v{N})",
          "pattern": "^[A-Z][A-Za-z0-9]+\\.v\\d+$",
          "examples": ["WeatherData.v1", "ScheduleEvent.v1"]
        }
      }
    }
  }
}
