#!/bin/sh

# Copyright (C) 2022 Univention GmbH <http://www.univention.de/>
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

#
# Script that runs in docker host before installing the app.
#

BFF_UI_USERS_CONFIG_PATH="/etc/ucsschool/bff-ui-users"
USER_DETAIL_RESPONSE_MODEL_CONFIG_PATH="$BFF_UI_USERS_CONFIG_PATH/user_detail_response_model_config.json"

if ! [ -e "$BFF_UI_USERS_CONFIG_PATH" ]; then
    mkdir -pv -m 755 "$BFF_UI_USERS_CONFIG_PATH"

fi

if ! [ -f "$USER_DETAIL_RESPONSE_MODEL_CONFIG_PATH" ]; then
    cat << EOF > $USER_DETAIL_RESPONSE_MODEL_CONFIG_PATH
{
    "url": "",
    "allowedActions": [
        "save"
    ],
    "pages": [
        {
            "label": "General",
            "name": "general",
            "fieldsets": [
                {
                    "label": "Basic settings",
                    "name": "general__basic",
                    "rows": [
                        [
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "firstname",
                                    "label": "First name",
                                    "required": false,
                                    "access": "write"
                                }
                            },
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "lastname",
                                    "label": "Last name",
                                    "required": true,
                                    "access": "write"
                                }
                            }
                        ],
                        [
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "divisNameAffix",
                                    "label": "Name affix",
                                    "required": false
                                }
                            },
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "name",
                                    "label": "Username",
                                    "required": true,
                                    "access": "write"
                                }
                            }
                        ],
                        [{
                            "type": "UInputPassword",
                            "props": {
                                "name": "password",
                                "label": "Password",
                                "required": false,
                                "access": "write"
                            }
                        }],
                        [{
                            "type": "UInputText",
                            "props": {
                                "name": "email",
                                "label": "Primary mail address",
                                "required": false,
                                "access": "write"
                            }
                        }],
                        [{
                            "type": "UInputCheckbox",
                            "props": {
                                "label": "Disabled",
                                "name": "disabled",
                                "required": false,
                                "access": "write"
                            }
                        }]
                    ]
                },
                {
                    "label": "Personal information",
                    "name": "general__personalInformation",
                    "rows": [
                        [{
                            "type": "UInputText",
                            "props": {
                                "name": "divisNickname",
                                "label": "Nickname",
                                "required": false,
                                "access": "write"
                            }
                        }],
                        [{
                            "type": "UInputText",
                            "props": {
                                "name": "displayName",
                                "label": "Display name",
                                "required": false,
                                "access": "write"
                            }
                        }],
                        [{
                            "type": "UInputDate",
                            "props": {
                                "name": "birthday",
                                "label": "Birthday",
                                "required": false,
                                "access": "write"
                            }
                        }],
                        [{
                            "type": "UInputText",
                            "props": {
                                "name": "e-mail",
                                "label": "E-mails",
                                "required": false
                            }
                        }]
                    ]
                },
                {
                    "label": "UCS@school",
                    "name": "general__ucs@school",
                    "rows": [
                        [{
                            "type": "USelect",
                            "props": {
                                "name": "school",
                                "label": "Primary school",
                                "options": [],
                                "required": true
                            }
                        }],
                        [{
                            "type": "UMultiSelect",
                            "props": {
                                "name": "schools",
                                "label": "Schools",
                                "options": [],
                                "required": true
                            }
                        }],
                        [{
                            "type": "UMultiSelect",
                            "props": {
                                "name": "roles",
                                "label": "Roles",
                                "options": [],
                                "required": true
                            }
                        }],
                        [
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "sourceUid",
                                    "label": "Source UID",
                                    "required": false
                                }
                            },
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "recordUid",
                                    "label": "Record UID",
                                    "required": false
                                }
                            }
                        ],
                        [{
                            "type": "UInputDate",
                            "props": {
                                "name": "ucsschoolPurgeTimestamp",
                                "label": "Scheduled deletion date",
                                "required": false
                            }
                        }]
                    ]
                }
            ]
        },
        {
            "label": "Account",
            "name": "account",
            "fieldsets": [
                {
                    "label": "General",
                    "name": "account__general",
                    "rows": [
                        [{
                            "type": "UInputDate",
                            "props": {
                                "name": "creationDate",
                                "label": "Creation date",
                                "required": false
                            }
                        }],
                        [{
                            "type": "UInputDate",
                            "props": {
                                "name": "lastChange",
                                "label": "Date of last change",
                                "required": false
                            }
                        }]
                    ]
                },
                {
                    "label": "Deactivation",
                    "name": "account__deactivation",
                    "rows": [
                        [{
                            "type": "UInputDate",
                            "props": {
                                "name": "expirationDate",
                                "label": "Expiration date",
                                "required": false
                            }
                        }],
                        [{
                            "type": "UInputCheckbox",
                            "props": {
                                "name": "disabled",
                                "label": "Disabled",
                                "required": false
                            }
                        }]
                    ]
                },
                {
                    "label": "Locked login",
                    "name": "account__lockedLogin",
                    "rows": [
                        [{
                            "type": "UInputCheckbox",
                            "props": {
                                "name": "pwdChangeNextLogin",
                                "label": "User has to change password on next login",
                                "required": false
                            }
                        }]
                    ]
                },
                {
                    "label": "Activation",
                    "name": "account__activation",
                    "rows": [
                        [{
                            "type": "UInputDate",
                            "props": {
                                "name": "accountActivationDate",
                                "label": "Activation date",
                                "required": false
                            }
                        }]
                    ]
                },
                {
                    "label": "Password recovery",
                    "name": "account__passwordRecovery",
                    "rows": [
                        [{
                            "type": "UInputText",
                            "props": {
                                "name": "PasswordRecoveryEmail",
                                "label": "Password recovery e-mail",
                                "required": false
                            }
                        }],
                        [{
                            "type": "UInputCheckbox",
                            "props": {
                                "name": "PasswordRecoveryEmailVerified",
                                "label": "Is the password recovery e-mail verified?",
                                "required": false
                            }
                        }]
                    ]
                },
                {
                    "label": "WLAN",
                    "name": "account__wlan",
                    "rows": [
                        [{
                            "type": "UInputCheckbox",
                            "props": {
                                "name": "networkAccess",
                                "label": "WLAN (Radius) usage",
                                "required": false
                            }
                        }]
                    ]
                },
                {
                    "label": "SAML settings",
                    "name": "account__samlSettings",
                    "rows": [
                        [{
                            "type": "USelect",
                            "props": {
                                "name": "serviceprovider",
                                "label": "SAML service provider",
                                "options": [],
                                "required": false
                            }
                        }]
                    ]
                }
            ]
        }
    ]
}
EOF
    echo "Created $USER_DETAIL_RESPONSE_MODEL_CONFIG_PATH with the default values."
