{
  "$schema": "../../packages/ui/screens/screen.schema.json",
  "id": "emailhub.email",
  "kind": "flow",
  "version": 3,
  "app": "email-hub",
  "route": "/m/e/:id",
  "title": {
    "$bind": "email.from"
  },
  "modes": {
    "guided": false,
    "compact": true
  },
  "density": "dense",
  "sections": "cards",
  "data": {
    "email": {
      "source": "emails.get",
      "params": {
        "id": {
          "$bind": "params.id"
        }
      }
    }
  },
  "chrome": {
    "title": {
      "$bind": "email.from"
    },
    "subtitle": {
      "$bind": "email.fromEmail"
    },
    "back": {
      "label": "Posteingang",
      "href": {
        "$text": "?screen=emailhub.inbox&sent={params.sent}&moved={params.moved}"
      }
    },
    "menu": {
      "$if": {
        "$not": {
          "$bind": "email.lock"
        }
      },
      "then": {
        "label": "Weitere Aktionen",
        "items": [
          {
            "label": "Als Vorlage speichern",
            "icon": "list",
            "action": "template"
          },
          {
            "label": "Keine Anfrage: zurück in Gmail",
            "icon": "arrow-left",
            "action": "move-to-inbox",
            "danger": true
          }
        ]
      }
    },
    "tabbarOnPhone": false
  },
  "readonly": {
    "$bind": "email.lock"
  },
  "draftScope": {
    "$bind": "params.id"
  },
  "derive": {
    "reply": {
      "from": [
        "register"
      ],
      "source": "emails.draft",
      "rewritten": "Antwort neu geschrieben – deine Änderungen wurden ersetzt."
    }
  },
  "actions": {
    "send": {
      "handler": "emails.send",
      "risk": "irreversible",
      "success": {
        "$text": "Gesendet an {email.fromEmail}"
      }
    },
    "save": {
      "handler": "emails.saveReply",
      "risk": "safe",
      "success": "Entwurf gespeichert"
    },
    "template": {
      "handler": "emails.saveTemplate",
      "risk": "safe",
      "success": "Als Vorlage gespeichert"
    },
    "move-to-inbox": {
      "handler": "emails.moveToInbox",
      "risk": "irreversible",
      "confirm": {
        "title": "Keine Probetraining-Anfrage?",
        "body": "Die E-Mail geht zurück in den normalen Gmail-Posteingang und verschwindet hier.",
        "confirmLabel": "Zurück in Gmail",
        "tone": "danger"
      },
      "success": "Zurück in Gmail verschoben",
      "next": {
        "$text": "?screen=emailhub.inbox&sent={params.sent}&moved={params.id},{params.moved}"
      }
    }
  },
  "lead": [
    {
      "component": "alert",
      "if": {
        "$bind": "email.lock"
      },
      "data": {
        "tone": "neutral",
        "title": {
          "$text": "Wird gerade von {email.lock} bearbeitet"
        },
        "body": "Du kannst lesen, aber nicht ändern."
      }
    },
    {
      "component": "page-header",
      "data": {
        "variant": "sub",
        "title": {
          "$bind": "email.subject"
        },
        "meta": {
          "$bind": "email.dateLine"
        }
      }
    },
    {
      "layout": "cluster",
      "children": [
        {
          "each": "email.phoneTags",
          "as": "t",
          "node": {
            "component": "badge",
            "data": {
              "label": {
                "$bind": "t.label"
              },
              "tone": {
                "$bind": "t.tone"
              },
              "kind": {
                "$bind": "t.kind"
              },
              "icon": {
                "$bind": "t.icon"
              }
            }
          }
        }
      ]
    }
  ],
  "submit": {
    "action": "send",
    "label": "Senden",
    "icon": "mail"
  },
  "navActions": [
    {
      "action": "save",
      "label": "Speichern"
    }
  ],
  "done": {
    "href": {
      "$text": "?screen=emailhub.inbox&sent={params.id},{params.sent}&moved={params.moved}"
    }
  },
  "steps": [
    {
      "id": "message",
      "title": "Nachricht",
      "body": [
        {
          "component": "message",
          "data": {
            "bare": true,
            "body": {
              "$bind": "email.body"
            }
          }
        }
      ]
    },
    {
      "id": "reply",
      "title": "Antwort",
      "head": [
        {
          "component": "segmented",
          "if": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "email.language"
                  },
                  "de"
                ]
              },
              {
                "$not": {
                  "$bind": "email.lock"
                }
              }
            ]
          },
          "data": {
            "id": "register",
            "name": "register",
            "legend": "Anrede",
            "hideLegend": true,
            "kind": "address",
            "value": {
              "$bind": "email.register"
            },
            "options": [
              {
                "value": "du",
                "label": "Du"
              },
              {
                "value": "ihr",
                "label": "ihr"
              },
              {
                "value": "sie",
                "label": "Sie"
              }
            ]
          }
        }
      ],
      "body": [
        {
          "component": "field",
          "data": {
            "id": "reply",
            "name": "reply",
            "label": "Antwort",
            "hideLabel": true,
            "control": "textarea",
            "rows": 10,
            "required": true,
            "hint": {
              "$bind": "email.draftMeta"
            },
            "disabled": {
              "$not": {
                "$not": {
                  "$bind": "email.lock"
                }
              }
            }
          }
        },
        {
          "component": "button",
          "if": {
            "$not": {
              "$bind": "email.lock"
            }
          },
          "data": {
            "label": "Neu schreiben",
            "icon": "sparkles",
            "kind": "ai",
            "block": "always",
            "action": "flow-derive",
            "actionId": "reply"
          }
        },
        {
          "component": "kv",
          "data": {
            "items": [
              {
                "term": "An",
                "value": {
                  "$text": "{email.from} <{email.fromEmail}>"
                }
              }
            ]
          }
        }
      ]
    }
  ],
  "review": {
    "confirm": true,
    "title": {
      "$text": "Antwort jetzt an {email.from} senden?"
    },
    "intro": {
      "$text": "An: {email.from} <{email.fromEmail}>"
    },
    "backLabel": "Abbrechen"
  }
}
