<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Content\StoreContentRequest;
use App\Http\Requests\Content\UpdateContentRequest;
use App\Services\ContentService;
use Illuminate\Http\Request;

class ContentController extends Controller
{
    protected $contentService;

    public function __construct(ContentService $contentService)
    {
        $this->contentService = $contentService;
    }

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

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

    public function store(StoreContentRequest $request)
    {
        $data = $request->validated();
        $data['user_id'] = auth('admin')->id();

        $result = $this->contentService->store($data);

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

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

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

    public function update(UpdateContentRequest $request, $id)
    {
        $data = $request->validated();

        $result = $this->contentService->update($id, $data);

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

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

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

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

        $result = $this->contentService->batchDelete($request->ids);

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

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

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

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

    public function formConfig(Request $request)
    {
        $request->validate([
            'category_id' => 'required|integer',
        ]);

        $result = $this->contentService->getFormConfig($request->category_id);

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