#!/bin/bash
# Scaffold a new site folder with default site.yaml
# Usage: ./add-site.sh example.de [--template person-dark] [--name "John Doe"]
set -euo pipefail

DOMAIN="${1:?Usage: ./add-site.sh <domain> [--template <template>] [--name <name>]}"
shift

TEMPLATE="custom"
NAME=""

while [[ $# -gt 0 ]]; do
    case $1 in
        --template) TEMPLATE="$2"; shift 2 ;;
        --name) NAME="$2"; shift 2 ;;
        *) echo "Unknown option: $1" >&2; exit 1 ;;
    esac
done

SITE_DIR="sites/$DOMAIN"

if [ -d "$SITE_DIR" ]; then
    echo "ERROR: $SITE_DIR already exists" >&2
    exit 1
fi

mkdir -p "$SITE_DIR"

if [ "$TEMPLATE" = "custom" ]; then
    cat > "$SITE_DIR/site.yaml" <<EOF
domain: $DOMAIN
template: custom
title: "$DOMAIN"
description: ""
EOF

    cat > "$SITE_DIR/index.html" <<EOF
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>$DOMAIN</title>
    <style>
        body { font-family: Inter, sans-serif; background: #0a0a0c; color: #e8e8ed; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; }
        h1 { font-weight: 600; }
    </style>
</head>
<body>
    <h1>$DOMAIN</h1>
</body>
</html>
EOF
else
    cat > "$SITE_DIR/site.yaml" <<EOF
domain: $DOMAIN
template: $TEMPLATE
title: "${NAME:-$DOMAIN}"
description: ""
lang: de

vars:
  name: "${NAME:-$DOMAIN}"
  role: ""
  initials: ""
  tagline: ""
  accent: "#c9a84c"
  accent_light: "rgba(201, 168, 76, 0.1)"
  font_primary: "Inter"
  font_secondary: "Inter"
  tags: []
  sections: []
  cta:
    text: "Kontakt"
    href: "mailto:info@$DOMAIN"
EOF
fi

echo "Created $SITE_DIR/"
echo "  -> Edit $SITE_DIR/site.yaml to customize"
[ "$TEMPLATE" = "custom" ] && echo "  -> Edit $SITE_DIR/index.html for content"
