<?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: "menu",
    properties: [
        new OA\Property(
            property: "page",
            type: "string",
            example: "gp_basic"
        ),
        new OA\Property(
            property: "nolink",
            type: "boolean",
            example: "true"
        ),
        new OA\Property(
            property: "view_inactive",
            type: "boolean",
            example: "true"
        ),
        new OA\Property(
            property: "key",
            type: "string",
            example: "gp_basic"
        ),
        new OA\Property(
            property: "title",
            type: "string",
            example: "Masterdatamanagement_Gp_Basic"
        ),
        new OA\Property(
            property: "keywords",
            type: "json",
            example: "Array of keywords for search ['gp_basic', ...]"
        ),
        new OA\Property(
            property: "id",
            type: "string",
            example: "menublock1"
        ),
        new OA\Property(
            property: "items",
            type: "json",
            example: "Array of items which have the same fields as the main object above (page, view_inactive, key, title, keywords)"
        )
    ],
    type: 'object'
)]

class Menus extends Fe_Controller
{
    
    /**
     * Endpoint to get user defined menus
     *
     * {menu_name} is the menu to load
     *
     * Possible: masterdatamanagement_md, masterdatamanagement_tech, top
     *
     * @return void
     */
    #[OA\Get(path: '/fe/menus/menus/{menu_name}', tags: ['menus'], parameters: [
        new OA\PathParameter(name: 'menu_name', description: 'The menu name to load', schema: new OA\Schema(type: 'string'), required: true, example: 'top')
    ])]
    #[OA\Response(response: '200', description: 'An array of all menu items', content: new OA\JsonContent(
        type: 'array',
        items: new OA\Items(
            ref: '#/components/schemas/menu'
        )
    ))]
    #[OA\Response(response: '404', description: 'Menu file not found', content: new OA\JsonContent(
        properties: [
            new OA\Property(property: "error", type: "string", example: "basic"),
            new OA\Property(property: "error_no", type: "integer", example: "99999"),
            new OA\Property(property: "error_func", type: "string", example: "Menus::index__get"),
            new OA\Property(property: "error_msg", type: "string", example: "Menu file not found: The menu file  .../projectadmin/failtable.xml does not exist or is not readable. - Menu_model::get_all")
        ]
    ))]
    public function index__get(): void
    {
        $request_values = $this->map_and_validate_request(
            ['menu_name'],
            'fe_menus__get'
        );

        $menu_name = $request_values['menu_name'];
        if (stripos($menu_name, 'masterdatamanagement')===0) {
            $trans_from='masterdatamanagement';
        } else {
            $trans_from='basic';
        }

        try {
            $this->load->model('Menu_model', 'menu');

            $list = $this->menu->get_all($menu_name, $trans_from);

            $this->create_response($list);
        } catch (Exception $exc) {
            basic_error('Menu file not found: '.$exc->getMessage(), self::M_JSON, __CLASS__.'::'.__FUNCTION__, 404);
        }
    }
}