{
  "id": "sales_engine",
  "label": "Ventas (con morphism)",
  "description": "Módulo conectado al manifest nakui-core 'sales': el form 'Vender' dispara el morphism que valida + mueve stock y caja con KCL post-checks reales.",
  "nakui_module_dir": "../../../crates/modules/nakui/modules/sales",
  "entities": [
    {
      "name": "Stock",
      "label": "Stock",
      "fields": [
        { "name": "id", "label": "ID", "kind": "text" },
        { "name": "sku_id", "label": "SKU", "kind": "text", "required": true },
        { "name": "ubicacion", "label": "Ubicación", "kind": "text" },
        { "name": "cantidad", "label": "Cantidad", "kind": "number", "required": true }
      ]
    },
    {
      "name": "Caja",
      "label": "Caja",
      "fields": [
        { "name": "id", "label": "ID", "kind": "text" },
        { "name": "name", "label": "Nombre", "kind": "text", "required": true },
        { "name": "currency", "label": "Moneda", "kind": "text", "default": "USD" },
        { "name": "saldo", "label": "Saldo", "kind": "number", "default": "0" }
      ]
    },
    {
      "name": "Venta",
      "label": "Venta",
      "fields": [
        { "name": "id", "label": "ID", "kind": "text" },
        { "name": "stock_id", "label": "Stock ref", "kind": "text" },
        { "name": "caja_id", "label": "Caja ref", "kind": "text" },
        { "name": "cantidad", "label": "Cantidad vendida", "kind": "number" },
        { "name": "precio_unitario", "label": "Precio unit.", "kind": "number" },
        { "name": "total", "label": "Total", "kind": "number" }
      ]
    }
  ],
  "menu": [
    { "label": "Stock", "view": "stock_list", "icon": "📦" },
    { "label": "+ Stock", "view": "stock_form", "icon": "✚" },
    { "label": "Cajas", "view": "caja_list", "icon": "💰" },
    { "label": "+ Caja", "view": "caja_form", "icon": "✚" },
    { "label": "Ventas registradas", "view": "venta_list", "icon": "🧾" },
    { "label": "Vender", "view": "vender_form", "icon": "⚡" }
  ],
  "views": {
    "stock_list": {
      "kind": "list",
      "title": "Stock",
      "entity": "Stock",
      "columns": [
        { "field": "sku_id", "label": "SKU", "weight": 1.5 },
        { "field": "ubicacion", "label": "Ubicación", "weight": 1.5 },
        { "field": "cantidad", "label": "Cantidad", "weight": 1.0 }
      ],
      "actions": [
        { "kind": "open_view", "view": "stock_form", "label": "✚ Stock" }
      ],
      "search_in": ["sku_id", "ubicacion"]
    },
    "stock_form": {
      "kind": "form",
      "title": "Nuevo stock",
      "entity": "Stock",
      "fields": [
        { "name": "id", "label": "ID interno", "kind": "text", "help": "El UUID lo genera el runtime; este field es para el campo `id` de la entity (Nakui lo usa)." },
        { "name": "sku_id", "label": "SKU", "kind": "text", "required": true },
        { "name": "ubicacion", "label": "Ubicación", "kind": "text", "required": true, "default": "loc-default" },
        { "name": "cantidad", "label": "Cantidad inicial", "kind": "number", "required": true, "default": "0" }
      ],
      "on_submit": {
        "kind": "seed_entity",
        "entity": "Stock",
        "next_view": "stock_list"
      }
    },
    "caja_list": {
      "kind": "list",
      "title": "Cajas",
      "entity": "Caja",
      "columns": [
        { "field": "name", "label": "Nombre", "weight": 2.0 },
        { "field": "currency", "label": "Moneda", "weight": 0.5 },
        { "field": "saldo", "label": "Saldo", "weight": 1.0 }
      ],
      "actions": [
        { "kind": "open_view", "view": "caja_form", "label": "✚ Caja" }
      ],
      "search_in": ["name", "currency"]
    },
    "caja_form": {
      "kind": "form",
      "title": "Nueva caja",
      "entity": "Caja",
      "fields": [
        { "name": "id", "label": "ID interno", "kind": "text" },
        { "name": "name", "label": "Nombre", "kind": "text", "required": true },
        { "name": "currency", "label": "Moneda", "kind": "text", "default": "USD" },
        { "name": "saldo", "label": "Saldo inicial", "kind": "number", "default": "0" }
      ],
      "on_submit": {
        "kind": "seed_entity",
        "entity": "Caja",
        "next_view": "caja_list"
      }
    },
    "venta_list": {
      "kind": "list",
      "title": "Ventas",
      "entity": "Venta",
      "columns": [
        { "field": "stock_id", "label": "Stock ref", "weight": 1.0 },
        { "field": "caja_id", "label": "Caja ref", "weight": 1.0 },
        { "field": "cantidad", "label": "Cant.", "weight": 0.6 },
        { "field": "precio_unitario", "label": "Precio", "weight": 0.8 },
        { "field": "total", "label": "Total", "weight": 1.0 }
      ],
      "actions": [
        { "kind": "open_view", "view": "vender_form", "label": "⚡ Vender" }
      ],
      "search_in": ["stock_id", "caja_id"]
    },
    "vender_form": {
      "kind": "form",
      "title": "Vender (morphism)",
      "entity": "Venta",
      "fields": [
        { "name": "stock_id_input", "label": "Stock", "kind": "entity_ref", "ref_entity": "Stock", "required": true, "help": "Click en un Stock de la lista para seleccionar." },
        { "name": "caja_id_input", "label": "Caja", "kind": "entity_ref", "ref_entity": "Caja", "required": true, "help": "Click en una Caja de la lista para seleccionar." },
        { "name": "venta_id", "label": "Venta UUID (idempotencia)", "kind": "text", "required": true, "help": "UUID nuevo por cada intento; mismo UUID = idempotente." },
        { "name": "cantidad", "label": "Cantidad a vender", "kind": "number", "required": true, "default": "1" },
        { "name": "precio_unitario", "label": "Precio unitario", "kind": "number", "required": true, "default": "0" },
        { "name": "timestamp", "label": "Timestamp ISO", "kind": "text", "required": true, "default": "2026-05-09T12:00:00Z" }
      ],
      "on_submit": {
        "kind": "morphism",
        "name": "vender",
        "inputs": {
          "stock": "stock_id_input",
          "caja": "caja_id_input"
        },
        "params": ["venta_id", "cantidad", "precio_unitario", "timestamp"],
        "next_view": "venta_list"
      }
    }
  }
}
