#!/bin/bash
# Legt den Demo-Tenant mit Stationen an.

set -euo pipefail

PB_URL="${PB_URL:-https://api.checkflo.de}"

if [ -z "${PB_EMAIL:-}" ] || [ -z "${PB_PASSWORD:-}" ]; then
  echo "Aufruf: PB_EMAIL=... PB_PASSWORD=... ./seed-demo.sh"
  exit 1
fi

# Authentifizierung
TOKEN=$(curl -sf -X POST "$PB_URL/api/collections/_superusers/auth-with-password" \
  -H "Content-Type: application/json" \
  -d "{\"identity\":\"$PB_EMAIL\",\"password\":\"$PB_PASSWORD\"}" | jq -r '.token')

echo "✓ Eingeloggt"

# ----------------------------------------------------------
# Tenant: Musterküche GmbH
# ----------------------------------------------------------
echo "→ Erstelle Demo-Tenant..."
TENANT=$(curl -sf -X POST "$PB_URL/api/collections/tenants/records" \
  -H "Authorization: $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name":          "Musterküche GmbH",
    "slug":          "musterkueche",
    "primary_color": "#e85d04",
    "plan":          "basic",
    "active":        true
  }')
TENANT_ID=$(echo "$TENANT" | jq -r '.id')
echo "✓ Tenant: Musterküche GmbH ($TENANT_ID)"

# ----------------------------------------------------------
# Stationen
# ----------------------------------------------------------
create_station() {
  local name=$1
  local type=$2
  local min=$3
  local max=$4
  local qr_id
  qr_id=$(uuidgen | tr '[:upper:]' '[:lower:]')

  curl -sf -X POST "$PB_URL/api/collections/stations/records" \
    -H "Authorization: $TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
      \"tenant\":          \"$TENANT_ID\",
      \"name\":            \"$name\",
      \"type\":            \"$type\",
      \"target_temp_min\": $min,
      \"target_temp_max\": $max,
      \"qr_id\":           \"$qr_id\",
      \"active\":          true
    }" | jq -r '"✓ Station: \(.name) — QR: \(.qr_id)"'
}

echo "→ Erstelle Stationen..."
create_station "Kühlschrank 1 (Küche)"     "kuehlschrank"  1  7
create_station "Kühlschrank 2 (Getränke)"  "kuehlschrank"  1  8
create_station "Tiefkühlzelle"             "tiefkuehl"    -22 -18
create_station "Warmhaltebehälter"         "warmhalte"     65  80
create_station "Wochenhygiene-Check"       "hygiene"        0   0

# ----------------------------------------------------------
# Demo-User (Admin des Tenants)
# ----------------------------------------------------------
echo "→ Erstelle Demo-User..."
curl -sf -X POST "$PB_URL/api/collections/users/records" \
  -H "Authorization: $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"email\":           \"chef@musterkueche.de\",
    \"password\":        \"Demo1234567!\",
    \"passwordConfirm\": \"Demo1234567!\",
    \"name\":            \"Chef Demo\",
    \"tenant\":          \"$TENANT_ID\",
    \"role\":            \"admin\",
    \"verified\":        true
  }" | jq -r '"✓ User: \(.email)"'

echo ""
echo "  ✓ Demo-Daten angelegt:"
echo "    Tenant:  Musterküche GmbH ($TENANT_ID)"
echo "    Login:   chef@musterkueche.de / Demo1234567!"
echo "    Stationen: 5"
echo ""
echo "  QR-Scan URL: https://checkflo.de/s/{qr_id}"
echo ""
