import { apiClient } from "./client";
import type { Order } from "../types/models";

export interface CreateOrderParams {
  symbol: string;
  side: "buy" | "sell";
  type: "market" | "limit" | "stop" | "stop_limit";
  quantity: number;
  price?: number;
  stopPrice?: number;
}

export interface GetOrdersParams {
  symbol?: string;
  status?: string;
  limit?: number;
  startTime?: string;
  endTime?: string;
}

export async function createOrder(params: CreateOrderParams): Promise<Order> {
  const response = await apiClient.post<Order>("/api/orders", params);
  return response.data;
}

export async function cancelOrder(orderId: string): Promise<Order> {
  const response = await apiClient.delete<Order>(`/api/orders/${orderId}`);
  return response.data;
}

interface OrdersResponse {
  status: string;
  orders: Order[];
  total: number;
}

export async function getOrders(params?: GetOrdersParams): Promise<Order[]> {
  const response = await apiClient.get<OrdersResponse>("/api/orders", {
    params,
  });
  return response.data.orders ?? [];
}

export async function getOpenOrders(symbol?: string): Promise<Order[]> {
  const response = await apiClient.get<Order[]>("/api/orders/open", {
    params: symbol ? { symbol } : undefined,
  });
  return response.data;
}
