'use server';

/**
 * 제품검사 관리 Server Actions
 *
 * API Endpoints:
 * - GET /api/v1/inspections - 목록 조회
 * - GET /api/v1/inspections/stats - 통계 조회
 * - GET /api/v1/inspections/calendar - 캘린더 스케줄 조회
 * - GET /api/v1/inspections/{id} - 상세 조회
 * - POST /api/v1/inspections - 등록
 * - PUT /api/v1/inspections/{id} - 수정
 * - DELETE /api/v1/inspections/{id} - 삭제
 * - PATCH /api/v1/inspections/{id}/complete - 검사 완료 처리
 * - GET /api/v1/orders/select - 수주 선택 목록 조회
 */

import { executeServerAction } from '@/lib/api/execute-server-action';
import { buildApiUrl } from '@/lib/api/query-params';
import type {
  ProductInspection,
  InspectionStats,
  InspectionStatus,
  InspectionCalendarItem,
  OrderSelectItem,
  InspectionFormData,
} from './types';
import {
  mockInspections,
  mockStats,
  mockCalendarItems,
  mockOrderSelectItems,
} from './mockData';

// 개발환경 Mock 데이터 fallback 플래그
const USE_MOCK_FALLBACK = true;

// ===== API 응답 타입 =====

interface ProductInspectionApi {
  id: number;
  quality_doc_number: string;
  site_name: string;
  client: string;
  location_count: number;
  required_info: string;
  inspection_period: string;
  inspector: string;
  status: 'reception' | 'in_progress' | 'completed';
  author: string;
  reception_date: string;
  manager: string;
  manager_contact: string;
  construction_site: {
    site_name: string;
    land_location: string;
    lot_number: string;
  };
  material_distributor: {
    company_name: string;
    company_address: string;
    representative_name: string;
    phone: string;
  };
  constructor_info: {
    company_name: string;
    company_address: string;
    name: string;
    phone: string;
  };
  supervisor: {
    office_name: string;
    office_address: string;
    name: string;
    phone: string;
  };
  schedule_info: {
    visit_request_date: string;
    start_date: string;
    end_date: string;
    inspector: string;
    site_postal_code: string;
    site_address: string;
    site_address_detail: string;
  };
  order_items: Array<{
    id: string;
    order_number: string;
    site_name: string;
    delivery_date: string;
    floor: string;
    symbol: string;
    order_width: number;
    order_height: number;
    construction_width: number;
    construction_height: number;
    change_reason: string;
  }>;
  created_at: string;
  updated_at: string;
}

interface PaginatedResponse {
  items: ProductInspectionApi[];
  current_page: number;
  last_page: number;
  per_page: number;
  total: number;
}

interface InspectionStatsApi {
  reception_count: number;
  in_progress_count: number;
  completed_count: number;
}

interface CalendarItemApi {
  id: number;
  start_date: string;
  end_date: string;
  inspector: string;
  site_name: string;
  status: 'reception' | 'in_progress' | 'completed';
}

interface OrderSelectItemApi {
  id: number;
  order_number: string;
  site_name: string;
  delivery_date: string;
  location_count: number;
}

// ===== 페이지네이션 =====

interface PaginationMeta {
  currentPage: number;
  lastPage: number;
  perPage: number;
  total: number;
}

// ===== 상태 변환 =====

function mapApiStatus(status: ProductInspectionApi['status']): InspectionStatus {
  switch (status) {
    case 'reception':
      return '접수';
    case 'in_progress':
      return '진행중';
    case 'completed':
      return '완료';
    default:
      return '접수';
  }
}

function mapFrontendStatus(status: InspectionStatus): string {
  switch (status) {
    case '접수':
      return 'reception';
    case '진행중':
      return 'in_progress';
    case '완료':
      return 'completed';
    default:
      return 'reception';
  }
}

// ===== API → Frontend 변환 =====

