"use server";
import { BASE_API_URL } from "@/config/api";
// import { ServerFetchError, serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
import { revalidateTag } from "next/cache";
import { cache } from "react";
import { serverFetchJson } from "@/app/utils/fetchUtil";
import { QcItemResult } from "../settings/qcItem";
import { RecordsRes } from "../utils";
import {
  ConsoPickOrderResult,
  PickOrderLineWithSuggestedLot,
  PickOrderResult,
  PreReleasePickOrderSummary,
  StockOutLine,
  
} from ".";
import { PurchaseQcResult } from "../po/actions";
// import { BASE_API_URL } from "@/config/api";

export interface SavePickOrderLineRequest {
  itemId: number
  qty: number
  uomId: number
}

export interface SavePickOrderRequest {
  type: string
  targetDate: string
  pickOrderLine: SavePickOrderLineRequest[]
}
export interface PostPickOrderResponse<T = null> {
  id: number | null;
  name: string;
  code: string;
  type?: string;
  message: string | null;
  errorPosition: string
  entity?: T | T[];
  consoCode?: string;
}
export interface PostStockOutLiineResponse<T> {
  id: number | null;
  name: string;
  code: string;
  type?: string;
  message: string | null;
  errorPosition: string | keyof T;
  entity: T | T[] | null;
}

export interface ReleasePickOrderInputs {
  consoCode: string;
  assignTo: number;
}

export interface CreateStockOutLine {
  consoCode: string;
  pickOrderLineId: number;
  inventoryLotLineId: number;
  qty: number;
}

export interface UpdateStockOutLine {
  id: number;
  // consoCode: String,
  itemId: number;
  qty: number;
  pickOrderLineId: number;
  inventoryLotLineId?: number;
  status: string;
  pickTime?: string;
  // pickerId: number?
}

export interface PickOrderQcInput {
  qty: number;
  status: string;
  qcResult: PurchaseQcResult[];
}

export interface PickOrderApprovalInput {
  allowQty: number;
  rejectQty: number;
  status: string;
}


export interface GetPickOrderInfoResponse {
  consoCode: string | null;
  pickOrders: GetPickOrderInfo[];
  items: CurrentInventoryItemInfo[];
}

export interface GetPickOrderInfo {
  id: number;
  code: string;
  targetDate: string;
  type: string;
  status: string;
  assignTo: number;
  pickOrderLines: GetPickOrderLineInfo[];
}

export interface GetPickOrderLineInfo {
  id: number;
  itemId: number;
  itemCode: string;
  itemName: string;
  availableQty: number| null;
  requiredQty: number;
  uomCode: string;
  uomDesc: string;
  suggestedList: any[];
  pickedQty: number;
}

export interface CurrentInventoryItemInfo {
  id: number;
  code: string;
  name: string;
  uomDesc: string;
  availableQty: number;
  requiredQty: number;
}

export interface SavePickOrderGroupRequest {
  groupIds?: number[];
  names?: string[];
  targetDate?: string;
  pickOrderId?: number | null;
}

export interface PickOrderGroupInfo {
  id: number;
  name: string;
  targetDate: string | null;
  pickOrderId: number | null;
}

export interface AssignPickOrderInputs {
  pickOrderIds: number[];
  assignTo: number;
}
export interface LotDetailWithStockOutLine {
  lotId: number;
  lotNo: string;
  expiryDate: string;
  location: string;
  stockUnit: string;
  availableQty: number;
  requiredQty: number;
  actualPickQty: number;
  suggestedPickLotId: number;
  lotStatus: string;
  lotAvailability: string;
  stockOutLineId?: number;
  stockOutLineStatus?: string;
  stockOutLineQty?: number;
}



