<?php

namespace PortalPeople\virtualstock\controllers\api;

use Yii;
use yii\rest\Controller;

class DemoDataController extends Controller
{
    public function actionOrders(): array
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $dir = __DIR__ . '/../../data';
        $files = glob($dir . '/*.json');
        $data = [];
        foreach ($files as $file) {
            $name = basename($file, '.json');
            $content = file_get_contents($file);
            $data['orders'][] = json_decode($content, true);
        }
        return $data;
    }

    public function actionAcknowledge(): array
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $data = Yii::$app->request->post();
        /* Uncomment the following lines to simulate an error response when 'items' are sent in the payload.
        if (array_key_exists('items', $data)) {
            Yii::$app->response->statusCode = 400;
            return ['status' => 'Error: Items should not be sent in the payload for this demo endpoint.'];
        }*/
        return ['status' => 'acknowledged'];
    }
    public function actionDispatch(): array
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return ['status' => 'acknowledged'];
    }
}