<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Product\ProductStoreRequest;
use App\Http\Requests\Product\ProductUpdateRequest;
use App\Services\ProductService;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function __construct(private ProductService $service) {}

    public function getCategory(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getCategory($request);
        }, __('message.product.category_fetched'));
    }

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

    // POST /products
    public function store(ProductStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.product.created'));
    }

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

    // PATCH /products/{id}
    public function update(int $id, ProductUpdateRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update($id, $request->validated());
        }, __('message.product.updated'));
    }

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

            return 'success';
        }, __('message.product.deleted'));
    }

    // GET /products/search
    public function search(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->search($request->all());
        }, __('message.product.searched'));
    }

    // Note: toggle 메서드는 is_active 필드 제거로 인해 비활성화됨
    // 필요시 attributes JSON이나 별도 필드로 구현
    // POST /products/{id}/toggle
    // public function toggle(int $id)
    // {
    //     return ApiResponse::handle(function () use ($id) {
    //         return $this->service->toggle($id);
    //     }, __('message.product.toggled'));
    // }
}
