import { http } from './client';
import type { PaginationParams } from './client';
import type {
  WashingPlan,
  FinishedProduct,
  FinishedProductInventoryRecord,
  Payment,
} from '../types';
import type { WashingCompletionFormData } from '../types';

// --- Dashboard ---

export interface WashingDashboardStats {
  total_plans: number;
  processing_plans: number;
  completed_plans: number;
  total_finished_products: number;
  total_stock_meters: number;
  pending_receivable: number;
}

export const washingApi = {
  // Dashboard
  dashboardStats: () =>
    http.get<WashingDashboardStats>('/washing/dashboard/stats').then(r => r.data),

  // Plans
  listPlans: (params?: PaginationParams) =>
    http.get<WashingPlan[]>('/washing/plans', params as Record<string, number>),

  listPendingPlans: () =>
    http.get<WashingPlan[]>('/washing/plans/pending').then(r => r.data),

  listCompletedPlans: () =>
    http.get<WashingPlan[]>('/washing/plans/completed').then(r => r.data),

  getPlan: (id: string) =>
    http.get<WashingPlan>(`/washing/plans/${id}`).then(r => r.data),

  updatePlanStatus: (id: string, data: { status: string }) =>
    http.patch(`/washing/plans/${id}/status`, data),

  // Completions
  completePlan: (planId: string, data: WashingCompletionFormData) =>
    http.post(`/washing/plans/${planId}/complete`, data),

  // Finished Products
  listFinishedProducts: (params?: PaginationParams) =>
    http.get<FinishedProduct[]>('/washing/finished-products', params as Record<string, number>),

  createFinishedProduct: (data: Partial<FinishedProduct>) =>
    http.post<FinishedProduct>('/washing/finished-products', data).then(r => r.data),

  outboundFinishedProduct: (id: string, data: { rolls: number; meters: number; notes?: string }) =>
    http.post<FinishedProductInventoryRecord>(`/washing/finished-products/${id}/outbound`, data).then(r => r.data),

  // Payments
  listPayments: (params?: PaginationParams) =>
    http.get<Payment[]>('/washing/payments', params as Record<string, number>),

  confirmPayment: (id: string) =>
    http.post<Payment>(`/washing/payments/${id}/confirm`).then(r => r.data),
};
