<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\CategoryTemplateService;
use Illuminate\Http\Request;

class CategoryTemplateController extends Controller
{
    public function __construct(private CategoryTemplateService $service) {}

    // GET /categories/{id}/templates
    public function index(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->index($id, $request->all());
        }, '카테고리 템플릿 목록');
    }

    // POST /categories/{id}/templates
    public function store(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->store($id, $request->all());
        }, '카테고리 템플릿 생성');
    }

    // GET /categories/templates/{tpl}
    public function show(int $tpl)
    {
        return ApiResponse::handle(function () use ($tpl) {
            return $this->service->show($tpl);
        }, '카테고리 템플릿 조회');
    }

    // PATCH /categories/templates/{tpl}
    public function update(int $tpl, Request $request)
    {
        return ApiResponse::handle(function () use ($tpl, $request) {
            return $this->service->update($tpl, $request->all());
        }, '카테고리 템플릿 수정');
    }

    // DELETE /categories/templates/{tpl}
    public function destroy(int $tpl)
    {
        return ApiResponse::handle(function () use ($tpl) {
            $this->service->destroy($tpl);

            return 'success';
        }, '카테고리 템플릿 삭제');
    }

    // POST /categories/{id}/templates/{tpl}/apply
    public function apply(int $id, int $tpl)
    {
        return ApiResponse::handle(function () use ($id, $tpl) {
            $this->service->apply($id, $tpl);

            return 'success';
        }, '카테고리 템플릿 적용');
    }

    // GET /categories/{id}/templates/{tpl}/preview
    public function preview(int $id, int $tpl)
    {
        return ApiResponse::handle(function () use ($id, $tpl) {
            return $this->service->preview($id, $tpl);
        }, '카테고리 템플릿 미리보기');
    }

    // GET /categories/{id}/templates/diff?a=&b=
    public function diff(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->diff($id, (int) $request->query('a'), (int) $request->query('b'));
        }, '카테고리 템플릿 비교');
    }
}
