{
  "$schema": "../../packages/ui/screens/screen.schema.json",
  "id": "emailhub.inbox",
  "version": 1,
  "app": "email-hub",
  "route": "/",
  "title": "Posteingang",
  "data": {
    "emails": {
      "source": "emails.list"
    }
  },
  "actions": {
    "sendAll": {
      "handler": "emails.sendAll",
      "risk": "irreversible",
      "confirm": {
        "title": {
          "$join": [
            "Alle ",
            {
              "$count": "emails.items",
              "as": "r",
              "where": {
                "$and": [
                  {
                    "$eq": [
                      {
                        "$bind": "r.status"
                      },
                      "pending"
                    ]
                  },
                  {
                    "$not": {
                      "$bind": "r.done"
                    }
                  }
                ]
              }
            },
            " vorbereiteten Antworten jetzt absenden?"
          ]
        },
        "body": "Bitte stelle sicher, dass du alle Antworten geprüft hast.",
        "consequences": {
          "$each": "emails.items",
          "as": "r",
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          },
          "map": {
            "$text": "An {r.from} <{r.fromEmail}>"
          }
        },
        "confirmLabel": "Alle senden",
        "tone": "primary"
      },
      "success": "Alle Antworten gesendet",
      "next": {
        "$join": [
          "?screen=emailhub.inbox&sent=",
          {
            "$each": "emails.items",
            "as": "r",
            "where": {
              "$and": [
                {
                  "$eq": [
                    {
                      "$bind": "r.status"
                    },
                    "pending"
                  ]
                },
                {
                  "$not": {
                    "$bind": "r.done"
                  }
                }
              ]
            },
            "map": {
              "$bind": "r.id"
            }
          },
          ",",
          {
            "$bind": "params.sent"
          },
          "&moved=",
          {
            "$bind": "params.moved"
          }
        ]
      }
    },
    "send": {
      "handler": "emails.send",
      "risk": "irreversible",
      "confirm": {
        "title": "Antwort jetzt senden?",
        "body": "Danach geschieht Folgendes:",
        "consequences": [
          "Die Antwort geht an die Absenderin oder den Absender der Anfrage.",
          "Die Anfrage wird in Gmail nach «ClaudeDone» verschoben und erscheint unter «Gesendet» und im Sende-Log."
        ],
        "confirmLabel": "Senden",
        "tone": "primary"
      },
      "success": "Antwort gesendet!",
      "next": {
        "$text": "?screen=emailhub.inbox&sent={target.id},{params.sent}&moved={params.moved}"
      }
    },
    "save": {
      "handler": "emails.saveReply",
      "risk": "safe",
      "success": "Entwurf gespeichert – für alle im Team sichtbar"
    },
    "regenerate": {
      "handler": "emails.regenerate",
      "risk": "safe",
      "success": "AI-Antwort generiert"
    },
    "template": {
      "handler": "emails.saveTemplate",
      "risk": "safe",
      "success": "Als Vorlage gespeichert"
    },
    "moveToInbox": {
      "handler": "emails.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=emailhub.inbox&sent={params.sent}&moved={target.id},{params.moved}"
      }
    },
    "refresh": {
      "handler": "emails.reload",
      "risk": "safe",
      "success": "Posteingang neu geladen"
    }
  },
  "body": [
    {
      "component": "page-header",
      "media": "phone",
      "data": {
        "title": "Posteingang"
      }
    },
    {
      "component": "list",
      "media": "phone",
      "if": {
        "$count": "emails.items",
        "as": "r",
        "where": {
          "$and": [
            {
              "$eq": [
                {
                  "$bind": "r.status"
                },
                "pending"
              ]
            },
            {
              "$not": {
                "$bind": "r.done"
              }
            }
          ]
        }
      },
      "data": {
        "label": "Offene Anfragen",
        "variant": "cards",
        "items": {
          "$each": "emails.items",
          "as": "r",
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          },
          "map": {
            "id": {
              "$bind": "r.id"
            },
            "title": {
              "$bind": "r.from"
            },
            "time": {
              "$bind": "r.time"
            },
            "timeTone": {
              "$bind": "r.timeTone"
            },
            "meta": {
              "$bind": "r.subject"
            },
            "excerpt": {
              "$bind": "r.snippet"
            },
            "tags": {
              "$each": "r.phoneTags",
              "as": "t",
              "map": {
                "label": {
                  "$bind": "t.label"
                },
                "tone": {
                  "$bind": "t.tone"
                },
                "kind": {
                  "$bind": "t.kind"
                },
                "icon": {
                  "$bind": "t.icon"
                }
              }
            },
            "href": {
              "$text": "?screen=emailhub.email&id={r.id}&sent={params.sent}&moved={params.moved}"
            }
          }
        }
      }
    },
    {
      "component": "page-header",
      "data": {
        "title": "Posteingang",
        "meta": "Schnuppertraining-Anfragen prüfen, bearbeiten und beantworten"
      },
      "media": "wide"
    },
    {
      "layout": "grid",
      "min": "12rem",
      "children": [
        {
          "component": "stat",
          "data": {
            "label": "Ausstehend",
            "value": {
              "$count": "emails.items",
              "as": "r",
              "where": {
                "$and": [
                  {
                    "$eq": [
                      {
                        "$bind": "r.status"
                      },
                      "pending"
                    ]
                  },
                  {
                    "$not": {
                      "$bind": "r.done"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "component": "stat",
          "data": {
            "label": "Beantwortet",
            "value": {
              "$count": "emails.items",
              "as": "r",
              "where": {
                "$or": [
                  {
                    "$eq": [
                      {
                        "$bind": "r.status"
                      },
                      "sent"
                    ]
                  },
                  {
                    "$bind": "r.sent"
                  }
                ]
              }
            }
          }
        }
      ],
      "media": "wide"
    },
    {
      "layout": "cluster",
      "justify": "between",
      "if": {
        "$gte": [
          {
            "$count": "emails.items",
            "as": "r",
            "where": {
              "$and": [
                {
                  "$eq": [
                    {
                      "$bind": "r.status"
                    },
                    "pending"
                  ]
                },
                {
                  "$not": {
                    "$bind": "r.done"
                  }
                }
              ]
            }
          },
          2
        ]
      },
      "children": [
        {
          "component": "alert",
          "data": {
            "tone": "neutral",
            "title": "Klicke auf eine Anfrage, um die Antwort zu prüfen und zu bearbeiten."
          }
        },
        {
          "component": "button",
          "data": {
            "label": {
              "$join": [
                "Alle senden (",
                {
                  "$count": "emails.items",
                  "as": "r",
                  "where": {
                    "$and": [
                      {
                        "$eq": [
                          {
                            "$bind": "r.status"
                          },
                          "pending"
                        ]
                      },
                      {
                        "$not": {
                          "$bind": "r.done"
                        }
                      }
                    ]
                  }
                },
                ")"
              ]
            },
            "icon": "mail",
            "action": "sendAll"
          }
        }
      ],
      "media": "wide"
    },
    {
      "layout": "stack",
      "gap": "sm",
      "children": [
        {
          "each": "emails.items",
          "as": "r",
          "node": {
            "layout": "details",
            "id": {
              "$text": "card-{r.id}"
            },
            "group": "inbox",
            "open": {
              "$eq": [
                {
                  "$bind": "r.id"
                },
                {
                  "$bind": "params.open"
                }
              ]
            },
            "title": {
              "$text": "{r.from} · {r.fromEmail}"
            },
            "meta": {
              "$bind": "r.subject"
            },
            "tags": {
              "$bind": "r.tags"
            },
            "aside": {
              "$bind": "r.dateLabel"
            },
            "children": [
              {
                "component": "alert",
                "if": {
                  "$bind": "r.lock"
                },
                "data": {
                  "tone": "warning",
                  "title": {
                    "$text": "Wird gerade von {r.lock} bearbeitet"
                  },
                  "body": "Du kannst die Antwort lesen, aber nicht ändern."
                }
              },
              {
                "layout": "grid",
                "min": "20rem",
                "children": [
                  {
                    "component": "message",
                    "data": {
                      "label": "Originalanfrage",
                      "from": {
                        "$text": "{r.from} <{r.fromEmail}>"
                      },
                      "dateLabel": {
                        "$bind": "r.dateLabel"
                      },
                      "body": {
                        "$bind": "r.body"
                      }
                    }
                  },
                  {
                    "layout": "stack",
                    "children": [
                      {
                        "component": "field",
                        "data": {
                          "id": {
                            "$text": "reply-{r.id}"
                          },
                          "name": {
                            "$text": "reply-{r.id}"
                          },
                          "label": "Vorbereitete Antwort",
                          "control": "textarea",
                          "rows": 14,
                          "value": {
                            "$bind": "r.replyDraft"
                          },
                          "hint": {
                            "$bind": "r.draftMeta"
                          },
                          "disabled": {
                            "$not": {
                              "$not": {
                                "$bind": "r.lock"
                              }
                            }
                          }
                        }
                      },
                      {
                        "component": "kv",
                        "data": {
                          "items": [
                            {
                              "term": "An",
                              "value": {
                                "$text": "{r.from} <{r.fromEmail}>"
                              }
                            }
                          ]
                        }
                      },
                      {
                        "component": "chip-group",
                        "if": {
                          "$and": [
                            {
                              "$eq": [
                                {
                                  "$bind": "r.language"
                                },
                                "de"
                              ]
                            },
                            {
                              "$not": {
                                "$bind": "r.lock"
                              }
                            }
                          ]
                        },
                        "data": {
                          "label": "Anrede – die AI schreibt die Antwort neu",
                          "name": "register",
                          "chips": [
                            {
                              "value": "du",
                              "label": "Du",
                              "pressed": {
                                "$eq": [
                                  {
                                    "$bind": "r.register"
                                  },
                                  "du"
                                ]
                              },
                              "href": {
                                "$text": "?screen=emailhub.inbox&open={r.id}&register=du&sent={params.sent}&moved={params.moved}"
                              }
                            },
                            {
                              "value": "ihr",
                              "label": "ihr",
                              "pressed": {
                                "$eq": [
                                  {
                                    "$bind": "r.register"
                                  },
                                  "ihr"
                                ]
                              },
                              "href": {
                                "$text": "?screen=emailhub.inbox&open={r.id}&register=ihr&sent={params.sent}&moved={params.moved}"
                              }
                            },
                            {
                              "value": "sie",
                              "label": "Sie",
                              "pressed": {
                                "$eq": [
                                  {
                                    "$bind": "r.register"
                                  },
                                  "sie"
                                ]
                              },
                              "href": {
                                "$text": "?screen=emailhub.inbox&open={r.id}&register=sie&sent={params.sent}&moved={params.moved}"
                              }
                            }
                          ],
                          "kind": "address"
                        }
                      },
                      {
                        "layout": "cluster",
                        "if": {
                          "$not": {
                            "$bind": "r.lock"
                          }
                        },
                        "children": [
                          {
                            "component": "button",
                            "data": {
                              "label": "Senden",
                              "variant": "primary",
                              "icon": "mail",
                              "action": "send",
                              "actionId": {
                                "$bind": "r.id"
                              }
                            }
                          },
                          {
                            "component": "button",
                            "data": {
                              "label": "Speichern",
                              "action": "save",
                              "actionId": {
                                "$bind": "r.id"
                              }
                            }
                          },
                          {
                            "component": "button",
                            "data": {
                              "label": "Neu schreiben",
                              "icon": "sparkles",
                              "action": "regenerate",
                              "actionId": {
                                "$bind": "r.id"
                              },
                              "kind": "ai"
                            }
                          },
                          {
                            "component": "button",
                            "data": {
                              "label": "Vorlage",
                              "icon": "list",
                              "action": "template",
                              "actionId": {
                                "$bind": "r.id"
                              }
                            }
                          },
                          {
                            "component": "button",
                            "data": {
                              "label": "Zurück in Posteingang",
                              "variant": "danger-ghost",
                              "icon": "arrow-left",
                              "action": "moveToInbox",
                              "actionId": {
                                "$bind": "r.id"
                              }
                            }
                          }
                        ]
                      },
                      {
                        "component": "alert",
                        "if": {
                          "$not": {
                            "$bind": "r.lock"
                          }
                        },
                        "data": {
                          "tone": "neutral",
                          "title": "Prüfe die Antwort, bevor du sie absendest. Mit «Speichern» wird der Entwurf für alle Teamkollegen sichtbar."
                        }
                      }
                    ]
                  }
                ]
              }
            ]
          },
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          }
        }
      ],
      "media": "wide"
    },
    {
      "component": "empty-state",
      "if": {
        "$not": {
          "$count": "emails.items",
          "as": "r",
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          }
        }
      },
      "data": {
        "icon": "check-circle",
        "title": "Alles erledigt!",
        "text": "Keine neuen Schnuppertraining-Anfragen vorhanden. Sobald neue Anfragen eintreffen, erscheinen sie hier automatisch. Bereits gesendete Antworten findest du unter «Gesendet».",
        "action": {
          "label": "Gesendet",
          "href": "?screen=emailhub.sent"
        }
      }
    }
  ],
  "chrome": {
    "title": "Email Hub",
    "subtitle": {
      "$if": {
        "$count": "emails.items",
        "as": "r",
        "where": {
          "$and": [
            {
              "$eq": [
                {
                  "$bind": "r.status"
                },
                "pending"
              ]
            },
            {
              "$not": {
                "$bind": "r.done"
              }
            }
          ]
        }
      },
      "then": {
        "$join": [
          {
            "$count": "emails.items",
            "as": "r",
            "where": {
              "$and": [
                {
                  "$eq": [
                    {
                      "$bind": "r.status"
                    },
                    "pending"
                  ]
                },
                {
                  "$not": {
                    "$bind": "r.done"
                  }
                }
              ]
            }
          },
          " offen"
        ]
      },
      "else": "Posteingang"
    },
    "actions": [
      {
        "icon": "refresh",
        "iconOnly": true,
        "variant": "ghost",
        "ariaLabel": "Neu laden",
        "action": "refresh"
      }
    ],
    "tabbarOnPhone": false
  }
}
