/**
 * 생산 현황판 서버 액션
 * 공정 기반 동적 탭 전환
 */

'use server';


import { executeServerAction } from '@/lib/api/execute-server-action';
import { buildApiUrl } from '@/lib/api/query-params';
import type { WorkOrder, WorkerStatus, DashboardStats, ProcessOption } from './types';

// ===== API 타입 =====
interface WorkOrderApiItem {
  id: number;
  work_order_no: string;
  project_name: string | null;
  process_id: number | null;
  process?: { id: number; process_code: string; process_name: string };
  status: 'unassigned' | 'pending' | 'waiting' | 'in_progress' | 'completed' | 'shipped';
  scheduled_date: string | null;
  memo: string | null;
  created_at: string;
  sales_order?: {
    id: number; order_no: string; client_id?: number; client_name?: string;
    item?: { id: number; code: string; name: string } | null;
    client?: { id: number; name: string }; root_nodes_count?: number;
  };
  assignee?: { id: number; name: string };
  items?: { id: number; item_name: string; item_id?: number | null; item?: { id: number; code: string; name: string } | null; quantity: number; options?: Record<string, unknown> | null }[];
}

// ===== 상태 변환 =====
function mapApiStatus(status: WorkOrderApiItem['status']): 'waiting' | 'inProgress' | 'completed' {
  switch (status) {
    case 'in_progress': return 'inProgress';
    case 'completed': case 'shipped': return 'completed';
    default: return 'waiting';
  }
}

// ===== API → WorkOrder 변환 =====
function transformToProductionFormat(api: WorkOrderApiItem): WorkOrder {
  const totalQuantity = (api.items || []).reduce((sum, item) => sum + Number(item.quantity), 0);
  const productCode = (api.items?.[0]?.options?.product_code as string) || api.sales_order?.item?.code || '-';
  const productName = (api.items?.[0]?.options?.product_name as string) || api.items?.[0]?.item_name || '-';
  const dueDate = api.scheduled_date || '';
  const today = new Date();
  today.setHours(0, 0, 0, 0);
  const due = dueDate ? new Date(dueDate) : null;
  const isDelayed = due ? due < today && api.status !== 'completed' && api.status !== 'shipped' : false;
  const delayDays = due && isDelayed
    ? Math.ceil((today.getTime() - due.getTime()) / (1000 * 60 * 60 * 24))
    : undefined;
  const isUrgent = due
    ? due.getTime() - today.getTime() <= 3 * 24 * 60 * 60 * 1000 && due.getTime() >= today.getTime()
    : false;

  return {
    id: String(api.id),
    orderNo: api.work_order_no,
    productCode,
    productName,
    processCode: api.process?.process_code || '-',
    processName: api.process?.process_name || '-',
    client: api.sales_order?.client_name || api.sales_order?.client?.name || '-',
    projectName: api.project_name || '-',
    assignees: api.assignee ? [api.assignee.name] : [],
    quantity: totalQuantity,
    shutterCount: api.sales_order?.root_nodes_count || 0,
    dueDate,
    priority: isUrgent ? 1 : 5,
    status: mapApiStatus(api.status),
    isUrgent,
    isDelayed,
    delayDays,
    instruction: api.memo || undefined,
    createdAt: api.created_at,
  };
}

// ===== 공정 옵션 목록 조회 =====
export async function getProcessOptions(): Promise<{
  success: boolean;
  data: ProcessOption[];
  error?: string;
}> {
  interface ProcessApiData {
    id: number;
    process_code: string;
    process_name: string;
    process_type?: string;
    department?: string;
  }

  const result = await executeServerAction({
    url: buildApiUrl('/api/v1/processes/options'),
    transform: (data: ProcessApiData[]) =>
      (data || []).map(p => ({
        id: p.id,
        code: p.process_code,
        name: p.process_name,
        type: p.process_type,
        department: p.department,
      })),
    errorMessage: '공정 목록 조회에 실패했습니다.',
  });

  return {
    success: result.success,
    data: result.data || [],
    error: result.error,
  };
}

// ===== 작업지시 통계 조회 (백엔드 stats API) =====
interface WorkOrderStatsApi {
  total: number;
  unassigned: number;
  pending: number;
  waiting: number;
  in_progress: number;
  completed: number;
  shipped: number;
  by_process: Record<string, number>;
}

// ===== 대시보드 데이터 조회 =====
export async function getDashboardData(processCode?: string): Promise<{
  success: boolean;
  workOrders: WorkOrder[];
  workerStatus: WorkerStatus[];
  stats: DashboardStats;
  error?: string;
}> {
  const emptyResult = {
    success: false,
    workOrders: [] as WorkOrder[],
    workerStatus: [] as WorkerStatus[],
    stats: { total: 0, waiting: 0, inProgress: 0, completed: 0, urgent: 0, delayed: 0 },
  };

  // 통계와 목록을 병렬 조회
  const [statsResult, listResult] = await Promise.all([
    executeServerAction<WorkOrderStatsApi>({
      url: buildApiUrl('/api/v1/work-orders/stats'),
      errorMessage: '통계 조회에 실패했습니다.',
    }),
    executeServerAction<{ data: WorkOrderApiItem[] }>({
      url: buildApiUrl('/api/v1/work-orders', {
        per_page: 100,
        process_code: processCode && processCode !== 'all' ? processCode : undefined,
      }),
      errorMessage: '데이터 조회에 실패했습니다.',
    }),
  ]);

  if (!listResult.success || !listResult.data) {
    return { ...emptyResult, error: listResult.error };
  }

  const apiData = listResult.data.data || [];
  const workOrders = apiData.map(transformToProductionFormat);

  // 통계: 백엔드 stats API 기반 (정확한 전체 카운트)
  // urgent/delayed는 목록 데이터에서 계산 (stats API에 미포함)
  const apiStats = statsResult.success && statsResult.data ? statsResult.data : null;
  const stats: DashboardStats = apiStats
    ? {
        total: apiStats.total,
        waiting: (apiStats.unassigned ?? 0) + (apiStats.pending ?? 0) + (apiStats.waiting ?? 0),
        inProgress: apiStats.in_progress ?? 0,
        completed: (apiStats.completed ?? 0) + (apiStats.shipped ?? 0),
        urgent: workOrders.filter(o => o.isUrgent).length,
        delayed: workOrders.filter(o => o.isDelayed).length,
      }
    : {
        // stats API 실패 시 목록 데이터 기반 폴백
        total: workOrders.length,
        waiting: workOrders.filter(o => o.status === 'waiting').length,
        inProgress: workOrders.filter(o => o.status === 'inProgress').length,
        completed: workOrders.filter(o => o.status === 'completed').length,
        urgent: workOrders.filter(o => o.isUrgent).length,
        delayed: workOrders.filter(o => o.isDelayed).length,
      };

  // 작업자별 현황 집계
  const workerMap = new Map<string, WorkerStatus>();
  workOrders.forEach(order => {
    order.assignees.forEach(name => {
      if (!name || name === '-') return;
      if (!workerMap.has(name)) {
        workerMap.set(name, { id: name, name, inProgress: 0, completed: 0, assigned: 0 });
      }
      const worker = workerMap.get(name)!;
      worker.assigned++;
      if (order.status === 'inProgress') worker.inProgress++;
      else if (order.status === 'completed') worker.completed++;
    });
  });

  return {
    success: true,
    workOrders,
    workerStatus: Array.from(workerMap.values()).slice(0, 10),
    stats,
  };
}
