import { buildTableQuery } from "$helpers/table-query/table-query.helper.js";

const requestTableQueryOptions = {
  sortableFields: [
    "fundName",
    "operationType",
    "amountRial",
    "units",
    "status",
    "paymentMethod",
    "externalSource",
    "createdAt",
  ],
  filters: {
    status: {
      field: "status",
      allowedValues: [
        "wait_for_payment",
        "reviewing",
        "approved",
        "rejected",
        "failed",
      ],
    },
    operationType: {
      field: "operationType",
      allowedValues: ["issuance", "redemption"],
    },
    paymentMethod: {
      field: "paymentMethod",
      allowedValues: ["gateway", "card", "slip"],
    },
    externalSource: {
      field: "externalSource",
      allowedValues: ["sepah_broker", "tadbir", "internal"],
      transform: (value) => (value === "internal" ? "" : value),
    },
    amountMin: {
      field: "amountRial",
      type: "number",
      operator: "gte",
    },
    amountMax: {
      field: "amountRial",
      type: "number",
      operator: "lte",
    },
    createdFrom: {
      field: "createdAt",
      type: "date",
      operator: "gte",
    },
    createdTo: {
      field: "createdAt",
      type: "date",
      operator: "lte",
      endOfDay: true,
    },
  },
  defaultSort: { field: "createdAt", direction: "desc" },
};

export const buildRequestTableQuery = (rawQuery) =>
  buildTableQuery(rawQuery, requestTableQueryOptions);
