<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Process;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ProcessApiController extends Controller
{
    /**
     * 공정 검색 (문서양식 품질검사 연결용)
     */
    public function search(Request $request): JsonResponse
    {
        $tenantId = session('selected_tenant_id');
        $query = $request->input('q', '');

        $processes = Process::query()
            ->where('tenant_id', $tenantId)
            ->where('is_active', true)
            ->when($query, function ($q) use ($query) {
                $q->where(function ($sub) use ($query) {
                    $sub->where('process_name', 'like', "%{$query}%")
                        ->orWhere('process_code', 'like', "%{$query}%");
                });
            })
            ->orderBy('process_name')
            ->limit(30)
            ->get(['id', 'process_code', 'process_name']);

        return response()->json([
            'success' => true,
            'data' => $processes,
        ]);
    }
}