export const resuggestPickOrder = async (pickOrderId: number) => {
  console.log("Resuggesting pick order:", pickOrderId);
  const result = await serverFetchJson<PostPickOrderResponse>(
    `${BASE_API_URL}/suggestedPickLot/resuggest/${pickOrderId}`,
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return result;
};

export const updateStockOutLineStatus = async (data: {
  id: number;
  status: string;
  qty?: number;
  remarks?: string;
}) => {
  console.log("Updating stock out line status:", data);
  const result = await serverFetchJson<PostStockOutLiineResponse<StockOutLine>>(
    `${BASE_API_URL}/stockOutLine/updateStatus`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return result;
};
// Missing function 1: newassignPickOrder
export const newassignPickOrder = async (data: AssignPickOrderInputs) => {
  const response = await serverFetchJson<PostPickOrderResponse>(
    `${BASE_API_URL}/pickOrder/assign`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return response;
};

// Missing function 2: releaseAssignedPickOrders  
export const releaseAssignedPickOrders = async (data: AssignPickOrderInputs) => {
  const response = await serverFetchJson<PostPickOrderResponse>(
    `${BASE_API_URL}/pickOrder/release-assigned`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return response;
};
// Get latest group name and create it automatically
export const getLatestGroupNameAndCreate = async () => {
  return serverFetchJson<PostPickOrderResponse>(
    `${BASE_API_URL}/pickOrder/groups/latest`,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
};

// Get all groups
export const fetchAllGroups = cache(async () => {
  return serverFetchJson<PickOrderGroupInfo[]>(
    `${BASE_API_URL}/pickOrder/groups/list`,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});

// Create or update groups (flexible - can handle both cases)
export const createOrUpdateGroups = async (data: SavePickOrderGroupRequest) => {
  const response = await serverFetchJson<PostPickOrderResponse>(
    `${BASE_API_URL}/pickOrder/groups/create`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return response;
};

// Get groups by pick order ID
export const fetchGroupsByPickOrderId = cache(async (pickOrderId: number) => {
  return serverFetchJson<PickOrderGroupInfo[]>(
    `${BASE_API_URL}/pickOrder/groups/${pickOrderId}`,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});

export const fetchPickOrderDetails = cache(async (ids: string) => {
  return serverFetchJson<GetPickOrderInfoResponse>(
    `${BASE_API_URL}/pickOrder/detail/${ids}`,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});
export interface PickOrderLotDetailResponse {
  lotId: number;
  lotNo: string;
  expiryDate: string;
  location: string;
  stockUnit: string;
  inQty: number;
  availableQty: number;
  requiredQty: number;
  actualPickQty: number;
  suggestedPickLotId: number;
  lotStatus: string;
  lotAvailability: 'available' | 'insufficient_stock' | 'expired' | 'status_unavailable';
}
interface ALLPickOrderLotDetailResponse {
  // Pick Order Information
  pickOrderId: number;
  pickOrderCode: string;
  pickOrderTargetDate: string;
  pickOrderType: string;
  pickOrderStatus: string;
  pickOrderAssignTo: number;
  groupName: string;
  
  // Pick Order Line Information
  pickOrderLineId: number;
  pickOrderLineRequiredQty: number;
  pickOrderLineStatus: string;
  
  // Item Information
  itemId: number;
  itemCode: string;
  itemName: string;
  uomCode: string;
  uomDesc: string;
  
  // Lot Information
  lotId: number;
  lotNo: string;
  expiryDate: string;
  location: string;
  stockUnit: string;
  availableQty: number;
  requiredQty: number;
  actualPickQty: number;
  suggestedPickLotId: number;
  lotStatus: string;
  stockOutLineId?: number;
  stockOutLineStatus?: string;
  stockOutLineQty?: number;
  lotAvailability: 'available' | 'insufficient_stock' | 'expired' | 'status_unavailable';
  processingStatus: string;
}
export const fetchALLPickOrderLineLotDetails = cache(async (userId?: number) => {
  const url = userId 
    ? `${BASE_API_URL}/pickOrder/all-lots-with-details?userId=${userId}`
    : `${BASE_API_URL}/pickOrder/all-lots-with-details`;
    
  return serverFetchJson<ALLPickOrderLotDetailResponse[]>(
    url,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});
export const fetchAllPickOrderDetails = cache(async (userId?: number) => {
  const url = userId 
    ? `${BASE_API_URL}/pickOrder/detail?userId=${userId}`
    : `${BASE_API_URL}/pickOrder/detail`;
    
  return serverFetchJson<GetPickOrderInfoResponse>(
    url,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});

export const fetchPickOrderLineLotDetails = cache(async (pickOrderLineId: number) => {
  return serverFetchJson<PickOrderLotDetailResponse[]>(
    `${BASE_API_URL}/pickOrder/lot-details/${pickOrderLineId}`,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});






export const createPickOrder = async (data: SavePickOrderRequest) => {
  console.log(data);
  const po = await serverFetchJson<PostPickOrderResponse>(
    `${BASE_API_URL}/pickOrder/create`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return po;
}

export const assignPickOrder = async (ids: number[]) => {
  const pickOrder = await serverFetchJson<any>(
    `${BASE_API_URL}/pickOrder/conso`,
    {
      method: "POST",
      body: JSON.stringify({ ids: ids }),
      headers: { "Content-Type": "application/json" },
    },
  );
  //   revalidateTag("po");
  return pickOrder;
};
export const consolidatePickOrder = async (ids: number[]) => {
  const pickOrder = await serverFetchJson<any>(
    `${BASE_API_URL}/pickOrder/conso`,
    {
      method: "POST",
      body: JSON.stringify({ ids: ids }),
      headers: { "Content-Type": "application/json" },
    },
  );
  return pickOrder;
};
export const consolidatePickOrder_revert = async (ids: number[]) => {
  const pickOrder = await serverFetchJson<any>(
    `${BASE_API_URL}/pickOrder/deconso`,
    {
      method: "POST",
      body: JSON.stringify({ ids: ids }),
      headers: { "Content-Type": "application/json" },
    },
  );
  //   revalidateTag("po");
  return pickOrder;
};

export const fetchPickOrderClient = cache(
  async (queryParams?: Record<string, any>) => {
    if (queryParams) {
      const queryString = new URLSearchParams(queryParams).toString();
      return serverFetchJson<RecordsRes<PickOrderResult[]>>(
        `${BASE_API_URL}/pickOrder/getRecordByPage?${queryString}`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    } else {
      return serverFetchJson<RecordsRes<PickOrderResult[]>>(
        `${BASE_API_URL}/pickOrder/getRecordByPage`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    }
  },
);


export const fetchPickOrderWithStockClient = cache(
  async (queryParams?: Record<string, any>) => {
    if (queryParams) {
      const queryString = new URLSearchParams(queryParams).toString();
      return serverFetchJson<RecordsRes<GetPickOrderInfo[]>>(
        `${BASE_API_URL}/pickOrder/getRecordByPageWithStock?${queryString}`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    } else {
      return serverFetchJson<RecordsRes<GetPickOrderInfo[]>>(
        `${BASE_API_URL}/pickOrder/getRecordByPageWithStock`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    }
  },
);

export const fetchConsoPickOrderClient = cache(
  async (queryParams?: Record<string, any>) => {
    if (queryParams) {
      const queryString = new URLSearchParams(queryParams).toString();
      return serverFetchJson<RecordsRes<ConsoPickOrderResult[]>>(
        `${BASE_API_URL}/pickOrder/getRecordByPage-conso?${queryString}`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    } else {
      return serverFetchJson<RecordsRes<ConsoPickOrderResult[]>>(
        `${BASE_API_URL}/pickOrder/getRecordByPage-conso`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    }
  },
);

export const fetchPickOrderLineClient = cache(
  async (queryParams?: Record<string, any>) => {
    if (queryParams) {
      const queryString = new URLSearchParams(queryParams).toString();
      return serverFetchJson<RecordsRes<PickOrderLineWithSuggestedLot[]>>(
        `${BASE_API_URL}/pickOrder/get-pickorder-line-byPage?${queryString}`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    } else {
      return serverFetchJson<RecordsRes<PickOrderLineWithSuggestedLot[]>>(
        `${BASE_API_URL}/pickOrder/get-pickorder-line-byPage`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    }
  },
);

export const fetchStockOutLineClient = cache(
  async (pickOrderLineId: number) => {
    return serverFetchJson<StockOutLine[]>(
      `${BASE_API_URL}/stockOutLine/getByPickOrderLineId/${pickOrderLineId}`,
      {
        method: "GET",
        next: { tags: ["pickorder"] },
      },
    );
  },
);

export const fetchConsoDetail = cache(async (consoCode: string) => {
  return serverFetchJson<PreReleasePickOrderSummary>(
    `${BASE_API_URL}/pickOrder/pre-release-info/${consoCode}`,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});

export const releasePickOrder = async (data: ReleasePickOrderInputs) => {
  console.log(data);
  console.log(JSON.stringify(data));
  const po = await serverFetchJson<{ consoCode: string }>(
    `${BASE_API_URL}/pickOrder/releaseConso`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return po;
};

export const createStockOutLine = async (data: CreateStockOutLine) => {
  console.log("triggering");
  const po = await serverFetchJson<PostStockOutLiineResponse<StockOutLine>>(
    `${BASE_API_URL}/stockOutLine/create`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return po;
};

export const updateStockOutLine = async (data: UpdateStockOutLine) => {
  console.log(data);
  const po = await serverFetchJson<PostStockOutLiineResponse<StockOutLine>>(
    `${BASE_API_URL}/stockOutLine/update`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return po;
};

export const completeConsoPickOrder = async (consoCode: string) => {
  const po = await serverFetchJson<PostStockOutLiineResponse<StockOutLine>>(
    `${BASE_API_URL}/pickOrder/consoPickOrder/complete/${consoCode}`,
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("pickorder");
  return po;
};

export const fetchConsoStatus = cache(async (consoCode: string) => {
  return serverFetchJson<{ status: string }>(
    `${BASE_API_URL}/stockOut/get-status/${consoCode}`,
    {
      method: "GET",
      next: { tags: ["pickorder"] },
    },
  );
});
