/**
 * TypeScript types for Orders Service
 * Mirrored from backend schemas: services/orders/app/schemas/order_schemas.py, procurement_schemas.py
 * Backend enums: services/orders/app/models/enums.py
 *
 * Coverage:
 * - Customer CRUD (individual, business, central bakery customers)
 * - Order CRUD (orders, order items, order workflow)
 * - Procurement Plans (MRP-style procurement planning)
 * - Procurement Requirements (demand-driven purchasing)
 * - Dashboard & Analytics
 */

// ================================================================
// ENUMS
// ================================================================

/**
 * Customer type classifications
 * Backend: CustomerType enum in models/enums.py (lines 10-18)
 */
export enum CustomerType {
  INDIVIDUAL = 'individual',
  BUSINESS = 'business',
  CENTRAL_BAKERY = 'central_bakery',
  RETAIL = 'RETAIL',
  WHOLESALE = 'WHOLESALE',
  RESTAURANT = 'RESTAURANT',
  HOTEL = 'HOTEL',
  ENTERPRISE = 'ENTERPRISE'
}

export enum DeliveryMethod {
  DELIVERY = 'delivery',
  PICKUP = 'pickup'
}

export enum PaymentTerms {
  IMMEDIATE = 'immediate',
  NET_30 = 'net_30',
  NET_60 = 'net_60'
}

export enum PaymentMethod {
  CASH = 'cash',
  CARD = 'card',
  BANK_TRANSFER = 'bank_transfer',
  ACCOUNT = 'account'
}

export enum PaymentStatus {
  PENDING = 'pending',
  PARTIAL = 'partial',
  PAID = 'paid',
  FAILED = 'failed',
  REFUNDED = 'refunded'
}

export enum CustomerSegment {
  VIP = 'vip',
  REGULAR = 'regular',
  WHOLESALE = 'wholesale'
}

export enum PriorityLevel {
  HIGH = 'high',
  NORMAL = 'normal',
  LOW = 'low'
}

export enum OrderType {
  STANDARD = 'standard',
  RUSH = 'rush',
  RECURRING = 'recurring',
  SPECIAL = 'special'
}

export enum OrderStatus {
  PENDING = 'pending',
  CONFIRMED = 'confirmed',
  IN_PRODUCTION = 'in_production',
  READY = 'ready',
  OUT_FOR_DELIVERY = 'out_for_delivery',
  DELIVERED = 'delivered',
  CANCELLED = 'cancelled',
  FAILED = 'failed'
}

export enum OrderSource {
  MANUAL = 'manual',
  ONLINE = 'online',
  PHONE = 'phone',
  APP = 'app',
  API = 'api'
}

export enum SalesChannel {
  DIRECT = 'direct',
  WHOLESALE = 'wholesale',
  RETAIL = 'retail'
}

export enum BusinessModel {
  INDIVIDUAL_BAKERY = 'individual_bakery',
  CENTRAL_BAKERY = 'central_bakery'
}

// ===== Customer Types =====

export interface CustomerBase {
  name: string;
  business_name?: string;
  customer_type: CustomerType;
  email?: string;
  phone?: string;
  address_line1?: string;
  address_line2?: string;
  city?: string;
  state?: string;
  postal_code?: string;
  country: string;
  is_active: boolean;
  preferred_delivery_method: DeliveryMethod;
  payment_terms: PaymentTerms;
  credit_limit?: number;
  discount_percentage: number;
  customer_segment: CustomerSegment;
  priority_level: PriorityLevel;
  special_instructions?: string;
  delivery_preferences?: Record<string, any>;
  product_preferences?: Record<string, any>;
}

export interface CustomerCreate extends CustomerBase {
  customer_code: string;
  tenant_id: string;
}

export interface CustomerUpdate extends Partial<Omit<CustomerBase, 'country' | 'is_active' | 'preferred_delivery_method' | 'payment_terms' | 'discount_percentage' | 'customer_segment' | 'priority_level'>> {
  country?: string;
  is_active?: boolean;
  preferred_delivery_method?: DeliveryMethod;
  payment_terms?: PaymentTerms;
  discount_percentage?: number;
  customer_segment?: CustomerSegment;
  priority_level?: PriorityLevel;
}

export interface CustomerResponse extends CustomerBase {
  id: string;
  tenant_id: string;
  customer_code: string;
  total_orders: number;
  total_spent: number;
  average_order_value: number;
  last_order_date?: string;
  created_at: string;
  updated_at: string;
}

// ===== Order Item Types =====

export interface OrderItemBase {
  product_id: string;
  product_name: string;
  product_sku?: string;
  product_category?: string;
  quantity: number;
  unit_of_measure: string;
  weight?: number;
  unit_price: number;
  line_discount: number;
  product_specifications?: Record<string, any>;
  customization_details?: string;
  special_instructions?: string;
  recipe_id?: string;
}

