<?php
/**
 * CodeIgniter Controller
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Stefan Roth
 * @copyright 2009 untermStrich
 * @package REST_controllers
 * @version $Id$
 */

require_once APPPATH . 'us_libraries/Fe_Controller.php';

require_once APPPATH . 'us_libraries/vendor/autoload.php';
use OpenApi\Attributes as OA;


#[OA\Schema(
    schema: "rightInfo",
    properties: [
        new OA\Property(
            property: "id",
            type: "int",
            example: "0"
        ),
        new OA\Property(
            property: "name",
            type: "string",
            example: "Unternehmer Plus"
        )
    ],
    type: 'object'
)]

class Basic extends Fe_Controller
{
    
    /**
     * Endpoint to logout user
     *
     * @return void
     */
    #[OA\PUT(path: '/fe/basic/logout/{xjxcookie}', tags: ['basic'], parameters: [
        new OA\PathParameter(name: 'xjxcookie', description: 'XSRF cookie - Get from ussettings.xsrf', schema: new OA\Schema(type: 'string'), required: true)])
    ]
    #[OA\Response(response: '200', description: 'OK', content: new OA\JsonContent(
        type: 'array',
        items: new OA\Items(
            ref: '#/components/schemas/ok'
        )
    ))]
    #[OA\Response(response: '500', description: 'XSRF attempt blocked - Wrong xjxcookie')]
    public function logout__put(): void
    {
        $this->map_and_validate_request(['xjxcookie'], 'fe_basic_logout__put');

        $validation_ok = $this->form_validation->run('fe_basic_logout__put');
        if ($validation_ok === false) {
            basic_error(
                validation_errors(),
                self::M_JSON,
                __CLASS__.'::'.__FUNCTION__,
                400 // Bad Request
            );
        }
        
        $this->login->restrict_xsrf($this->input->post('xjxcookie'), self::M_REST);

        $this->auth->do_logout();
        
        $this->create_response(["OK"]);
    }
    
    /**
     * Endpoint to get user rights
     *
     * 0 => Entrepreneur plus<br>
     * 1 => Entrepreneur<br>
     * 2 => Management<br>
     * 3 => Staff<br>
     * 99 => Blocked<br>
     * 100 => Module not bought
     * 
     * @return void
     */
    #[OA\GET(path: '/fe/basic/login_rights', tags: ['basic'])
    ]
    #[OA\Response(response: '200', description: 'All access right', content: new OA\JsonContent(
        properties: [
            new OA\Property(property: "projectadmin", type: "integer", example: "0"),
            new OA\Property(property: "staffadmin", type: "integer", example: "0"),
            new OA\Property(property: "addressesandcontacts", type: "integer", example: "0"),
            new OA\Property(property: "masterdatamanagement", type: "integer", example: "0"),
            new OA\Property(property: "workinput", type: "integer", example: "0"),
            new OA\Property(property: "timelogging", type: "integer", example: "0"),
            new OA\Property(property: "feeadmin", type: "integer", example: "0"),
            new OA\Property(property: "externalexpenses", type: "integer", example: "0"),
            new OA\Property(property: "projectevaluation", type: "integer", example: "0"),
            new OA\Property(property: "projectdeskpanel", type: "integer", example: "0"),
            new OA\Property(property: "calculateaccordingexpenditure", type: "integer", example: "1"),
            new OA\Property(property: "opentasks", type: "integer", example: "0"),
            new OA\Property(property: "personalappointmentplaner", type: "integer", example: "0"),
            new OA\Property(property: "holidayadmin", type: "integer", example: "0"),
            new OA\Property(property: "resourceplanner", type: "integer", example: "0"),
            new OA\Property(property: "correspondence", type: "integer", example: "0"),
            new OA\Property(property: "correspondence__bought", type: "integer", example: "1"),
            new OA\Property(property: "drawingandproductdirectory", type: "integer", example: "0"),
            new OA\Property(property: "drawingandproductinandout", type: "integer", example: "0"),
            new OA\Property(property: "mailinboxandoutbox", type: "integer", example: "0"),
            new OA\Property(property: "datacollection", type: "integer", example: "0"),
            new OA\Property(property: "whatiswhere", type: "integer", example: "0"),
            new OA\Property(property: "mobilesynchronisation", type: "integer", example: "1"),
            new OA\Property(property: "logoutusers", type: "integer", example: "0"),
            new OA\Property(property: "reportdesign", type: "integer", example: "0"),
            new OA\Property(property: "restservices", type: "integer", example: "1")
        ]
    ))]
    public function login_rights__get(): void
    {
        $rights = $this->login->get_rights(true);
        
        $this->create_response($rights);
    }
    
    /**
     * Endpoint to get user information
     *
     * @return void
     */
    #[OA\GET(path: '/fe/basic/login_user', tags: ['basic'])
    ]
    #[OA\Response(response: '200', description: 'An array with information about the current logged in user', content: new OA\JsonContent(
        properties: [
            new OA\Property(property: "staff_name", type: "string", example: "Ing. Thorsten Markus Köhler DI(FH)"),
            new OA\Property(property: "staff_id", type: "integer", example: "1"),
            new OA\Property(property: "username", type: "string", example: "ingthmak"),
            new OA\Property(property: "project_manager_id", type: "integer", example: "0"),
            new OA\Property(property: "is_project_manager", type: "integer", example: "0"),
            new OA\Property(property: "respect_project_manager", type: "integer", example: "0"),
            new OA\Property(property: "division_manager_id", type: "integer", example: "1"),
            new OA\Property(property: "is_division_manager", type: "integer", example: "1"),
            new OA\Property(property: "respect_division_manager", type: "integer", example: "0"),
            new OA\Property(property: "allow_staff_even_if_not_by_project_manager", type: "integer", example: "0"),
            new OA\Property(property: "allow_staff_even_if_not_by_division_manager", type: "integer", example: "0"),
            new OA\Property(property: "allow_own_project_if_other_project_manager", type: "integer", example: "0"),
            new OA\Property(property: "allow_own_project_if_other_division_manager", type: "integer", example: "0"),
            new OA\Property(property: "multitenancy_use", type: "boolean", example: "true"),
            new OA\Property(property: "multitenancy_is_superuser", type: "boolean", example: "true"),
            new OA\Property(property: "multitenancy_user_company_id", type: "integer", example: "1")
        
        ]
    ))]
    public function login_user__get(): void
    {
        $userinfo = $this->login->get('userinfo');
        $multitenancy = $this->multitenancy->get_info();
        $userinfo['multitenancy_use'] = $multitenancy->getUse();
        $userinfo['multitenancy_is_superuser'] = $multitenancy->getIs_superuser();
        $userinfo['multitenancy_user_company_id'] = $multitenancy->getUser_company_id();
        
        $this->create_response($userinfo);
    }
    
    /**
     * Endpoint to get all possible rights
     *
     * @return void
     */
    #[OA\GET(path: '/fe/basic/right_list', tags: ['basic'])
    ]
    #[OA\Response(response: '200', description: 'An array of all possible rights, known by the system', content: new OA\JsonContent(
        items: new OA\Items(ref: '#/components/schemas/rightInfo'),
        type: 'array'
    ))]
    public function right_list__get(): void
    {
        $rights = $this->login->get_right_list(true);
        
        $this->create_response($rights);
    }
    

    
}