function transformApiToFrontend(api: ProductInspectionApi): ProductInspection {
  return {
    id: String(api.id),
    qualityDocNumber: api.quality_doc_number,
    siteName: api.site_name,
    client: api.client,
    locationCount: api.location_count,
    requiredInfo: api.required_info,
    inspectionPeriod: api.inspection_period,
    inspector: api.inspector,
    status: mapApiStatus(api.status),
    author: api.author,
    receptionDate: api.reception_date,
    manager: api.manager,
    managerContact: api.manager_contact,
    constructionSite: {
      siteName: api.construction_site?.site_name || '',
      landLocation: api.construction_site?.land_location || '',
      lotNumber: api.construction_site?.lot_number || '',
    },
    materialDistributor: {
      companyName: api.material_distributor?.company_name || '',
      companyAddress: api.material_distributor?.company_address || '',
      representativeName: api.material_distributor?.representative_name || '',
      phone: api.material_distributor?.phone || '',
    },
    constructorInfo: {
      companyName: api.constructor_info?.company_name || '',
      companyAddress: api.constructor_info?.company_address || '',
      name: api.constructor_info?.name || '',
      phone: api.constructor_info?.phone || '',
    },
    supervisor: {
      officeName: api.supervisor?.office_name || '',
      officeAddress: api.supervisor?.office_address || '',
      name: api.supervisor?.name || '',
      phone: api.supervisor?.phone || '',
    },
    scheduleInfo: {
      visitRequestDate: api.schedule_info?.visit_request_date || '',
      startDate: api.schedule_info?.start_date || '',
      endDate: api.schedule_info?.end_date || '',
      inspector: api.schedule_info?.inspector || '',
      sitePostalCode: api.schedule_info?.site_postal_code || '',
      siteAddress: api.schedule_info?.site_address || '',
      siteAddressDetail: api.schedule_info?.site_address_detail || '',
    },
    orderItems: (api.order_items || []).map((item) => ({
      id: item.id,
      orderNumber: item.order_number,
      siteName: item.site_name || '',
      deliveryDate: item.delivery_date || '',
      floor: item.floor,
      symbol: item.symbol,
      orderWidth: item.order_width,
      orderHeight: item.order_height,
      constructionWidth: item.construction_width,
      constructionHeight: item.construction_height,
      changeReason: item.change_reason,
    })),
  };
}

// ===== Frontend → API 변환 =====

function transformFormToApi(data: InspectionFormData): Record<string, unknown> {
  return {
    quality_doc_number: data.qualityDocNumber,
    site_name: data.siteName,
    client: data.client,
    manager: data.manager,
    manager_contact: data.managerContact,
    construction_site: {
      site_name: data.constructionSite.siteName,
      land_location: data.constructionSite.landLocation,
      lot_number: data.constructionSite.lotNumber,
    },
    material_distributor: {
      company_name: data.materialDistributor.companyName,
      company_address: data.materialDistributor.companyAddress,
      representative_name: data.materialDistributor.representativeName,
      phone: data.materialDistributor.phone,
    },
    constructor_info: {
      company_name: data.constructorInfo.companyName,
      company_address: data.constructorInfo.companyAddress,
      name: data.constructorInfo.name,
      phone: data.constructorInfo.phone,
    },
    supervisor: {
      office_name: data.supervisor.officeName,
      office_address: data.supervisor.officeAddress,
      name: data.supervisor.name,
      phone: data.supervisor.phone,
    },
    schedule_info: {
      visit_request_date: data.scheduleInfo.visitRequestDate,
      start_date: data.scheduleInfo.startDate,
      end_date: data.scheduleInfo.endDate,
      inspector: data.scheduleInfo.inspector,
      site_postal_code: data.scheduleInfo.sitePostalCode,
      site_address: data.scheduleInfo.siteAddress,
      site_address_detail: data.scheduleInfo.siteAddressDetail,
    },
    order_items: data.orderItems.map((item) => ({
      order_number: item.orderNumber,
      floor: item.floor,
      symbol: item.symbol,
      order_width: item.orderWidth,
      order_height: item.orderHeight,
      construction_width: item.constructionWidth,
      construction_height: item.constructionHeight,
      change_reason: item.changeReason,
    })),
  };
}

