<?php

namespace EasyRestaurantManager\Http\Controllers;

use EasyRestaurantManager\Http\Requests\StoreMenuRequest;
use EasyRestaurantManager\Resources\MenuResource;
use EasyRestaurantManager\Services\Arr;
use WP_REST_Request;

class MenuController
{
    public static function storeMenu(WP_REST_Request $request)
    {
        $data = $request->get_param('data');
        $id = Arr::get($data, 'id');
       
        $validatedRequest = new StoreMenuRequest($request);
        if ($validatedRequest->fails()) {
            return rest_ensure_response([
                'message' => 'Validation failed',
                'errors'  => $validatedRequest->errors()
            ], 400);
        };

        if ($id) {
            $res = MenuResource::updateMenu($data);
   
            if ($res === false) {
                return rest_ensure_response([
                    'message' => 'Failed to update menu'
                ], 500);
            }
            return rest_ensure_response([
                'message' => 'Menu updated successfully',
                'menu_data' => $res
            ]);
        }
    
        $res = MenuResource::store($validatedRequest->validated());
        if (!$res) {
            return rest_ensure_response([
                'message' => 'Failed to create menu'
            ], 500);
        }
        return rest_ensure_response([
            'message' => 'Menu created successfully',
            'menu_data' => $res
        ]);
    }

    public static function getMenu()
    {
        $res = MenuResource::getMenu();
       
        if (!$res) {
            return rest_ensure_response([
                'message' => 'No menu found'
            ], 500);
        }

        $menus_with_decoded_data = [];
        foreach ($res as $menu) {
            $menu->portion_sizes = json_decode($menu->portion_sizes, true); 
            $menu->addons = json_decode($menu->addons, true); 
            $menus_with_decoded_data[] = $menu;
        }
    

        return rest_ensure_response([
            'message' => 'menu get successfully',
            'menus' => $menus_with_decoded_data,
            'latest_menu' => array_slice($menus_with_decoded_data, 0, 5),
        ]);
    }

    public static function getMenusWithCategory() {
        $menus = self::getMenu();
        // foreach($$menus->data['menus'] as $menu) {
        //     dd('ffdfd', $menus->data['menus']);
        // }
    }

    public static function loadMoreMenuItems(WP_REST_Request $request)
    {
        $perPage = $request->get_param('perPage');
        $currentCount = $request->get_param('currentCount');
        $menus = self::getMenu();
        $menus = $menus->data['menus'];
        $menus = array_slice($menus, $currentCount, $perPage);
        return rest_ensure_response([
            'message' => 'menu get successfully',
            'menus' => $menus
        ]);
    }

    public static function deleteMenu(WP_REST_Request $request)
    {
        $id = $request->get_param('id');
        if (!$id) {
            return rest_ensure_response([
                'message' => 'Menu id is required'
            ], 400);
        }
        $res = MenuResource::deleteMenu($id);
        
        if (!$res) {
            return rest_ensure_response([
                'message' => 'Failed to delete menu'
            ], 500);
        }
        return rest_ensure_response([
            'message' => 'menu deleted successfully',
            'menus' => $res
        ]);
    }
}