export interface OrderItemCreate extends OrderItemBase {}

export interface OrderItemUpdate {
  quantity?: number;
  unit_price?: number;
  line_discount?: number;
  product_specifications?: Record<string, any>;
  customization_details?: string;
  special_instructions?: string;
}

export interface OrderItemResponse extends OrderItemBase {
  id: string;
  order_id: string;
  line_total: number;
  status: string;
  created_at: string;
  updated_at: string;
}

// ===== Order Types =====

export interface OrderBase {
  customer_id: string;
  order_type: OrderType;
  priority: PriorityLevel;
  requested_delivery_date: string;
  delivery_method: DeliveryMethod;
  delivery_address?: Record<string, any>;
  delivery_instructions?: string;
  delivery_window_start?: string;
  delivery_window_end?: string;
  discount_percentage: number;
  delivery_fee: number;
  payment_method?: PaymentMethod;
  payment_terms: PaymentTerms;
  special_instructions?: string;
  custom_requirements?: Record<string, any>;
  allergen_warnings?: Record<string, any>;
  order_source: OrderSource;
  sales_channel: SalesChannel;
  order_origin?: string;
  communication_preferences?: Record<string, any>;
}

export interface OrderCreate extends OrderBase {
  tenant_id: string;
  items: OrderItemCreate[];
}

export interface OrderUpdate {
  status?: OrderStatus;
  priority?: PriorityLevel;
  requested_delivery_date?: string;
  confirmed_delivery_date?: string;
  delivery_method?: DeliveryMethod;
  delivery_address?: Record<string, any>;
  delivery_instructions?: string;
  delivery_window_start?: string;
  delivery_window_end?: string;
  payment_method?: PaymentMethod;
  payment_status?: PaymentStatus;
  special_instructions?: string;
  custom_requirements?: Record<string, any>;
  allergen_warnings?: Record<string, any>;
}

export interface OrderResponse extends OrderBase {
  id: string;
  tenant_id: string;
  order_number: string;
  status: OrderStatus;
  order_date: string;
  confirmed_delivery_date?: string;
  actual_delivery_date?: string;
  subtotal: number;
  discount_amount: number;
  tax_amount: number;
  total_amount: number;
  payment_status: PaymentStatus;
  business_model?: string;
  estimated_business_model?: string;
  production_batch_id?: string;
  quality_score?: number;
  customer_rating?: number;
  created_at: string;
  updated_at: string;
  items: OrderItemResponse[];
}

// ===== Dashboard and Analytics Types =====

export interface OrdersDashboardSummary {
  // Current period metrics
  total_orders_today: number;
  total_orders_this_week: number;
  total_orders_this_month: number;
  
  // Revenue metrics
  revenue_today: number;
  revenue_this_week: number;
  revenue_this_month: number;
  
  // Order status breakdown
  pending_orders: number;
  confirmed_orders: number;
  in_production_orders: number;
  ready_orders: number;
  delivered_orders: number;
  
  // Customer metrics
  total_customers: number;
  new_customers_this_month: number;
  repeat_customers_rate: number;
  
  // Performance metrics
  average_order_value: number;
  order_fulfillment_rate: number;
  on_time_delivery_rate: number;
  
  // Business model detection
  business_model?: string;
  business_model_confidence?: number;
  
  // Recent activity
  recent_orders: OrderResponse[];
  high_priority_orders: OrderResponse[];
}

export interface DemandRequirements {
  date: string;
  tenant_id: string;
  
  // Product demand breakdown
  product_demands: Record<string, any>[];
  
  // Aggregate metrics
  total_orders: number;
  total_quantity: number;
  total_value: number;
  
  // Business context
  business_model?: string;
  rush_orders_count: number;
  special_requirements: string[];
  
  // Timing requirements
  earliest_delivery: string;
  latest_delivery: string;
  average_lead_time_hours: number;
}

export interface BusinessModelDetection {
  business_model: string;
  confidence: string;
  detected_at: string;
}

export interface ServiceStatus {
  service: string;
  status: string;
  timestamp: string;
  tenant_id: string;
}

// ===== Query Parameters Types =====

export interface GetOrdersParams {
  tenant_id: string;
  status_filter?: string;
  start_date?: string;
  end_date?: string;
  skip?: number;
  limit?: number;
}

export interface GetCustomersParams {
  tenant_id: string;
  active_only?: boolean;
  skip?: number;
  limit?: number;
}

export interface UpdateOrderStatusParams {
  tenant_id: string;
  order_id: string;
  new_status: OrderStatus;
  reason?: string;
}

export interface GetDemandRequirementsParams {
  tenant_id: string;
  target_date: string;
}
