import { shopRequest } from './http'; export interface ApiBanner { id: number; slug: string; title: string; title_i18n?: Record; subtitle: string; cta_label: string; image_url: string; target_route: string; display_order: number; } export interface ApiCategory { id: number; parent: number | null; slug: string; title: string; localized_title: string; title_i18n?: Record; icon_url: string; small_icon_url: string; accent_color: string; display_order: number; is_quick: boolean; products_count: number; } export interface ApiStoreSummary { id: number; slug: string; name: string; tagline: string; country: string; city: string; origin_label: string; logo_url: string; banner_url: string; badge: string; membership_label: string; sales_label: string; since_label: string; avg_response_time: string; on_time_delivery_rate: number; satisfaction_rate: number; rating_avg: string | number; reviews_count: number; products_count: number; is_verified: boolean; } export interface ApiProductCard { id: number; slug: string; sku: string; title: string; primary_image_url: string; hero_image_url: string; price: string; formatted_price: string; compare_at_price: string | null; formatted_old_price: string | null; discount_badge: string; coin_price: number; currency: string; stock_qty: number; origin_label: string; rating_avg: string | number; reviews_count: number; store: number; store_slug: string; store_name: string; category: number; category_slug: string; category_title: string; is_popular: boolean; is_new_store_pick: boolean; is_bookmarked: boolean; } export interface ApiProductMedia { id: number; image_url: string; alt_text: string; sort_order: number; } export interface ApiProductVariant { id: number; sku: string; title: string; attributes_json: Record; price_override: string | null; coin_price_override: number | null; stock_qty: number; is_active: boolean; } export interface ApiReview { id: number; product: number; author_name: string; rating: number; comment: string; image_url: string; formatted_date: string; created_at: string; } export interface ApiProductDetail extends ApiProductCard { breadcrumb: string; description: string; condition_label: string; processing_time_label: string; product_type_label: string; specs_json: Record; shipping_info_json: Record; store_detail: ApiStoreSummary; media: ApiProductMedia[]; variants: ApiProductVariant[]; reviews: ApiReview[]; related_products: ApiProductCard[]; } export interface ApiStoreDetail extends ApiStoreSummary { description: string; created_at: string; products: ApiProductCard[]; } export interface ApiCartItem { id: number; product_id: number; product_slug: string; title: string; image_url: string; origin_label: string; store_id: number; store_slug: string; store_name: string; variant: number | null; variant_title: string; quantity: number; unit_price: string; formatted_unit_price: string; line_total: string; formatted_line_total: string; coin_unit_price: number; } export interface ApiCartPricing { items_count: number; total_quantity: number; total_units?: number; shops_count: number; subtotal: string; formatted_subtotal: string; shipping_fee: string; formatted_shipping_fee: string; discount_amount: string; formatted_discount: string; formatted_discount_amount?: string; promo_code: string; total: string; total_amount?: string; formatted_total: string; coin_total: number; coins_total?: number; } export interface ApiCart { id: number; currency: string; promo_code: string; items: ApiCartItem[]; pricing: ApiCartPricing; updated_at: string; } export interface ApiAddress { id: number; label: string; recipient_name: string; phone: string; country: string; city: string; street_line: string; postal_code: string; lat: string | number | null; lon: string | number | null; is_default: boolean; full_address: string; created_at: string; } export interface ApiShippingMethod { id: number; code: string; title: string; title_i18n?: Record; subtitle: string; price: string; formatted_price: string; coin_price: number; estimated_days_min: number; estimated_days_max: number; is_default: boolean; } export interface ApiPaymentMethod { id: string; code: string; label: string; icon_url: string; is_default: boolean; balance?: number; } export interface ApiShipmentEvent { id: number; status: string; title: string; location: string; formatted_time: string; occurred_at: string; } export interface ApiShipment { id: number; store: number; booth_name: string; booth_slug: string; booth_avatar: string; status: 'ordered' | 'packed' | 'shipping' | 'delivered' | 'canceled'; carrier_name: string; tracking_code: string; estimated_delivery_label: string; completed_steps: number; thumbnails: string[]; events: ApiShipmentEvent[]; } export interface ApiOrderItem { id: number; product: number | null; product_slug: string; store: number; store_name: string; store_slug: string; title_snapshot: string; image_snapshot: string; origin_snapshot: string; unit_price: string; formatted_unit_price: string; coin_unit_price: number; quantity: number; line_total: string; formatted_line_total: string; } export interface ApiOrderSummary { id: number; order_number: string; status: string; payment_method: string; payment_method_label: string; payment_status: string; currency: string; total_amount: string; formatted_total: string; coins_charged: number; formatted_date: string; formatted_datetime: string; thumbnails: string[]; items_count: number; shops_count: number; delivered_shipments_count: number; total_shipments_count: number; placed_at: string; } export interface ApiOrderDetail extends ApiOrderSummary { subtotal: string; formatted_subtotal: string; shipping_fee: string; formatted_shipping_fee: string; discount_amount: string; formatted_discount: string; promo_code: string; shipping_method_title: string; shipping_address_snapshot: { recipient_name?: string; phone?: string; full_address?: string; city?: string; country?: string; street_line?: string; }; notes: string; items: ApiOrderItem[]; shipments: ApiShipment[]; } export interface ApiOrderCounters { current: number; in_progress: number; delivered: number; canceled: number; returned: number; } export interface ApiUserProfile { is_authenticated: boolean; id?: number; guest_token?: string; full_name: string; email: string; phone_number: string; avatar_url: string; habib_coins_balance: number; shop_provider: Record | null; provider_requests: { id: number; service: string; status: string; public_name: string; created_at: string; }[]; } export interface ApiSupportSubject { id: number; slug: string; label: string; title_i18n?: Record; icon_url: string; display_order: number; } export interface ApiSupportTicket { id: number; ticket_number: string; order: number | null; order_number: string; subject: number | null; subject_label: string; description: string; attachment_url: string; status: string; admin_reply: string; formatted_date: string; created_at: string; } export interface ApiConversation { id: number; store: number; store_detail: ApiStoreSummary; last_message_preview: string; last_message_at: string | null; formatted_last_time: string; unread_user_count: number; created_at: string; } export interface ApiChatMessage { id: number; conversation: number; sender_type: 'customer' | 'store' | 'system'; text: string; attachment_url: string; product_ref: number | null; order_ref: number | null; is_read: boolean; formatted_time: string; date_group: string; created_at: string; } export interface HomeFeedResponse { banners: ApiBanner[]; quick_categories: ApiCategory[]; categories: ApiCategory[]; popular_products: ApiProductCard[]; new_store_products: ApiProductCard[]; featured_stores: ApiStoreSummary[]; cart_items_count: number; habib_coins_balance: number; } export const shopApi = { // Home & Catalog fetchHomeFeed: () => shopRequest('/home/'), fetchCategories: (q?: string) => shopRequest<{ results: ApiCategory[] }>('/categories/', { query: { q } }), fetchCategoryDetail: (slug: string, q?: string) => shopRequest<{ category: ApiCategory; products: ApiProductCard[] }>( `/categories/${encodeURIComponent(slug)}/`, { query: { q } } ), fetchProducts: (params?: { q?: string; category?: string; store?: string; popular?: boolean; new_store?: boolean; sort?: 'price_asc' | 'price_desc' | 'rating' | 'newest'; }) => shopRequest<{ count: number; results: ApiProductCard[] }>('/products/', { query: params, }), fetchProductDetail: (idOrSlug: string | number) => shopRequest(`/products/${encodeURIComponent(String(idOrSlug))}/`), fetchProductReviews: (idOrSlug: string | number) => shopRequest<{ count: number; results: ApiReview[] }>( `/products/${encodeURIComponent(String(idOrSlug))}/reviews/` ), createProductReview: ( idOrSlug: string | number, payload: { rating: number; comment: string; author_name?: string; image_url?: string } ) => shopRequest( `/products/${encodeURIComponent(String(idOrSlug))}/reviews/`, { method: 'POST', body: payload } ), fetchStores: (q?: string) => shopRequest<{ count: number; results: ApiStoreSummary[] }>('/stores/', { query: { q }, }), fetchStoreDetail: (idOrSlug: string | number) => shopRequest(`/stores/${encodeURIComponent(String(idOrSlug))}/`), // Cart & Checkout fetchCart: (includeShipping = false) => shopRequest('/cart/', { query: { include_shipping: includeShipping ? '1' : undefined }, }), clearCart: () => shopRequest('/cart/', { method: 'DELETE' }), addCartItem: (payload: { product_id: string | number; variant_id?: number | null; quantity?: number; }) => shopRequest('/cart/items/', { method: 'POST', body: payload }), updateCartItem: (itemId: number, quantity: number) => shopRequest(`/cart/items/${itemId}/`, { method: 'PATCH', body: { quantity }, }), removeCartItem: (itemId: number) => shopRequest(`/cart/items/${itemId}/`, { method: 'DELETE' }), mergeGuestCart: (guestToken?: string) => shopRequest('/cart/merge/', { method: 'POST', body: guestToken ? { guest_token: guestToken } : {}, }), applyPromoCode: (promoCode: string) => shopRequest('/cart/promo/', { method: 'POST', body: { promo_code: promoCode }, }), fetchShippingMethods: () => shopRequest<{ results: ApiShippingMethod[] }>('/checkout/shipping-methods/'), fetchCheckoutSummary: (shippingMethodCodeOrId?: string | number) => shopRequest<{ cart: ApiCart; shipping_method: ApiShippingMethod; pricing: ApiCartPricing; habib_coins_balance: number; }>('/checkout/summary/', { query: { shipping_method: shippingMethodCodeOrId }, }), fetchPaymentMethods: () => shopRequest<{ habib_coins_balance: number; results: ApiPaymentMethod[] }>( '/payment/methods/' ), // Orders & Shipments fetchOrders: (status?: 'current' | 'delivered' | 'canceled') => shopRequest<{ count: number; results: ApiOrderSummary[] }>('/orders/', { query: { status }, }), fetchOrderCounters: () => shopRequest('/orders/counters/'), placeOrder: (payload: { address_id?: number | null; shipping_address?: Record; shipping_method?: string | number; payment_method: string; promo_code?: string; notes?: string; }) => shopRequest('/orders/', { method: 'POST', body: payload }), fetchOrderDetail: (orderNumber: string) => shopRequest(`/orders/${encodeURIComponent(orderNumber)}/`), fetchOrderReceipt: (orderNumber: string) => shopRequest<{ order_number: string; status: string; payment_status: string; payment_method: string; payment_method_label: string; total_paid: string; coins_charged: number; order_date: string; shipping_address: Record; }>(`/orders/${encodeURIComponent(orderNumber)}/receipt/`), cancelOrder: (orderNumber: string) => shopRequest(`/orders/${encodeURIComponent(orderNumber)}/cancel/`, { method: 'POST', }), fetchShipmentTracking: (orderNumber: string, shipmentId: number) => shopRequest( `/orders/${encodeURIComponent(orderNumber)}/shipments/${shipmentId}/` ), // Profile, Addresses, Bookmarks & Provider fetchProfile: () => shopRequest('/me/profile/'), updateProfile: (payload: { full_name?: string; email?: string; phone_number?: string; }) => shopRequest('/me/profile/', { method: 'PUT', body: payload }), fetchAddresses: () => shopRequest<{ results: ApiAddress[] }>('/me/addresses/'), createAddress: (payload: { label?: string; recipient_name: string; phone?: string; country?: string; city?: string; street_line: string; postal_code?: string; lat?: number | null; lon?: number | null; is_default?: boolean; }) => shopRequest('/me/addresses/', { method: 'POST', body: payload }), updateAddress: ( id: number, payload: Partial<{ label: string; recipient_name: string; phone: string; country: string; city: string; street_line: string; postal_code: string; lat: number | null; lon: number | null; is_default: boolean; }> ) => shopRequest(`/me/addresses/${id}/`, { method: 'PATCH', body: payload, }), deleteAddress: (id: number) => shopRequest(`/me/addresses/${id}/`, { method: 'DELETE' }), fetchBookmarks: () => shopRequest<{ count: number; results: { id: number; product: number; product_detail: ApiProductCard }[]; }>('/me/bookmarks/'), addBookmark: (productIdOrSlug: string | number) => shopRequest<{ id: number; product: number; product_detail: ApiProductCard }>( '/me/bookmarks/', { method: 'POST', body: { product_id: productIdOrSlug } } ), removeBookmark: (productIdOrSlug: string | number) => shopRequest( `/me/bookmarks/${encodeURIComponent(String(productIdOrSlug))}/`, { method: 'DELETE' } ), applyAsProvider: (payload: { store_name: string; description?: string }) => shopRequest<{ id?: number; service: string; status: string; public_name: string; message: string; }>('/providers/apply/', { method: 'POST', body: payload }), // Support & Chat fetchSupportSubjects: () => shopRequest<{ results: ApiSupportSubject[] }>('/support/subjects/'), fetchSupportTickets: () => shopRequest<{ count: number; results: ApiSupportTicket[] }>('/support/tickets/'), createSupportTicket: (payload: { order_number?: string; product_id?: string | number; subject?: string | number; subject_label?: string; description: string; attachment_url?: string; }) => shopRequest('/support/tickets/', { method: 'POST', body: payload, }), fetchChatConversations: () => shopRequest<{ count: number; results: ApiConversation[] }>('/chat/conversations/'), createChatConversation: (storeIdOrSlug: string | number) => shopRequest('/chat/conversations/', { method: 'POST', body: { store_id: storeIdOrSlug }, }), fetchChatMessages: (pkOrSlug: string | number, afterId?: number) => shopRequest<{ conversation: ApiConversation; results: ApiChatMessage[] }>( `/chat/conversations/${encodeURIComponent(String(pkOrSlug))}/messages/`, { query: { after_id: afterId } } ), sendChatMessage: ( pkOrSlug: string | number, payload: { text?: string; attachment_url?: string } ) => shopRequest<{ conversation: ApiConversation; message: ApiChatMessage; new_messages: ApiChatMessage[]; }>( `/chat/conversations/${encodeURIComponent(String(pkOrSlug))}/messages/`, { method: 'POST', body: payload } ), };