<?php

use api\classes\API_portalsettings;

session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_portalsettings.php";

$API_portalsettings = new API_portalsettings();

if ($API_portalsettings->request_method === 'GET') {
    $API_portalsettings->checkPermissions('admin-portalsettings', 'RO');

    $portalSettings = $API_portalsettings->getPortalSettings();
    if ($portalSettings !== null) {
        unset($portalSettings[0]["mail_smtp_pass"]);
    }

    $API_portalsettings->apiOutput($code = 200, ['success' => $portalSettings]);
} elseif ($API_portalsettings->request_method === 'PUT') {

    # Edit the portal settings of the platform

    $API_portalsettings->checkPermissions('admin-portalsettings', 'RW');

    $requiredFields = [
        'portal_uuid' => ['type' => 'uuid'],
        'portal_name' => ['type' => 'string'],
        'portal_provider_name' => ['type' => 'string'],
        'admin_auth_methods' => ['type' => 'string']
    ];

    if (count($API_portalsettings->getPortalSettings()) !== 1) {
        $API_portalsettings->apiOutput(400, ['error' => 'Invalid portal_uuid']);
    }

    $API_portalsettings->validateData($requiredFields);

    # Update the permission
    $API_portalsettings->updatePortalSettings();

}
