<?php

namespace App\Http\Controllers;

use App\Models\AiModel;
use Illuminate\Http\Request;
use App\Http\Api\AuthApi;
use Illuminate\Support\Facades\Log;
use App\Tools\RedisClusters;
use App\Http\Resources\AiModelResource;

class SysModelController extends Controller
{
    protected $key = "/ai/model/system/";
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $user = AuthApi::current($request);
        if (!$user) {
            Log::error('notification auth failed {request}', ['request' => $request]);
            return $this->error(__('auth.failed'), 401, 401);
        }
        $modelsId = RedisClusters::get($this->key . $request->get('view', 'wbw'));
        if (!is_array($modelsId)) {
            $modelsId = [];
        }
        $result = AiModel::whereIn('uid', $modelsId)
            ->get();
        return $this->ok(
            [
                "rows" => AiModelResource::collection(resource: $result),
                "count" => count($result),
            ]
        );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $user = AuthApi::current($request);
        if (!$user) {
            Log::error('notification auth failed {request}', ['request' => $request]);
            return $this->error(__('auth.failed'), 401, 401);
        }
        RedisClusters::put(
            $this->key . $request->get('view', 'wbw'),
            $request->get('models')
        );
        return $this->ok('ok');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\AiModel  $aiModel
     * @return \Illuminate\Http\Response
     */
    public function show(string $view)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\AiModel  $aiModel
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, AiModel $aiModel)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\AiModel  $aiModel
     * @return \Illuminate\Http\Response
     */
    public function destroy(AiModel $aiModel)
    {
        //
    }
}
