#!/bin/bash

eval "$(/usr/sbin/ucr shell)"

set -e


univention-install -y jq

url="http://localhost:${appcenter_apps_wekan_ports_8080}/wekan"
admin="wekan-admin"
password="$(cat /etc/wekan-admin.secret)"

# logon
token="$(curl -s -H "Content-type:application/json" $url/users/login -d '{ "username": "'$admin'", "password": "'$password'" }' | jq -e -r '.token')"

# list users
curl -s -H "Content-type:application/json" -H "Authorization: Bearer $token" $url/api/users
curl -s -H "Content-type:application/json" -H "Authorization: Bearer $token" $url/api/users | jq -e -r '. | .[] | .username' | grep wekan-admin

# check wekan mongodb
docker exec -i wekan-app node <<'EOF'
const { createRequire } = require('node:module');
const bundleRequire = createRequire('/build/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/_.cjs');
const { MongoClient } = bundleRequire('mongodb');
(async () => {
  const client = await MongoClient.connect(process.env.MONGO_URL);
  try {
    const settings = client.db().collection('settings');
    const res = await settings.findOne({});
    if (!res) throw new Error('WeKan settings not initialized');
    if (res.defaultAuthenticationMethod !== 'ldap' || res.displayAuthenticationMethod !== false) {
      throw new Error('UCS authentication settings were not preserved');
    }
  } finally { await client.close(); }
})().catch(error => { console.error(error); process.exitCode = 1; });
EOF
# Exactly the two app services; no importer or old MongoDB process after startup.
test "$(docker inspect -f '{{.State.Running}}' wekan-app)" = true
test "$(docker inspect -f '{{.State.Running}}' wekan-db)" = true
docker inspect -f '{{json .Config.Entrypoint}}' wekan-db | grep -q '/ferretdb'
! docker inspect wekan-ucs-migration >/dev/null 2>&1
! docker inspect wekan-ucs-migration-db >/dev/null 2>&1

echo "OK"
exit 0