{
  "$schema": "../../packages/ui/screens/screen.schema.json",
  "id": "comms.inquiry-reply",
  "kind": "flow",
  "version": 3,
  "app": "anfragen",
  "route": "/anfragen/:id",
  "title": {
    "$text": "{inquiry.from} · {inquiry.subject}"
  },
  "requires": [
    "comms.inquiries"
  ],
  "modes": {
    "guided": false,
    "compact": true
  },
  "density": "dense",
  "data": {
    "inquiry": {
      "source": "comms.inquiries.get",
      "params": {
        "id": {
          "$bind": "params.id"
        }
      }
    },
    "inquiries": {
      "source": "comms.inquiries.list"
    }
  },
  "back": {
    "label": "Posteingang",
    "href": {
      "$text": "?screen=comms.inquiries&sent={params.sent}&moved={params.moved}"
    }
  },
  "aside": [
    {
      "component": "list",
      "data": {
        "label": "Ausstehende Anfragen",
        "items": {
          "$each": "inquiries.items",
          "as": "r",
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          },
          "map": {
            "id": {
              "$bind": "r.id"
            },
            "title": {
              "$bind": "r.name"
            },
            "time": {
              "$bind": "r.time"
            },
            "meta": {
              "$bind": "r.subject"
            },
            "excerpt": {
              "$bind": "r.preview"
            },
            "avatar": {
              "$bind": "r.initials"
            },
            "tags": {
              "$bind": "r.tags"
            },
            "href": {
              "$text": "?screen=comms.inquiry-reply&id={r.id}&sent={params.sent}&moved={params.moved}"
            },
            "current": {
              "$eq": [
                {
                  "$bind": "r.id"
                },
                {
                  "$bind": "params.id"
                }
              ]
            },
            "actions": {
              "$if": {
                "$flag": "anfragen.swipe"
              },
              "then": [
                {
                  "action": "assign",
                  "label": "Ich übernehme das",
                  "icon": "user"
                },
                {
                  "action": "close",
                  "label": "Ohne Antwort erledigen",
                  "icon": "check"
                }
              ]
            },
            "swipe": {
              "$if": {
                "$flag": "anfragen.swipe"
              },
              "then": {
                "start": {
                  "action": "assign",
                  "label": "Übernehmen",
                  "icon": "user",
                  "tone": "accent"
                },
                "end": {
                  "action": "close",
                  "label": "Erledigt",
                  "icon": "check",
                  "tone": "neutral"
                }
              }
            }
          }
        }
      }
    }
  ],
  "asideLabel": "Ausstehende Anfragen",
  "readonly": {
    "$bind": "inquiry.lock"
  },
  "draftScope": {
    "$bind": "params.id"
  },
  "derive": {
    "reply": {
      "from": [
        "register",
        "group",
        "slot"
      ],
      "source": "comms.inquiries.draft",
      "rewritten": "Antwort neu geschrieben – deine Änderungen wurden ersetzt."
    }
  },
  "actions": {
    "send": {
      "handler": "comms.sendReply",
      "risk": "irreversible",
      "success": {
        "$if": {
          "$and": [
            {
              "$flag": "anfragen.next-inquiry"
            },
            {
              "$bind": "inquiry.nextId"
            }
          ]
        },
        "then": {
          "$text": "Antwort an {inquiry.from} gesendet · nächste Anfrage"
        },
        "else": {
          "$text": "Antwort gesendet! An: {inquiry.fromEmail}"
        }
      }
    },
    "save": {
      "handler": "comms.saveReply",
      "risk": "safe",
      "success": "Entwurf gespeichert – für alle im Team sichtbar"
    },
    "template": {
      "handler": "comms.saveTemplate",
      "risk": "safe",
      "success": "Als Vorlage gespeichert"
    },
    "moveToInbox": {
      "handler": "comms.moveToInbox",
      "risk": "irreversible",
      "confirm": {
        "title": "Keine Schnuppertraining-Anfrage?",
        "body": "Die E-Mail wird zurück in den normalen Gmail-Posteingang verschoben und hier entfernt.",
        "confirmLabel": "Zurück in Posteingang",
        "tone": "danger"
      },
      "success": "E-Mail zurück in den Posteingang verschoben",
      "next": {
        "$text": "?screen=comms.inquiries&sent={params.sent}&moved={params.id},{params.moved}"
      }
    },
    "assign": {
      "handler": "comms.assignInquiry",
      "risk": "safe",
      "success": "Du bearbeitest diese Anfrage – das Team sieht es."
    },
    "close": {
      "handler": "comms.closeInquiry",
      "risk": "reversible",
      "success": "Ohne Antwort erledigt"
    }
  },
  "submit": {
    "action": "send",
    "label": "Senden"
  },
  "done": {
    "href": {
      "$if": {
        "$and": [
          {
            "$flag": "anfragen.next-inquiry"
          },
          {
            "$bind": "inquiry.nextId"
          }
        ]
      },
      "then": {
        "$text": "?screen=comms.inquiry-reply&id={inquiry.nextId}&sent={params.id},{params.sent}&moved={params.moved}"
      },
      "else": {
        "$text": "?screen=comms.inquiries&sent={params.id},{params.sent}&moved={params.moved}"
      }
    }
  },
  "steps": [
    {
      "id": "inquiry",
      "title": "Originalanfrage",
      "help": "Die Anfrage, wie sie eingegangen ist. Die Stichworte darüber hat Club Hub aus der Nachricht erkannt (Gruppe, Personen, Sprache, Anrede).",
      "body": [
        {
          "component": "alert",
          "if": {
            "$bind": "inquiry.lock"
          },
          "data": {
            "tone": "warning",
            "title": {
              "$text": "Wird gerade von {inquiry.lock} bearbeitet"
            },
            "body": "Du kannst die Antwort lesen, aber nicht ändern."
          }
        },
        {
          "layout": "cluster",
          "children": [
            {
              "each": "inquiry.tags",
              "as": "t",
              "node": {
                "component": "badge",
                "data": {
                  "label": {
                    "$bind": "t.label"
                  },
                  "tone": {
                    "$bind": "t.tone"
                  }
                }
              }
            }
          ]
        },
        {
          "component": "message",
          "data": {
            "label": "Nachricht",
            "from": {
              "$text": "{inquiry.from} <{inquiry.fromEmail}>"
            },
            "dateLabel": {
              "$bind": "inquiry.dateLabel"
            },
            "subject": {
              "$bind": "inquiry.subject"
            },
            "body": {
              "$bind": "inquiry.body"
            },
            "clamp": 7
          }
        }
      ]
    },
    {
      "id": "reply",
      "title": "Vorbereitete Antwort",
      "help": "Die AI hat die Antwort mit den Trainingszeiten der erkannten Gruppe vorbereitet. Du / ihr / Sie schreibt sie in dieser Anrede neu, «Neu schreiben» erstellt eine neue Version. «Speichern» macht den Entwurf für alle im Team sichtbar.",
      "body": [
        {
          "layout": "grid",
          "min": "14rem",
          "if": {
            "$and": [
              {
                "$flag": "anfragen.trial-date"
              },
              {
                "$not": {
                  "$bind": "inquiry.lock"
                }
              }
            ]
          },
          "children": [
            {
              "component": "field",
              "data": {
                "id": "group",
                "name": "group",
                "label": "Gruppe",
                "control": "select",
                "required": true,
                "hint": {
                  "$bind": "inquiry.groupHint"
                },
                "value": {
                  "$bind": "inquiry.suggestedGroup"
                },
                "options": {
                  "$each": "trial.groupOptions",
                  "as": "o",
                  "map": {
                    "value": {
                      "$bind": "o.value"
                    },
                    "label": {
                      "$bind": "o.label"
                    }
                  }
                }
              }
            },
            {
              "each": "trial.groups",
              "as": "g",
              "node": {
                "component": "field",
                "if": {
                  "$eq": [
                    {
                      "$bind": "values.group"
                    },
                    {
                      "$bind": "g.label"
                    }
                  ]
                },
                "data": {
                  "id": {
                    "$text": "slot-{g.key}"
                  },
                  "name": "slot",
                  "label": "Probetraining",
                  "control": "select",
                  "required": true,
                  "hint": "Aus dem Trainingskalender",
                  "value": {
                    "$bind": "g.suggested"
                  },
                  "options": {
                    "$each": "g.slots",
                    "as": "s",
                    "map": {
                      "value": {
                        "$bind": "s.label"
                      },
                      "label": {
                        "$if": {
                          "$bind": "s.closed"
                        },
                        "then": {
                          "$text": "{s.label} – Ferien"
                        },
                        "else": {
                          "$bind": "s.label"
                        }
                      },
                      "disabled": {
                        "$bind": "s.closed"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "component": "segmented",
          "if": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "inquiry.language"
                  },
                  "de"
                ]
              },
              {
                "$not": {
                  "$bind": "inquiry.lock"
                }
              }
            ]
          },
          "data": {
            "id": "register",
            "name": "register",
            "legend": "Anrede",
            "kind": "address",
            "value": {
              "$bind": "inquiry.register"
            },
            "options": [
              {
                "value": "du",
                "label": "Du"
              },
              {
                "value": "ihr",
                "label": "ihr"
              },
              {
                "value": "sie",
                "label": "Sie"
              }
            ]
          }
        },
        {
          "component": "field",
          "data": {
            "id": "reply",
            "name": "reply",
            "label": "Antwort",
            "control": "textarea",
            "rows": 14,
            "required": true,
            "hint": {
              "$bind": "inquiry.draftMeta"
            },
            "disabled": {
              "$not": {
                "$not": {
                  "$bind": "inquiry.lock"
                }
              }
            }
          }
        },
        {
          "component": "kv",
          "data": {
            "items": [
              {
                "term": "An",
                "value": {
                  "$text": "{inquiry.from} <{inquiry.fromEmail}>"
                }
              }
            ]
          }
        },
        {
          "component": "switch",
          "if": {
            "$and": [
              {
                "$flag": "anfragen.trial-date"
              },
              {
                "$not": {
                  "$bind": "inquiry.lock"
                }
              }
            ]
          },
          "data": {
            "id": "remind",
            "name": "remind",
            "label": "Am Vortag erinnern",
            "hint": "Kurze E-Mail mit Adresse und was mitbringen.",
            "checked": true
          }
        },
        {
          "layout": "cluster",
          "if": {
            "$not": {
              "$bind": "inquiry.lock"
            }
          },
          "children": [
            {
              "component": "button",
              "data": {
                "label": "Speichern",
                "action": "save",
                "actionId": {
                  "$bind": "inquiry.id"
                }
              }
            },
            {
              "component": "button",
              "data": {
                "label": "Neu schreiben",
                "icon": "sparkles",
                "kind": "ai",
                "action": "flow-derive",
                "actionId": "reply"
              }
            },
            {
              "component": "button",
              "data": {
                "label": "Vorlage",
                "icon": "list",
                "action": "template",
                "actionId": {
                  "$bind": "inquiry.id"
                }
              }
            },
            {
              "component": "button",
              "data": {
                "label": "Zurück in Posteingang",
                "variant": "danger-ghost",
                "icon": "arrow-left",
                "action": "moveToInbox",
                "actionId": {
                  "$bind": "inquiry.id"
                }
              }
            }
          ]
        },
        {
          "component": "alert",
          "if": {
            "$not": {
              "$bind": "inquiry.lock"
            }
          },
          "data": {
            "tone": "neutral",
            "title": "Tipp: Prüfe die Antwort, bevor du sie absendest. Mit «Speichern» wird der Entwurf für alle Teamkollegen sichtbar."
          }
        }
      ]
    }
  ],
  "review": {
    "confirm": true,
    "title": {
      "$text": "Antwort jetzt an {inquiry.from} senden?"
    },
    "intro": {
      "$text": "An: {inquiry.from} <{inquiry.fromEmail}>"
    },
    "backLabel": "Abbrechen",
    "summary": [
      {
        "component": "message",
        "if": {
          "$flag": "anfragen.review-effects"
        },
        "data": {
          "direction": "out",
          "label": "So geht die Antwort raus",
          "from": "Taekwondo Bern <training@taekwondobern.ch>",
          "to": {
            "$text": "{inquiry.from} <{inquiry.fromEmail}>"
          },
          "subject": {
            "$text": "Re: {inquiry.subject}"
          },
          "body": {
            "$if": {
              "$bind": "values.reply"
            },
            "then": {
              "$bind": "values.reply"
            },
            "else": "(leer)"
          }
        }
      }
    ],
    "effectsTitle": "Was passiert, wenn du sendest:",
    "effects": {
      "$if": {
        "$flag": "anfragen.review-effects"
      },
      "then": {
        "$if": {
          "$flag": "anfragen.trial-date"
        },
        "then": [
          {
            "$text": "Die Antwort geht an {inquiry.fromEmail}."
          },
          {
            "$text": "{inquiry.trialLabel} ist für {values.slot} als Probetraining notiert – das Trainingsteam sieht es."
          },
          {
            "$if": {
              "$bind": "values.remind"
            },
            "then": "Am Vortag geht eine Erinnerung raus.",
            "else": "Es geht keine Erinnerung raus."
          },
          "Die Anfrage erscheint unter «Gesendet» und im Sende-Log."
        ],
        "else": [
          {
            "$text": "Die Antwort geht an {inquiry.fromEmail}."
          },
          "Die Anfrage wird in Gmail nach «ClaudeDone» verschoben und erscheint unter «Gesendet» und im Sende-Log."
        ]
      },
      "else": []
    }
  }
}
