"use server";

import { cache } from 'react';
import { Pageable, serverFetchBlob, serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
//import { JobOrder, JoStatus, Machine, Operator } from ".";
import { BASE_API_URL } from "@/config/api";
import { revalidateTag } from "next/cache";
import { convertObjToURLSearchParams } from "@/app/utils/commonUtil";
import { FileResponse } from "@/app/api/pdf/actions"; 
export interface GetBagInfoResponse {
    id: number;
    bagId: number;
    bagName: string;
    lotId: number;
    lotNo: string;
    stockOutLineId: number;
    code: string;
    balanceQty: number;
}
export const getBagInfo = cache(async () => {
    return serverFetchJson<GetBagInfoResponse[]>(
      `${BASE_API_URL}/bag/bagInfo`,
      {
        method: "GET",
        next: { tags: ["bagInfo"] },
      }
    );
  });

  export interface CreateJoBagConsumptionRequest {
    bagId: number;
    bagLotLineId: number;
    jobId: number;
    //startQty: number;
    consumedQty: number;
    scrapQty: number;
  }
  export const createJoBagConsumption = cache(async (request: CreateJoBagConsumptionRequest) => {
    return serverFetchJson<any>(
      `${BASE_API_URL}/bag/createJoBagConsumption`,
      {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(request),
      }
    );
  });

  export interface BagUsageRecordResponse {
    id: number;
    bagId: number;
    bagLotLineId: number;
    jobId: number;
    jobOrderCode: string;
    stockOutLineId: number;
    startQty: number;
    consumedQty: number;
    scrapQty: number;
    endQty: number;
    date: string;
    time: string;
    bagName?: string;
    bagCode?: string;
    lotNo?: string;
}

// 添加 API 调用函数：

export const getBagUsageRecords = cache(async () => {
    return serverFetchJson<BagUsageRecordResponse[]>(
        `${BASE_API_URL}/bag/bagUsageRecords`,
        {
            method: "GET",
            next: { tags: ["bagUsageRecords"] },
        }
    );
});
export interface BagSummaryResponse {
    id: number;
    bagName: string;
    bagCode: string;
    takenBagBalance: number;
    deleted: boolean;
}
export interface BagLotLineResponse {
    id: number;
    bagId: number;
    lotNo: string;
    stockOutLineId: number;
    startQty: number;
    consumedQty: number;
    scrapQty: number;
    balanceQty: number;
    firstUseDate: string;
    lastUseDate: string;
}
export interface BagConsumptionResponse {
    id: number;
    bagId: number;
    bagLotLineId: number;
    jobId: number;
    jobOrderCode: string;
    stockOutLineId: number;
    startQty: number;
    consumedQty: number;
    scrapQty: number;
    endQty: number;
    date: string;
    time: string;
}
export const fetchBags = cache(async () =>
  serverFetchJson<BagSummaryResponse[]>(`${BASE_API_URL}/bag/bags`, { method: "GET" })
);

export const fetchBagLotLines = cache(async (bagId: number) =>
  serverFetchJson<BagLotLineResponse[]>(`${BASE_API_URL}/bag/bags/${bagId}/lot-lines`, { method: "GET" })
);

export const fetchBagConsumptions = cache(async (bagLotLineId: number) =>
  serverFetchJson<BagConsumptionResponse[]>(`${BASE_API_URL}/bag/lot-lines/${bagLotLineId}/consumptions`, { method: "GET" })
);

export interface SoftDeleteBagResponse {
  id: number | null;
  code: string | null;
  name: string | null;
  type: string | null;
  message: string | null;
  errorPosition: string | null;
  entity: any | null;
}

export const softDeleteBagByItemId = async (itemId: number): Promise<SoftDeleteBagResponse> => {
  const response = await serverFetchJson<SoftDeleteBagResponse>(
    `${BASE_API_URL}/bag/by-item/${itemId}/soft-delete`,
    {
      method: "PUT",
      headers: { "Content-Type": "application/json" },
    }
  );
  revalidateTag("bagInfo");
  revalidateTag("bags");
  return response;
};