// ===== 제품검사 목록 조회 =====

export async function getInspections(params?: {
  page?: number;
  size?: number;
  q?: string;
  status?: InspectionStatus | '전체';
  dateFrom?: string;
  dateTo?: string;
}): Promise<{
  success: boolean;
  data: ProductInspection[];
  pagination: PaginationMeta;
  error?: string;
  __authError?: boolean;
}> {
  const defaultPagination = { currentPage: 1, lastPage: 1, perPage: 20, total: 0 };

  const result = await executeServerAction<PaginatedResponse>({
    url: buildApiUrl('/api/v1/inspections', {
      page: params?.page,
      per_page: params?.size,
      q: params?.q,
      status: params?.status && params.status !== '전체' ? mapFrontendStatus(params.status) : undefined,
      date_from: params?.dateFrom,
      date_to: params?.dateTo,
    }),
    errorMessage: '제품검사 목록 조회에 실패했습니다.',
  });

  if (!result.success) {
    if (USE_MOCK_FALLBACK) {
      let filtered = [...mockInspections];
      if (params?.status && params.status !== '전체') {
        filtered = filtered.filter(i => i.status === params.status);
      }
      if (params?.q) {
        const q = params.q.toLowerCase();
        filtered = filtered.filter(i =>
          i.siteName.toLowerCase().includes(q) ||
          i.client.toLowerCase().includes(q) ||
          i.qualityDocNumber.toLowerCase().includes(q) ||
          i.inspector.toLowerCase().includes(q)
        );
      }
      const page = params?.page || 1;
      const size = params?.size || 20;
      const start = (page - 1) * size;
      return {
        success: true,
        data: filtered.slice(start, start + size),
        pagination: { currentPage: page, lastPage: Math.ceil(filtered.length / size), perPage: size, total: filtered.length },
      };
    }
    return { success: false, data: [], pagination: defaultPagination, error: result.error, __authError: result.__authError };
  }

  const d = result.data || { items: [], current_page: 1, last_page: 1, per_page: 20, total: 0 };
  return {
    success: true,
    data: d.items.map(transformApiToFrontend),
    pagination: {
      currentPage: d.current_page,
      lastPage: d.last_page,
      perPage: d.per_page,
      total: d.total,
    },
  };
}

// ===== 통계 조회 =====

export async function getInspectionStats(params?: {
  dateFrom?: string;
  dateTo?: string;
}): Promise<{
  success: boolean;
  data?: InspectionStats;
  error?: string;
  __authError?: boolean;
}> {
  const result = await executeServerAction<InspectionStatsApi>({
    url: buildApiUrl('/api/v1/inspections/stats', {
      date_from: params?.dateFrom,
      date_to: params?.dateTo,
    }),
    errorMessage: '제품검사 통계 조회에 실패했습니다.',
  });

  if (!result.success) {
    if (USE_MOCK_FALLBACK) return { success: true, data: mockStats };
    return { success: false, error: result.error, __authError: result.__authError };
  }

  const s = result.data;
  return {
    success: true,
    data: s ? {
      receptionCount: s.reception_count,
      inProgressCount: s.in_progress_count,
      completedCount: s.completed_count,
    } : undefined,
  };
}

// ===== 캘린더 스케줄 조회 =====

