#!/bin/bash
set -euo pipefail

NC_URL="${NC_URL:-https://cloud.bstein.dev}"
ADMIN_USER="${ADMIN_USER:?}"
ADMIN_PASS="${ADMIN_PASS:?}"

export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y -qq curl jq >/dev/null

run_occ() {
  runuser -u www-data -- php /var/www/html/occ "$@"
}

log() { echo "[$(date -Is)] $*"; }

log "Ensuring Nextcloud app files are present"
if [[ ! -d /var/www/html/lib && -d /usr/src/nextcloud/lib ]]; then
  rsync -a --delete \
    --exclude config \
    --exclude data \
    /usr/src/nextcloud/ /var/www/html/
fi

log "Ensuring Nextcloud permissions"
mkdir -p /var/www/html/data
chown 33:33 /var/www/html || true
chmod 775 /var/www/html || true
chown -R 33:33 /var/www/html/apps /var/www/html/custom_apps /var/www/html/data /var/www/html/config 2>/dev/null || true

log "Applying Atlas theming"
run_occ config:app:set theming name --value "Atlas Cloud"
run_occ config:app:set theming slogan --value "Unified access to Atlas services"
run_occ config:app:set theming url --value "https://cloud.bstein.dev"
run_occ config:app:set theming color --value "#0f172a"
run_occ config:app:set theming disable-user-theming --value "yes"

log "Applying Atlas Mail styling defaults"
run_occ app:install customcss >/dev/null 2>&1 || true
run_occ app:enable customcss >/dev/null 2>&1 || true
MAIL_CSS=$(cat <<'CSS'
.mail-message-body, .mail-message-body pre, .mail-message-body code, .mail-message-body table {
  font-family: "Inter", "Source Sans 3", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: var(--color-main-text);
}
.mail-message-body pre {
  background: rgba(15, 23, 42, 0.06);
  padding: 12px;
  border-radius: 8px;
}
.mail-message-body blockquote {
  border-left: 3px solid var(--color-border);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--color-text-lighter);
}
.mail-message-body img {
  max-width: 100%;
  border-radius: 6px;
}
CSS
)
run_occ config:app:set customcss css --value "${MAIL_CSS}" >/dev/null

log "Setting default quota to 250 GB"
run_occ config:app:set files default_quota --value "250 GB"

API_BASE="${NC_URL}/ocs/v2.php/apps/external/api/v1"
AUTH=(-u "${ADMIN_USER}:${ADMIN_PASS}" -H "OCS-APIRequest: true")

log "Removing existing external links"
existing=$(curl -sf "${AUTH[@]}" "${API_BASE}?format=json" | jq -r '.ocs.data[].id // empty')
for id in ${existing}; do
  curl -sf "${AUTH[@]}" -X DELETE "${API_BASE}/sites/${id}?format=json" >/dev/null || true
done

SITES=(
  "Vaultwarden|https://vault.bstein.dev"
  "Jellyfin|https://stream.bstein.dev"
  "Gitea|https://scm.bstein.dev"
  "Jenkins|https://ci.bstein.dev"
  "Harbor|https://registry.bstein.dev"
  "Vault|https://secret.bstein.dev"
  "Jitsi|https://meet.bstein.dev"
  "Grafana|https://metrics.bstein.dev"
  "Chat LLM|https://chat.ai.bstein.dev"
  "Vision|https://draw.ai.bstein.dev"
  "STT/TTS|https://talk.ai.bstein.dev"
)

log "Seeding external links"
for entry in "${SITES[@]}"; do
  IFS="|" read -r name url <<<"${entry}"
  curl -sf "${AUTH[@]}" -X POST "${API_BASE}/sites?format=json" \
    -d "name=${name}" \
    -d "url=${url}" \
    -d "lang=" \
    -d "type=link" \
    -d "device=" \
    -d "icon=" \
    -d "groups[]=" \
    -d "redirect=1" >/dev/null
done

log "Maintenance run completed"
