<?php

namespace App\Http\Controllers\Api\V1\Design;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Common\PaginateRequest;
use App\Http\Requests\Design\Model\StoreRequest;
use App\Http\Requests\Design\Model\UpdateRequest;
use App\Services\Design\ModelService;

class DesignModelController extends Controller
{
    public function __construct(
        protected ModelService $service
    ) {}

    // q, page, size만 서비스로 전달 (현재 시그니처 유지)
    public function index(PaginateRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $v = $request->validatedOrDefaults();   // page/size 기본값 주입됨
            $q = $v['q'] ?? '';
            $page = (int) ($v['page'] ?? 1);
            $size = (int) ($v['size'] ?? 20);
            $sort = $v['sort'] ?? null;   // id|code|name|created_at
            $order = $v['order'] ?? 'desc'; // asc|desc

            return $this->service->list($q, $page, $size);
        }, __('message.fetched'));
    }

    public function store(StoreRequest $request)
    {
        return ApiResponse::handle(
            fn () => $this->service->create($request->validated()),
            __('message.created')
        );
    }

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

    public function update(UpdateRequest $request, int $id)
    {
        return ApiResponse::handle(
            fn () => $this->service->update($id, $request->validated()),
            __('message.updated')
        );
    }

    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->delete($id);

            return null;
        }, __('message.deleted'));
    }
}