fi

USER_CREATE_RESPONSE_MODEL_CONFIG_PATH="$BFF_UI_USERS_CONFIG_PATH/user_create_response_model_config.json"

if ! [ -f "$USER_CREATE_RESPONSE_MODEL_CONFIG_PATH" ]; then
    cat << EOF > $USER_CREATE_RESPONSE_MODEL_CONFIG_PATH
{
    "url": "",
    "allowedActions": ["save"],
    "pages": [
        {
            "label": "General",
            "name": "general",
            "fieldsets": [
                {
                    "label": "Basic settings",
                    "name": "general__basicSettings",
                    "rows": [
                        [
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "firstname",
                                    "label": "First name",
                                    "required": true
                                }
                            },
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "lastname",
                                    "label": "Last name",
                                    "required": true
                                }
                            }
                        ],
                        [
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "name",
                                    "label": "Username",
                                    "required": true
                                }
                            },
                            {
                                "type": "UInputDate",
                                "props": {
                                    "name": "birthday",
                                    "label": "Birthday",
                                    "required": false
                                }
                            }
                        ],
                        [
                            {
                                "type": "UInputText",
                                "props": {
                                    "name": "primaryMail",
                                    "label": "Primary mail address",
                                    "required": false
                                }
                            }
                        ],
                        [
                            {
                                "type": "UInputPassword",
                                "props": {
                                    "name": "password",
                                    "label": "Password",
                                    "required": false
                                }
                            }
                        ],
                        [
                            {
                                "type": "UInputCheckbox",
                                "props": {
                                    "label": "Disabled",
                                    "name": "disabled",
                                    "required": false
                                }
                            }
                        ]
                    ]
                },
                {
                    "label": "Account",
                    "name": "general__account",
                    "rows": [
                        [
                            {
                                "type": "UInputDate",
                                "props": {
                                    "name": "accountActivationDate",
                                    "label": "Activation date",
                                    "required": false
                                }
                            }
                        ],
                        [
                            {
                                "type": "UInputDate",
                                "props": {
                                    "name": "expirationDate",
                                    "label": "Expiration date",
                                    "required": false
                                }
                            }
                        ]
                    ]
                },
                {
                    "label": "UCS@school",
                    "name": "ucs@school",
                    "rows": [
                        [
                            {
                                "type": "USelect",
                                "props": {
                                    "name": "school",
                                    "label": "Primary school",
                                    "required": true
                                }
                            }
                        ],
                        [
                            {
                                "type": "UMultiSelect",
                                "props": {
                                    "name": "schoolClasses",
                                    "label": "School classes",
                                    "required": true
                                }
                            }
                        ],
                        [
                            {
                                "type": "USelect",
                                "props": {
                                    "name": "role",
                                    "label": "Role",
                                    "required": true
                                }
                            }
                        ]
                    ]
                }
            ]
        }
    ]
}
EOF
    echo "Created $USER_CREATE_RESPONSE_MODEL_CONFIG_PATH with the default values."
fi