<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Content\StoreContentModelRequest;
use App\Http\Requests\Content\UpdateContentModelRequest;
use App\Services\ContentModelService;
use Illuminate\Http\Request;

class ContentModelController extends Controller
{
    protected $modelService;

    public function __construct(ContentModelService $modelService)
    {
        $this->modelService = $modelService;
    }

    public function index(Request $request)
    {
        $result = $this->modelService->index($request->all());

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

    public function store(StoreContentModelRequest $request)
    {
        $result = $this->modelService->store($request->validated());

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

    public function show($id)
    {
        $result = $this->modelService->show((int) $id);

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

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

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

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

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

    public function updateStatus(Request $request, $id)
    {
        $request->validate([
            'status' => 'required|in:0,1',
        ]);

        $result = $this->modelService->updateStatus($id, $request->status);

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

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

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

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