import { TrendingUp, TrendingDown, DollarSign, Activity } from 'lucide-react';
import type { Portfolio } from '@/types';
import { formatPrice, formatPercent, formatNumber } from '@/utils/indicators';

interface PortfolioTrackerProps {
  portfolio: Portfolio;
}

export default function PortfolioTracker({ portfolio }: PortfolioTrackerProps) {
  const isProfitable = portfolio.totalPnl >= 0;

  return (
    <div className="card">
      <h3 className="text-lg font-semibold mb-4">Portfolio</h3>

      <div className="grid grid-cols-2 gap-4 mb-6">
        <div className="bg-dark-bg rounded-lg p-4">
          <div className="flex items-center gap-2 mb-1">
            <DollarSign className="w-4 h-4 text-gray-400" />
            <span className="text-sm text-gray-400">Total Value</span>
          </div>
          <p className="text-2xl font-bold">{formatPrice(portfolio.totalValue)}</p>
        </div>

        <div className="bg-dark-bg rounded-lg p-4">
          <div className="flex items-center gap-2 mb-1">
            {isProfitable ? (
              <TrendingUp className="w-4 h-4 text-green-500" />
            ) : (
              <TrendingDown className="w-4 h-4 text-red-500" />
            )}
            <span className="text-sm text-gray-400">Total P&L</span>
          </div>
          <p
            className={`text-2xl font-bold ${
              isProfitable ? 'text-green-500' : 'text-red-500'
            }`}
          >
            {formatPrice(portfolio.totalPnl)}
          </p>
          <p
            className={`text-sm ${isProfitable ? 'text-green-500' : 'text-red-500'}`}
          >
            {formatPercent(portfolio.totalPnlPercent)}
          </p>
        </div>

        <div className="bg-dark-bg rounded-lg p-4">
          <div className="flex items-center gap-2 mb-1">
            <DollarSign className="w-4 h-4 text-gray-400" />
            <span className="text-sm text-gray-400">Cash</span>
          </div>
          <p className="text-xl font-semibold">{formatPrice(portfolio.cash)}</p>
        </div>

        <div className="bg-dark-bg rounded-lg p-4">
          <div className="flex items-center gap-2 mb-1">
            <Activity className="w-4 h-4 text-gray-400" />
            <span className="text-sm text-gray-400">Trades</span>
          </div>
          <p className="text-xl font-semibold">{portfolio.trades.length}</p>
        </div>
      </div>

      {portfolio.position && (
        <div className="bg-dark-bg rounded-lg p-4 mb-4">
          <h4 className="font-semibold mb-3">Current Position</h4>
          <div className="space-y-2 text-sm">
            <div className="flex justify-between">
              <span className="text-gray-400">Symbol</span>
              <span className="font-medium">{portfolio.position.symbol}</span>
            </div>
            <div className="flex justify-between">
              <span className="text-gray-400">Quantity</span>
              <span className="font-medium">{formatNumber(portfolio.position.quantity)}</span>
            </div>
            <div className="flex justify-between">
              <span className="text-gray-400">Avg Price</span>
              <span className="font-medium">{formatPrice(portfolio.position.avgPrice)}</span>
            </div>
            <div className="flex justify-between">
              <span className="text-gray-400">Current Price</span>
              <span className="font-medium">{formatPrice(portfolio.position.currentPrice)}</span>
            </div>
            <div className="flex justify-between border-t border-dark-border pt-2">
              <span className="text-gray-400">Unrealized P&L</span>
              <span
                className={`font-semibold ${
                  portfolio.position.unrealizedPnl >= 0 ? 'text-green-500' : 'text-red-500'
                }`}
              >
                {formatPrice(portfolio.position.unrealizedPnl)} (
                {formatPercent(portfolio.position.unrealizedPnlPercent)})
              </span>
            </div>
          </div>
        </div>
      )}

      {portfolio.trades.length > 0 && (
        <div>
          <h4 className="font-semibold mb-3">Recent Trades</h4>
          <div className="space-y-2 max-h-48 overflow-y-auto">
            {portfolio.trades.slice(-5).reverse().map((trade) => (
              <div
                key={trade.id}
                className="bg-dark-bg rounded-lg p-3 text-sm flex justify-between items-center"
              >
                <div>
                  <span
                    className={`font-semibold ${
                      trade.action === 'BUY' ? 'text-green-500' : 'text-red-500'
                    }`}
                  >
                    {trade.action}
                  </span>
                  <span className="text-gray-400 ml-2">
                    {formatNumber(trade.quantity)} @ {formatPrice(trade.price)}
                  </span>
                </div>
                <div className="text-right">
                  <p className="font-medium">{formatPrice(trade.total)}</p>
                  {trade.pnl !== undefined && (
                    <p
                      className={`text-xs ${
                        trade.pnl >= 0 ? 'text-green-500' : 'text-red-500'
                      }`}
                    >
                      {formatPrice(trade.pnl)}
                    </p>
                  )}
                </div>
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  );
}
