import type { ApiResponse, PaginatedResponse } from '@alqove/types';
import type { AlqoveClient } from '../client';
import type { OrderDetail, LabelRateOption } from './purchases';

export interface SellerCancelBody {
  reason: 'sold_locally' | 'item_damaged' | 'other';
  note?: string | null;
}

export function createOrderEndpoints(client: AlqoveClient) {
  return {
    list(storeId: string, params?: Record<string, string>) {
      return client.get<PaginatedResponse<OrderDetail>>(
        `/v1/stores/${storeId}/orders`,
        params,
      );
    },

    get(storeId: string, orderId: string) {
      return client.get<ApiResponse<OrderDetail>>(
        `/v1/stores/${storeId}/orders/${orderId}`,
      );
    },

    // Buyer-scoped cancel (existing)
    cancel(orderId: string) {
      return client.post<ApiResponse<OrderDetail>>(`/v1/orders/${orderId}/cancel`, {});
    },

    // Seller-scoped cancel (new)
    storeCancel(storeId: string, orderId: string, body: SellerCancelBody) {
      return client.post<ApiResponse<OrderDetail>>(
        `/v1/stores/${storeId}/orders/${orderId}/cancel`,
        body,
      );
    },

    buyLabel(
      storeId: string,
      orderId: string,
      body: { parcel_preset_id: string },
    ) {
      return client.post<ApiResponse<OrderDetail>>(
        `/v1/stores/${storeId}/orders/${orderId}/labels`,
        body,
      );
    },

    previewLabel(
      storeId: string,
      orderId: string,
      body: { parcel_preset_id: string },
    ) {
      return client.post<ApiResponse<LabelRateOption[]>>(
        `/v1/stores/${storeId}/orders/${orderId}/labels/preview`,
        body,
      );
    },
  };
}