export async function getInspectionCalendar(params?: {
  year?: number;
  month?: number;
  inspector?: string;
  status?: InspectionStatus | '전체';
}): Promise<{
  success: boolean;
  data: InspectionCalendarItem[];
  error?: string;
  __authError?: boolean;
}> {
  const result = await executeServerAction<CalendarItemApi[]>({
    url: buildApiUrl('/api/v1/inspections/calendar', {
      year: params?.year,
      month: params?.month,
      inspector: params?.inspector,
      status: params?.status && params.status !== '전체' ? mapFrontendStatus(params.status) : undefined,
    }),
    errorMessage: '캘린더 스케줄 조회에 실패했습니다.',
  });

  if (!result.success) {
    if (USE_MOCK_FALLBACK) return { success: true, data: mockCalendarItems };
    return { success: false, data: [], error: result.error, __authError: result.__authError };
  }

  const items = result.data || [];
  return {
    success: true,
    data: items.map((item) => ({
      id: String(item.id),
      startDate: item.start_date,
      endDate: item.end_date,
      inspector: item.inspector,
      siteName: item.site_name,
      status: mapApiStatus(item.status as ProductInspectionApi['status']),
    })),
  };
}

// ===== 상세 조회 =====

export async function getInspectionById(id: string): Promise<{
  success: boolean;
  data?: ProductInspection;
  error?: string;
  __authError?: boolean;
}> {
  const result = await executeServerAction<ProductInspectionApi>({
    url: buildApiUrl(`/api/v1/inspections/${id}`),
    errorMessage: '제품검사 상세 조회에 실패했습니다.',
  });

  if (!result.success) {
    if (USE_MOCK_FALLBACK) {
      const mockItem = mockInspections.find(i => i.id === id);
      if (mockItem) return { success: true, data: mockItem };
      return { success: false, error: '해당 데이터를 찾을 수 없습니다.' };
    }
    return { success: false, error: result.error, __authError: result.__authError };
  }

  return result.data
    ? { success: true, data: transformApiToFrontend(result.data) }
    : { success: false, error: '상세 조회 실패' };
}

// ===== 등록 =====

export async function createInspection(data: InspectionFormData): Promise<{
  success: boolean;
  data?: ProductInspection;
  error?: string;
  __authError?: boolean;
}> {
  const apiData = transformFormToApi(data);
  const result = await executeServerAction<ProductInspectionApi>({
    url: buildApiUrl('/api/v1/inspections'),
    method: 'POST',
    body: apiData,
    errorMessage: '제품검사 등록에 실패했습니다.',
  });

  if (!result.success) return { success: false, error: result.error, __authError: result.__authError };
  return result.data
    ? { success: true, data: transformApiToFrontend(result.data) }
    : { success: true };
}

// ===== 수정 =====

export async function updateInspection(
  id: string,
  data: Partial<InspectionFormData>
): Promise<{
  success: boolean;
  data?: ProductInspection;
  error?: string;
  __authError?: boolean;
}> {
  const apiData: Record<string, unknown> = {};

  if (data.qualityDocNumber !== undefined) apiData.quality_doc_number = data.qualityDocNumber;
  if (data.siteName !== undefined) apiData.site_name = data.siteName;
  if (data.client !== undefined) apiData.client = data.client;
  if (data.manager !== undefined) apiData.manager = data.manager;
  if (data.managerContact !== undefined) apiData.manager_contact = data.managerContact;

  if (data.constructionSite) {
    apiData.construction_site = {
      site_name: data.constructionSite.siteName,
      land_location: data.constructionSite.landLocation,
      lot_number: data.constructionSite.lotNumber,
    };
  }
  if (data.materialDistributor) {
    apiData.material_distributor = {
      company_name: data.materialDistributor.companyName,
      company_address: data.materialDistributor.companyAddress,
      representative_name: data.materialDistributor.representativeName,
      phone: data.materialDistributor.phone,
    };
  }
  if (data.constructorInfo) {
    apiData.constructor_info = {
      company_name: data.constructorInfo.companyName,
      company_address: data.constructorInfo.companyAddress,
      name: data.constructorInfo.name,
      phone: data.constructorInfo.phone,
    };
  }
  if (data.supervisor) {
    apiData.supervisor = {
      office_name: data.supervisor.officeName,
      office_address: data.supervisor.officeAddress,
      name: data.supervisor.name,
      phone: data.supervisor.phone,
    };
  }
  if (data.scheduleInfo) {
    apiData.schedule_info = {
      visit_request_date: data.scheduleInfo.visitRequestDate,
      start_date: data.scheduleInfo.startDate,
      end_date: data.scheduleInfo.endDate,
      inspector: data.scheduleInfo.inspector,
      site_postal_code: data.scheduleInfo.sitePostalCode,
      site_address: data.scheduleInfo.siteAddress,
      site_address_detail: data.scheduleInfo.siteAddressDetail,
    };
  }
  if (data.orderItems) {
    apiData.order_items = data.orderItems.map((item) => ({
      order_number: item.orderNumber,
      floor: item.floor,
      symbol: item.symbol,
      order_width: item.orderWidth,
      order_height: item.orderHeight,
      construction_width: item.constructionWidth,
      construction_height: item.constructionHeight,
      change_reason: item.changeReason,
    }));
  }

  const result = await executeServerAction<ProductInspectionApi>({
    url: buildApiUrl(`/api/v1/inspections/${id}`),
    method: 'PUT',
    body: apiData,
    errorMessage: '제품검사 수정에 실패했습니다.',
  });

  if (!result.success) return { success: false, error: result.error, __authError: result.__authError };
  return result.data
    ? { success: true, data: transformApiToFrontend(result.data) }
    : { success: true };
}

