import { Shield, ClipboardList, DollarSign } from "lucide-react";

// Custom renderer for durations column
function renderDurations(value: any) {
  if (!value || !Array.isArray(value) || value.length === 0) return "None";
  const tags = value.map((d: any) => `${d.duration} ${d.timeframe}`);
  return (
    <div className="flex flex-wrap gap-1">
      {tags.map((tag: string, index: number) => (
        <span
          key={index}
          className="rounded bg-blue-100 px-2 py-1 text-xs font-medium text-blue-800"
        >
          {tag}
        </span>
      ))}
    </div>
  );
}

export const columns: ColumnDefinition[] = [
  {
    key: "id",
    title: "ID",
    type: "text",
    icon: Shield,
    sortable: true,
    searchable: true,
    filterable: true,
    description: "Unique identifier for the plan",
    priority: 3,
    expandedOnly: true,
  },
  // Compound column combining image and title for better preview
  {
    key: "planCompound",
    title: "Plan",
    type: "compound",
    disablePrefixSort: true,
    sortable: true,
    searchable: true,
    filterable: true,
    priority: 1,
    icon: Shield,
    render: {
      type: "compound",
      config: {
        image: {
          key: "image", // Row's image URL
          fallback: "/img/placeholder.svg",
          type: "image",
          title: "Image",
          description: "Plan image",
          editable: true,
          usedInCreate: true,
          filterable: false,
          sortable: false,
        },
        primary: {
          key: "title", // Row's display title
          title: "Title",
          description: "Display title",
          editable: true,
          usedInCreate: true,
          sortable: true,
          sortKey: "title",
        },
      },
    },
  },
  {
    key: "name",
    title: "Name",
    type: "text",
    icon: Shield,
    sortable: true,
    searchable: true,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Plan name",
    priority: 1,
    expandedOnly: true,
  },
  {
    key: "description",
    title: "Description",
    type: "text",
    icon: ClipboardList,
    sortable: false,
    searchable: true,
    filterable: false,
    editable: true,
    usedInCreate: true,
    description: "Plan description",
    priority: 3,
    expandedOnly: true,
  },
  {
    key: "walletType",
    title: "Wallet",
    type: "select",
    sortable: true,
    searchable: true,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Type of wallet for the plan",
    priority: 1,
    apiEndpoint: {
      url: "/api/admin/finance/wallet/options",
      method: "GET",
    },
  },
  {
    key: "currency",
    title: "Currency",
    type: "select",
    icon: ClipboardList,
    sortable: true,
    searchable: true,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Currency used for investments",
    priority: 1,
    dynamicSelect: {
      refreshOn: "walletType", // watch the walletType field
      // When walletType changes, call endpointBuilder with its current value.
      endpointBuilder: (walletTypeValue: string | undefined) =>
        walletTypeValue
          ? {
              url: `/api/admin/finance/currency/options?type=${walletTypeValue}`,
              method: "GET",
            }
          : null,
      disableWhenEmpty: true, // disable currency select when walletType is not set
    },
  },
  {
    key: "minAmount",
    title: "Min Amount",
    type: "number",
    sortable: true,
    searchable: false,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Minimum investment amount",
    priority: 1,
  },
  {
    key: "maxAmount",
    title: "Max Amount",
    type: "number",
    sortable: true,
    searchable: false,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Maximum investment amount",
    priority: 1,
  },
  {
    key: "profitPercentage",
    title: "Profit %",
    type: "number",
    sortable: true,
    searchable: false,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Profit percentage",
    priority: 1,
  },
  {
    key: "invested",
    title: "Invested",
    type: "number",
    icon: DollarSign,
    sortable: true,
    searchable: false,
    filterable: true,
    editable: false,
    usedInCreate: false,
    description: "Total amount invested",
    priority: 2,
    expandedOnly: true,
  },
  {
    key: "minProfit",
    title: "Min Profit",
    type: "number",
    icon: DollarSign,
    sortable: true,
    searchable: false,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Minimum profit",
    priority: 2,
    expandedOnly: true,
  },
  {
    key: "maxProfit",
    title: "Max Profit",
    type: "number",
    icon: DollarSign,
    sortable: true,
    searchable: false,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Maximum profit",
    priority: 2,
    expandedOnly: true,
  },
  {
    key: "defaultProfit",
    title: "Default Profit",
    type: "number",
    icon: DollarSign,
    sortable: true,
    searchable: false,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Default profit value",
    priority: 2,
    expandedOnly: true,
  },
  {
    key: "defaultResult",
    title: "Default Result",
    type: "select",
    icon: ClipboardList,
    sortable: true,
    searchable: true,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Default result (WIN, LOSS, DRAW)",
    priority: 2,
    expandedOnly: true,
    options: [
      { value: "WIN", label: "Win" },
      { value: "LOSS", label: "Loss" },
      { value: "DRAW", label: "Draw" },
    ],
  },
  {
    key: "trending",
    title: "Trending",
    type: "boolean",
    sortable: true,
    searchable: true,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Whether the plan is trending",
    priority: 1,
  },
  {
    key: "status",
    title: "Status",
    type: "boolean",
    sortable: true,
    searchable: true,
    filterable: true,
    editable: true,
    usedInCreate: true,
    description: "Plan status",
    priority: 1,
  },
  {
    key: "durations",
    title: "Durations",
    type: "multiselect",
    icon: ClipboardList,
    sortable: false,
    searchable: false,
    filterable: false,
    editable: true,
    usedInCreate: true,
    description: "Available durations for this plan",
    priority: 2,
    apiEndpoint: {
      url: "/api/admin/finance/investment/duration/options",
      method: "GET",
    },
    render: {
      type: "custom",
      render: (value: any, row: any) => renderDurations(value),
    },
  },
  {
    key: "createdAt",
    title: "Created At",
    type: "date",
    icon: ClipboardList,
    sortable: true,
    searchable: true,
    filterable: true,
    description: "Creation date",
    render: { type: "date", format: "PPP" },
    priority: 3,
    expandedOnly: true,
  },
];
