import React from "react";
import { Edit2 } from "lucide-react";
import { RecentTransactions } from "@/hooks/useAssetAnalyticsReport";
import { stripHtmlTags, formatNumberWithHtml, getAmountValue, getAmountColor } from "@/lib/asset-analytics-utils";

interface RecentTransactionsTableProps {
  recentTrans: RecentTransactions;
}

export const RecentTransactionsTable: React.FC<RecentTransactionsTableProps> = ({ recentTrans }) => {
  if (!recentTrans || recentTrans.transactions.length === 0) {
    return null;
  }

  return (
    <div className="rounded-lg border border-border bg-card shadow-sm">
      <div className="border-b border-border bg-muted px-6 py-4">
        <h3 className="text-lg font-semibold text-primary">
          Recent Transactions
        </h3>
      </div>
      <div className="overflow-x-auto">
        <table className="w-full">
          <thead className="bg-muted border-b border-border">
            <tr>
              <th className="px-4 py-3 text-left text-xs font-semibold text-muted-foreground">
                Ref. ID
              </th>
              <th className="px-4 py-3 text-left text-xs font-semibold text-muted-foreground">
                Placement Date
              </th>
              <th className="px-4 py-3 text-left text-xs font-semibold text-muted-foreground">
                Bank
              </th>
              <th className="px-4 py-3 text-left text-xs font-semibold text-muted-foreground">
                Security Ticker
              </th>
              <th className="px-4 py-3 text-left text-xs font-semibold text-muted-foreground">
                Name
              </th>
              <th className="px-4 py-3 text-left text-xs font-semibold text-muted-foreground">
                Category
              </th>
              <th className="px-4 py-3 text-right text-xs font-semibold text-muted-foreground">
                Price
              </th>
              <th className="px-4 py-3 text-right text-xs font-semibold text-muted-foreground">
                Quantity
              </th>
              <th className="px-4 py-3 text-left text-xs font-semibold text-muted-foreground">
                Currency
              </th>
              <th className="px-4 py-3 text-right text-xs font-semibold text-muted-foreground">
                Amount
              </th>
              <th className="px-4 py-3 text-center text-xs font-semibold text-muted-foreground">
                Options
              </th>
            </tr>
          </thead>
          <tbody className="divide-y divide-border">
            {recentTrans.transactions.map((trans, idx) => (
              <tr key={idx} className="hover:bg-muted">
                <td className="px-4 py-3 text-sm text-foreground">
                  {stripHtmlTags(trans.uid_title)}
                </td>
                <td className="px-4 py-3 text-sm text-foreground">
                  {trans.transaction_date}
                </td>
                <td className="px-4 py-3 text-sm text-foreground">
                  {trans.bank_name}
                </td>
                <td className="px-4 py-3 text-sm text-foreground">
                  {trans.ticker}
                </td>
                <td className="px-4 py-3 text-sm text-foreground">
                  {trans.isin}
                </td>
                <td className="px-4 py-3 text-sm text-foreground">
                  {trans.f_type}
                </td>
                <td className="px-4 py-3 text-right text-sm text-foreground">
                  {trans.price}
                </td>
                <td className="px-4 py-3 text-right text-sm text-foreground">
                  {formatNumberWithHtml(trans.quantity)}
                </td>
                <td className="px-4 py-3 text-sm text-foreground">
                  {trans.currency}
                </td>
                <td className={`px-4 py-3 text-right text-sm font-semibold ${getAmountColor(trans.amount)}`}>
                  {formatNumberWithHtml(getAmountValue(trans.amount))}
                </td>
                <td className="px-4 py-3 text-center">
                  <a
                    href={trans.view_url}
                    className="inline-flex items-center justify-center w-8 h-8 rounded-lg border border-border-strong bg-card hover:bg-muted"
                    title="Edit"
                  >
                    <Edit2 className="h-4 w-4 text-muted-foreground" />
                  </a>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
};
