import apiClient from './client';

interface OrderItem {
  id: string;
  productId: string;
  productName: string;
  quantity: number;
  price: number;
}

interface Order {
  id: string;
  orderNo: string;
  customerId: string;
  customerName: string;
  totalAmount: number;
  status: 'pending' | 'processing' | 'shipped' | 'delivered' | 'cancelled';
  items: OrderItem[];
  createdAt: string;
  updatedAt: string;
}

interface OrderListResponse {
  data: Order[];
  total: number;
  page: number;
  pageSize: number;
}

export const orderApi = {
  getList: async (params: {
    page?: number;
    pageSize?: number;
    status?: string;
    startDate?: string;
    endDate?: string;
  }): Promise<OrderListResponse> => {
    return apiClient.get('/orders', { params });
  },
  getDetail: async (id: string): Promise<Order> => {
    return apiClient.get(`/orders/${id}`);
  },
  create: async (data: any): Promise<Order> => {
    return apiClient.post('/orders', data);
  },
  updateStatus: async (id: string, status: Order['status']): Promise<Order> => {
    return apiClient.put(`/orders/${id}/status`, { status });
  },
  cancel: async (id: string): Promise<Order> => {
    return apiClient.put(`/orders/${id}/cancel`);
  },
};

export default orderApi;