/**
 * 부적합관리 Server Actions
 *
 * API Endpoints:
 * - GET    /api/v1/material/nonconforming-reports         목록 조회
 * - GET    /api/v1/material/nonconforming-reports/stats    통계
 * - GET    /api/v1/material/nonconforming-reports/{id}     단건 조회
 * - POST   /api/v1/material/nonconforming-reports          등록
 * - PUT    /api/v1/material/nonconforming-reports/{id}     수정
 * - DELETE /api/v1/material/nonconforming-reports/{id}     삭제
 * - PATCH  /api/v1/material/nonconforming-reports/{id}/status  상태 변경
 * - POST   /api/v1/material/nonconforming-reports/{id}/submit-approval  결재상신
 */

'use server';

import { buildApiUrl } from '@/lib/api/query-params';
import { executeServerAction } from '@/lib/api/execute-server-action';
import { executePaginatedAction } from '@/lib/api/execute-paginated-action';
import type { NonconformingListItem } from './types';

const BASE_PATH = '/api/v1/material/nonconforming-reports';

// 목록 조회
export async function getNonconformingReports(params?: {
  page?: number;
  per_page?: number;
  search?: string;
  status?: string;
  nc_type?: string;
  from_date?: string;
  to_date?: string;
}) {
  return executePaginatedAction<NonconformingListItem, NonconformingListItem>({
    url: buildApiUrl(BASE_PATH, {
      page: params?.page,
      per_page: params?.per_page,
      search: params?.search,
      status: params?.status !== 'all' ? params?.status : undefined,
      nc_type: params?.nc_type !== 'all' ? params?.nc_type : undefined,
      from_date: params?.from_date,
      to_date: params?.to_date,
    }),
    transform: (item) => item,
    errorMessage: '부적합 목록 조회에 실패했습니다.',
  });
}

// 통계
export async function getNonconformingStats() {
  return executeServerAction({
    url: buildApiUrl(`${BASE_PATH}/stats`),
    errorMessage: '부적합 통계 조회에 실패했습니다.',
  });
}

// 단건 조회
export async function getNonconformingReport(id: number | string) {
  return executeServerAction({
    url: buildApiUrl(`${BASE_PATH}/${id}`),
    errorMessage: '부적합 상세 조회에 실패했습니다.',
  });
}

// 등록
export async function createNonconformingReport(data: Record<string, unknown>) {
  return executeServerAction({
    url: buildApiUrl(BASE_PATH),
    method: 'POST',
    body: data,
    errorMessage: '부적합 등록에 실패했습니다.',
  });
}

// 수정
export async function updateNonconformingReport(id: number | string, data: Record<string, unknown>) {
  return executeServerAction({
    url: buildApiUrl(`${BASE_PATH}/${id}`),
    method: 'PUT',
    body: data,
    errorMessage: '부적합 수정에 실패했습니다.',
  });
}

// 삭제
export async function deleteNonconformingReport(id: number | string) {
  return executeServerAction({
    url: buildApiUrl(`${BASE_PATH}/${id}`),
    method: 'DELETE',
    errorMessage: '부적합 삭제에 실패했습니다.',
  });
}

// 상태 변경
export async function changeNonconformingStatus(id: number | string, status: string) {
  return executeServerAction({
    url: buildApiUrl(`${BASE_PATH}/${id}/status`),
    method: 'PATCH',
    body: { status },
    errorMessage: '상태 변경에 실패했습니다.',
  });
}

// 결재상신
export async function submitNonconformingApproval(
  id: number | string,
  data: { title?: string; form_id?: number; steps: Array<{ approver_id: number; step_type?: string }> }
) {
  return executeServerAction({
    url: buildApiUrl(`${BASE_PATH}/${id}/submit-approval`),
    method: 'POST',
    body: data,
    errorMessage: '결재상신에 실패했습니다.',
  });
}
