<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Content\StoreContentModelFieldRequest;
use App\Http\Requests\Content\UpdateContentModelFieldRequest;
use App\Services\ContentModelFieldService;
use Illuminate\Http\Request;

class ContentModelFieldController extends Controller
{
    protected $fieldService;

    public function __construct(ContentModelFieldService $fieldService)
    {
        $this->fieldService = $fieldService;
    }

    public function index($modelId)
    {
        $result = $this->fieldService->index((int) $modelId);

        return response()->json($result);
    }

    public function store($modelId, StoreContentModelFieldRequest $request)
    {
        $result = $this->fieldService->store((int) $modelId, $request->validated());

        return response()->json($result);
    }

    public function update(UpdateContentModelFieldRequest $request, $id)
    {
        $result = $this->fieldService->update($id, $request->validated());

        return response()->json($result);
    }

    public function destroy($id)
    {
        $result = $this->fieldService->destroy($id);

        return response()->json($result);
    }

    public function saveSort(Request $request)
    {
        $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'integer',
            'sorts' => 'required|array',
            'sorts.*' => 'integer',
        ]);

        $result = $this->fieldService->saveSort($request->ids, $request->sorts);

        return response()->json($result);
    }
}
