// Mock data + a thin API client stub for the self-hosted MongoDB/Node backend.
// Replace the BASE_URL and wire real fetch() calls when the backend is up.
export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "http://backend.thebyteflare.com/api/";

export type Trend = "BULLISH" | "BEARISH" | "SIDEWAYS";
export type PositionSide = "LONG" | "SHORT";
export type TradeStatus = "WAITING_TRIGGER" | "OPEN" | "COMPLETED" | "LIQUIDATED";

export interface CoinTick {
  symbol: string;
  price: number;
  change24h: number;
  volume24h: number;
  rsi: number;
  trend: Trend;
  ema_cross: "GOLDEN" | "DEATH" | "NEUTRAL";
}

export interface ActiveTrade {
  _id: string;
  coin_symbol: string;
  position_side: PositionSide;
  order_sizing_type: "FIXED_AMOUNT" | "PERCENTAGE";
  order_size_value: number;
  leverage: number;
  entry_price: number;
  current_price: number;
  current_status: TradeStatus;
  stop_loss: number;
  take_profit: number;
  max_wait_time_minutes: number;
  opened_at: string;
  pnl: number;
  pnl_pct: number;
}

export const MOCK_COINS: CoinTick[] = [
  { symbol: "BTCUSDT", price: 68420.5, change24h: 2.34, volume24h: 1.2e9, rsi: 62, trend: "BULLISH", ema_cross: "GOLDEN" },
  { symbol: "ETHUSDT", price: 3542.18, change24h: 1.12, volume24h: 8.4e8, rsi: 58, trend: "BULLISH", ema_cross: "GOLDEN" },
  { symbol: "SOLUSDT", price: 168.42, change24h: -1.85, volume24h: 3.2e8, rsi: 44, trend: "BEARISH", ema_cross: "DEATH" },
  { symbol: "XRPUSDT", price: 0.6218, change24h: 0.24, volume24h: 1.8e8, rsi: 52, trend: "SIDEWAYS", ema_cross: "NEUTRAL" },
  { symbol: "AVAXUSDT", price: 38.72, change24h: 3.42, volume24h: 1.4e8, rsi: 68, trend: "BULLISH", ema_cross: "GOLDEN" },
  { symbol: "DOGEUSDT", price: 0.1642, change24h: -2.14, volume24h: 2.1e8, rsi: 38, trend: "BEARISH", ema_cross: "DEATH" },
  { symbol: "LINKUSDT", price: 18.24, change24h: 1.86, volume24h: 9.8e7, rsi: 61, trend: "BULLISH", ema_cross: "GOLDEN" },
  { symbol: "MATICUSDT", price: 0.7412, change24h: -0.42, volume24h: 8.2e7, rsi: 49, trend: "SIDEWAYS", ema_cross: "NEUTRAL" },
];

export const MOCK_TRADES: ActiveTrade[] = [
  {
    _id: "t1", coin_symbol: "BTCUSDT", position_side: "LONG",
    order_sizing_type: "PERCENTAGE", order_size_value: 5, leverage: 10,
    entry_price: 67200, current_price: 68420.5, current_status: "OPEN",
    stop_loss: 66500, take_profit: 69800, max_wait_time_minutes: 240,
    opened_at: new Date(Date.now() - 42 * 60_000).toISOString(),
    pnl: 122.05, pnl_pct: 1.82,
  },
  {
    _id: "t2", coin_symbol: "SOLUSDT", position_side: "SHORT",
    order_sizing_type: "FIXED_AMOUNT", order_size_value: 250, leverage: 5,
    entry_price: 171.2, current_price: 168.42, current_status: "OPEN",
    stop_loss: 174.0, take_profit: 162.0, max_wait_time_minutes: 180,
    opened_at: new Date(Date.now() - 22 * 60_000).toISOString(),
    pnl: 40.6, pnl_pct: 1.62,
  },
  {
    _id: "t3", coin_symbol: "ETHUSDT", position_side: "LONG",
    order_sizing_type: "PERCENTAGE", order_size_value: 3, leverage: 8,
    entry_price: 3560, current_price: 3542.18, current_status: "OPEN",
    stop_loss: 3510, take_profit: 3640, max_wait_time_minutes: 120,
    opened_at: new Date(Date.now() - 8 * 60_000).toISOString(),
    pnl: -14.28, pnl_pct: -0.5,
  },
];

export function fmtUsd(n: number | null | undefined, digits = 2) {
  if (n == null || !Number.isFinite(n)) return "—";
  return n.toLocaleString("en-US", { style: "currency", currency: "USD", minimumFractionDigits: digits, maximumFractionDigits: digits });
}
export function fmtNum(n: number | null | undefined, digits = 2) {
  if (n == null || !Number.isFinite(n)) return "—";
  return n.toLocaleString("en-US", { minimumFractionDigits: digits, maximumFractionDigits: digits });
}
export function fmtCompact(n: number | null | undefined) {
  if (n == null || !Number.isFinite(n)) return "—";
  return Intl.NumberFormat("en", { notation: "compact", maximumFractionDigits: 2 }).format(n);
}

/** Placeholder API calls — swap for real fetch() when backend is ready. */
export const api = {
  async closeTrade(tradeId: string): Promise<{ ok: boolean }> {
    await new Promise((r) => setTimeout(r, 400));
    return { ok: true, ...{ tradeId } };
  },
};
