"use server";

// import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
// import { BASE_API_URL } from "@/config/api";
import {
  serverFetchJson,
  serverFetchWithNoContent,
} from "../../utils/fetchUtil";
import { BASE_API_URL } from "../../../config/api";
import { revalidateTag } from "next/cache";
import { cache } from "react";


export interface ShopAndTruck{
  id: number;
  name: String;
  code: String;
  addr1: String;
  addr2: String;
  addr3: String;
  contactNo: number;  
  type: String;
  contactEmail: String;
  contactName: String;
  truckLanceCode: String;
  DepartureTime: String;
  LoadingSequence: number;
  districtReference: Number;
  Store_id: Number
}

export interface Shop{
  id: number;
  name: String;
  code: String;
  addr3: String;
}

export interface Truck{
  id?: number;
  truckLanceCode: String;
  departureTime: String | number[];
  loadingSequence: number;
  districtReference: Number;
  storeId: Number | String;
  remark?: String | null;
}

export interface SaveTruckLane {
  id: number;
  truckLanceCode: string;
  departureTime: string;
  loadingSequence: number;
  districtReference: number;
  storeId: string;
  remark?: string | null;
}

export interface DeleteTruckLane {
  id: number;
}

export interface SaveTruckRequest {
  id?: number | null;
  store_id: string;
  truckLanceCode: string;
  departureTime: string;
  shopId: number;
  shopName: string;
  shopCode: string;
  loadingSequence: number;
  districtReference?: number | null;
  remark?: string | null;
}

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

export const fetchAllShopsAction = cache(async (params?: Record<string, string | number | null>) => {
  const endpoint = `${BASE_API_URL}/shop/combo/allShop`;
  const qs = params
    ? Object.entries(params)
        .filter(([, v]) => v !== null && v !== undefined && String(v).trim() !== "")
        .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
        .join("&")
    : "";

  const url = qs ? `${endpoint}?${qs}` : endpoint;

  return serverFetchJson<ShopAndTruck[]>(url, {
    method: "GET",
    headers: { "Content-Type": "application/json" },
  });
});

export const findTruckLaneByShopIdAction = cache(async (shopId: number | string) => {
  const endpoint = `${BASE_API_URL}/truck/findTruckLane/${shopId}`;
  
  return serverFetchJson<Truck[]>(endpoint, {
    method: "GET",
    headers: { "Content-Type": "application/json" },
  });
});

export const updateTruckLaneAction = async (data: SaveTruckLane) => {
  const endpoint = `${BASE_API_URL}/truck/updateTruckLane`;
  
  return serverFetchJson<MessageResponse>(endpoint, {
    method: "POST",
    body: JSON.stringify(data),
    headers: { "Content-Type": "application/json" },
  });
};

export const deleteTruckLaneAction = async (data: DeleteTruckLane) => {
  const endpoint = `${BASE_API_URL}/truck/deleteTruckLane`;
  
  return serverFetchJson<MessageResponse>(endpoint, {
    method: "POST",
    body: JSON.stringify(data),
    headers: { "Content-Type": "application/json" },
  });
};

export const createTruckAction = async (data: SaveTruckRequest) => {
  const endpoint = `${BASE_API_URL}/truck/create`;
  
  return serverFetchJson<MessageResponse>(endpoint, {
    method: "POST",
    body: JSON.stringify(data),
    headers: { "Content-Type": "application/json" },
  });
};