// ===== 삭제 =====

export async function deleteInspection(id: string): Promise<{
  success: boolean;
  error?: string;
  __authError?: boolean;
}> {
  const result = await executeServerAction({
    url: buildApiUrl(`/api/v1/inspections/${id}`),
    method: 'DELETE',
    errorMessage: '제품검사 삭제에 실패했습니다.',
  });

  return { success: result.success, error: result.error, __authError: result.__authError };
}

// ===== 검사 완료 처리 =====

export async function completeInspection(
  id: string,
  data?: { result?: '합격' | '불합격' }
): Promise<{
  success: boolean;
  data?: ProductInspection;
  error?: string;
  __authError?: boolean;
}> {
  const apiData: Record<string, unknown> = {};
  if (data?.result) {
    apiData.result = data.result === '합격' ? 'pass' : 'fail';
  }

  const result = await executeServerAction<ProductInspectionApi>({
    url: buildApiUrl(`/api/v1/inspections/${id}/complete`),
    method: 'PATCH',
    body: apiData,
    errorMessage: '검사 완료 처리에 실패했습니다.',
  });

  if (!result.success) return { success: false, error: result.error, __authError: result.__authError };
  return result.data
    ? { success: true, data: transformApiToFrontend(result.data) }
    : { success: true };
}

// ===== 수주 선택 목록 조회 =====

export async function getOrderSelectList(params?: {
  q?: string;
}): Promise<{
  success: boolean;
  data: OrderSelectItem[];
  error?: string;
  __authError?: boolean;
}> {
  const result = await executeServerAction<OrderSelectItemApi[]>({
    url: buildApiUrl('/api/v1/orders/select', { q: params?.q }),
    errorMessage: '수주 선택 목록 조회에 실패했습니다.',
  });

  if (!result.success) {
    if (USE_MOCK_FALLBACK) {
      let filtered = [...mockOrderSelectItems];
      if (params?.q) {
        const q = params.q.toLowerCase();
        filtered = filtered.filter(i =>
          i.orderNumber.toLowerCase().includes(q) || i.siteName.toLowerCase().includes(q)
        );
      }
      return { success: true, data: filtered };
    }
    return { success: false, data: [], error: result.error, __authError: result.__authError };
  }

  const items = result.data || [];
  return {
    success: true,
    data: items.map((item) => ({
      id: String(item.id),
      orderNumber: item.order_number,
      siteName: item.site_name,
      deliveryDate: item.delivery_date,
      locationCount: item.location_count,
    })),
  };
}
