<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Material\MaterialStoreRequest;
use App\Http\Requests\Material\MaterialUpdateRequest;
use App\Services\MaterialService;
use Illuminate\Http\Request;

class MaterialController extends Controller
{
    public function __construct(private MaterialService $service) {}

    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getMaterials($request->all());
        }, __('message.material.fetched'));
    }

    public function store(MaterialStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            // 동적 필드 지원을 위해 전체 입력값 전달 (Service에서 검증)
            return $this->service->setMaterial($request->all());
        }, __('message.material.created'));
    }

    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getMaterial($id);
        }, __('message.material.fetched'));
    }

    public function update(MaterialUpdateRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            // 동적 필드 지원을 위해 전체 입력값 전달 (Service에서 검증)
            return $this->service->updateMaterial($id, $request->all());
        }, __('message.material.updated'));
    }

    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroyMaterial($id);
        }, __('message.material.deleted'));
    }
}
