<?php

namespace app\modules\module_shop\controllers;

use app\modules\module_shop\models\ModuleShop;
use JetBrains\PhpStorm\NoReturn;
use kernel\helpers\Debug;
use kernel\Request;
use kernel\RestController;

class ModuleShopRestController extends RestController
{

    public function __construct()
    {
        $this->model = new ModuleShop();
    }

    #[NoReturn] public function actionIndex(): void
    {
        $request = new Request();
        $page = $request->get('page') ?? 1;
        $perPage = $request->get('per_page') ?? 10;
        $query = $this->model->query();

        $query->orderBy('created_at', 'DESC');

        if ($page > 1) {
            $query->skip(($page - 1) * $perPage)->take($perPage);
        } else {
            $query->take($perPage);
        }

        $expand = $this->expand();
        $expandParams = explode( ",", $request->get('expand') ?? "");
        $finalExpand = array_intersect($expandParams, $expand);
        if ($finalExpand) {
            $res = $query->get()->load($finalExpand)->toArray();
        } else {
            $res = $query->get()->toArray();
        }

        $this->renderApi($res);
    }

    #[NoReturn] public function actionIndexGroupBySlug(): void
    {
        $request = new Request();
        $page = $request->get('page') ?? 1;
        $perPage = $request->get('per_page') ?? 10;
        $query = $this->model->query();

        $query->select('ms1.*')
            ->from('module_shop as ms1')
            ->leftJoin('module_shop as ms2', function ($join) {
                $join->on('ms1.slug', '=', 'ms2.slug')
                    ->on('ms1.id', '<', 'ms2.id');
            })
            ->where('ms2.slug', '=', null);

        if ($page > 1) {
            $query->skip(($page - 1) * $perPage)->take($perPage);
        } else {
            $query->take($perPage);
        }

        $expand = $this->expand();
        $expandParams = explode( ",", $request->get('expand') ?? "");
        $finalExpand = array_intersect($expandParams, $expand);
        if ($finalExpand) {
            $res = $query->get()->load($finalExpand)->toArray();
        } else {
            $res = $query->get()->toArray();
        }

        $this->renderApi($res);
    }

    #[NoReturn] public function actionView($id): void
    {
        $expand = $this->expand();
        $request = new Request();
        $expandParams = explode( ",", $request->get('expand') ?? "");
        $model = $this->model->where("id", $id)->first();
        $model->views++;

        $finalExpand = array_intersect($expandParams, $expand);
        if ($finalExpand){
            $model->load($finalExpand);
        }
        $res = [];
        if ($model){
            $res = $model->toArray();
        }

        $model->save();
        $this->renderApi($res);
    }

    public function actionInstall($id): void
    {
        $model = $this->model->where("id", $id)->first();
        $model->installations++;
        $model->save();
        $this->renderApi($model->toArray());
    }

}