<?php

namespace App\Http\Controllers\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Process\StoreProcessRequest;
use App\Http\Requests\V1\Process\UpdateProcessRequest;
use App\Services\ProcessService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ProcessController extends Controller
{
    public function __construct(
        private readonly ProcessService $processService
    ) {}

    /**
     * 공정 목록 조회
     */
    public function index(Request $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->index(
                $request->only(['page', 'size', 'q', 'status', 'process_type'])
            ),
            'message.fetched'
        );
    }

    /**
     * 공정 상세 조회
     */
    public function show(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->show($id),
            'message.fetched'
        );
    }

    /**
     * 공정 생성
     */
    public function store(StoreProcessRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->store($request->validated()),
            'message.created'
        );
    }

    /**
     * 공정 수정
     */
    public function update(UpdateProcessRequest $request, int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->update($id, $request->validated()),
            'message.updated'
        );
    }

    /**
     * 공정 삭제
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->destroy($id),
            'message.deleted'
        );
    }

    /**
     * 공정 일괄 삭제
     */
    public function destroyMany(Request $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => ['deleted_count' => $this->processService->destroyMany($request->input('ids', []))],
            'message.deleted'
        );
    }

    /**
     * 공정 상태 토글
     */
    public function toggleActive(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->toggleActive($id),
            'message.updated'
        );
    }

    /**
     * 공정 옵션 목록 (드롭다운용)
     */
    public function options(): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->options(),
            'message.fetched'
        );
    }

    /**
     * 공정 통계
     */
    public function stats(): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->processService->getStats(),
            'message.fetched'
        );
    }
}
