/**
 * Suppliers service TypeScript type definitions
 * Mirrored from backend API schemas
 */

// Enums
export enum SupplierType {
  INGREDIENTS = 'ingredients',
  PACKAGING = 'packaging',
  EQUIPMENT = 'equipment',
  SERVICES = 'services',
  UTILITIES = 'utilities',
  MULTI = 'multi',
}

export enum SupplierStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  PENDING_APPROVAL = 'pending_approval',
  SUSPENDED = 'suspended',
  BLACKLISTED = 'blacklisted',
}

export enum PaymentTerms {
  COD = 'cod',
  NET_15 = 'net_15',
  NET_30 = 'net_30',
  NET_45 = 'net_45',
  NET_60 = 'net_60',
  PREPAID = 'prepaid',
  CREDIT_TERMS = 'credit_terms',
}

export enum PurchaseOrderStatus {
  DRAFT = 'draft',
  PENDING_APPROVAL = 'pending_approval',
  APPROVED = 'approved',
  SENT_TO_SUPPLIER = 'sent_to_supplier',
  CONFIRMED = 'confirmed',
  PARTIALLY_RECEIVED = 'partially_received',
  COMPLETED = 'completed',
  CANCELLED = 'cancelled',
  DISPUTED = 'disputed',
}

export enum DeliveryStatus {
  SCHEDULED = 'scheduled',
  IN_TRANSIT = 'in_transit',
  OUT_FOR_DELIVERY = 'out_for_delivery',
  DELIVERED = 'delivered',
  PARTIALLY_DELIVERED = 'partially_delivered',
  FAILED_DELIVERY = 'failed_delivery',
  RETURNED = 'returned',
}

export enum QualityRating {
  EXCELLENT = 5,
  GOOD = 4,
  AVERAGE = 3,
  POOR = 2,
  VERY_POOR = 1,
}

export enum DeliveryRating {
  EXCELLENT = 5,
  GOOD = 4,
  AVERAGE = 3,
  POOR = 2,
  VERY_POOR = 1,
}

export enum InvoiceStatus {
  PENDING = 'pending',
  APPROVED = 'approved',
  PAID = 'paid',
  OVERDUE = 'overdue',
  DISPUTED = 'disputed',
  CANCELLED = 'cancelled',
}

export enum OrderPriority {
  NORMAL = 'normal',
  HIGH = 'high',
  URGENT = 'urgent',
}

export enum PerformanceMetricType {
  DELIVERY_PERFORMANCE = 'delivery_performance',
  QUALITY_SCORE = 'quality_score',
  PRICE_COMPETITIVENESS = 'price_competitiveness',
  ORDER_ACCURACY = 'order_accuracy',
}

export enum AlertSeverity {
  CRITICAL = 'critical',
  HIGH = 'high',
  MEDIUM = 'medium',
  LOW = 'low',
}

// Supplier Management Types
export interface SupplierCreate {
  name: string;
  supplier_code?: string;
  tax_id?: string;
  supplier_type: SupplierType;
  contact_person?: string;
  email?: string;
  phone?: string;
  address_line1?: string;
  address_line2?: string;
  city?: string;
  state?: string;
  postal_code?: string;
  country?: string;
  payment_terms?: PaymentTerms;
  credit_limit?: number;
  currency?: string;
  standard_lead_time?: number; // in days
  minimum_order_amount?: number;
  certifications?: Record<string, any>;
  business_hours?: Record<string, any>;
  specializations?: Record<string, any>;
  notes?: string;
}

export interface SupplierUpdate extends Partial<SupplierCreate> {
  status?: SupplierStatus;
}

export interface SupplierResponse {
  id: string;
  tenant_id: string;
  name: string;
  supplier_code: string;
  tax_id?: string;
  supplier_type: SupplierType;
  status: SupplierStatus;
  contact_person?: string;
  email?: string;
  phone?: string;
  address_line1?: string;
  address_line2?: string;
  city?: string;
  state?: string;
  postal_code?: string;
  country?: string;
  payment_terms: PaymentTerms;
  credit_limit: number;
  currency: string;
  standard_lead_time: number;
  minimum_order_amount: number;
  certifications: Record<string, any>;
  business_hours: Record<string, any>;
  specializations: Record<string, any>;
  notes?: string;
  created_at: string; // ISO 8601 date string
  updated_at: string; // ISO 8601 date string
  created_by?: string;
  updated_by?: string;
}

export interface SupplierSummary {
  id: string;
  name: string;
  supplier_code: string;
  supplier_type: SupplierType;
  status: SupplierStatus;
  contact_person?: string;
  email?: string;
  phone?: string;
  city?: string;
  country?: string;
  payment_terms: PaymentTerms;
  created_at: string;
}

// Approval Workflow
export interface SupplierApproval {
  action: 'approve' | 'reject';
  notes?: string;
}

// Purchase Orders
export interface PurchaseOrderItem {
  inventory_product_id: string;
  product_code: string;
  product_name?: string;
  ordered_quantity: number;
  unit_of_measure: string;
  unit_price: number;
  total_price?: number; // calculated field
  quality_requirements?: string;
  notes?: string;
}

export interface PurchaseOrderCreate {
  supplier_id: string;
  reference_number?: string;
  priority: OrderPriority;
  required_delivery_date?: string; // ISO 8601 date string
  delivery_address?: string;
  tax_amount?: number;
  shipping_cost?: number;
  discount_amount?: number;
  notes?: string;
  items: PurchaseOrderItem[];
}

export interface PurchaseOrderUpdate extends Partial<PurchaseOrderCreate> {
  status?: PurchaseOrderStatus;
}

export interface PurchaseOrderResponse {
  id: string;
  tenant_id: string;
  supplier_id: string;
  supplier_name: string;
  reference_number: string;
  status: PurchaseOrderStatus;
  priority: OrderPriority;
  subtotal: number;
  tax_amount: number;
  shipping_cost: number;
  discount_amount: number;
  total_amount: number;
  currency: string;
  order_date: string; // ISO 8601 date string
  required_delivery_date?: string;
  delivery_address?: string;
  approved_at?: string;
  approved_by?: string;
  sent_at?: string;
  confirmed_at?: string;
  notes?: string;
  items: PurchaseOrderItem[];
  created_at: string;
  updated_at: string;
  created_by: string;
  updated_by?: string;
}

export interface PurchaseOrderApproval {
  action: 'approve' | 'reject';
  notes?: string;
}

// Deliveries
export interface DeliveryItem {
  purchase_order_item_id: string;
  product_code: string;
  ordered_quantity: number;
  delivered_quantity: number;
  quality_rating?: number; // 1-5 scale
  quality_notes?: string;
  expiry_date?: string; // ISO 8601 date string
  batch_number?: string;
  temperature_on_arrival?: number;
  condition_notes?: string;
}

export interface DeliveryCreate {
  purchase_order_id: string;
  scheduled_date?: string; // ISO 8601 date string
  delivery_window_start?: string;
  delivery_window_end?: string;
  delivery_address?: string;
  carrier_name?: string;
  tracking_number?: string;
  special_instructions?: string;
  items: DeliveryItem[];
}

export interface DeliveryUpdate extends Partial<DeliveryCreate> {
  status?: DeliveryStatus;
  actual_delivery_date?: string;
  received_by?: string;
  delivery_notes?: string;
}

export interface DeliveryResponse {
  id: string;
  tenant_id: string;
  purchase_order_id: string;
  supplier_id: string;
  supplier_name: string;
  reference_number: string;
  status: DeliveryStatus;
  scheduled_date?: string;
  actual_delivery_date?: string;
  delivery_window_start?: string;
  delivery_window_end?: string;
  delivery_address?: string;
  carrier_name?: string;
  tracking_number?: string;
  special_instructions?: string;
  received_by?: string;
  delivery_notes?: string;
  items: DeliveryItem[];
  created_at: string;
  updated_at: string;
}

export interface DeliveryReceiptConfirmation {
  received_by: string;
  receipt_date: string; // ISO 8601 date string
  general_notes?: string;
  items: {
    delivery_item_id: string;
    accepted_quantity: number;
    rejected_quantity?: number;
    quality_rating: number; // 1-5 scale
    quality_notes?: string;
    condition_issues?: string[];
  }[];
}

// Performance Tracking
export interface PerformanceCalculationRequest {
  period?: 'week' | 'month' | 'quarter' | 'year' | 'custom';
  period_start?: string; // ISO 8601 date string
  period_end?: string;   // ISO 8601 date string
}

export interface PerformanceMetrics {
  supplier_id: string;
  tenant_id: string;
  calculation_period: {
    start_date: string;
    end_date: string;
  };
  delivery_performance: {
    score: number; // 0-100
    on_time_deliveries: number;
    total_deliveries: number;
    average_delay_days: number;
  };
  quality_score: {
    score: number; // 0-100
    total_ratings: number;
    average_rating: number; // 1-5 scale
    rejection_rate: number; // 0-1
  };
  price_competitiveness: {
    score: number; // 0-100
    average_price_vs_market: number;
    cost_savings: number;
  };
  order_accuracy: {
    score: number; // 0-100
    accurate_orders: number;
    total_orders: number;
    error_types: Record<string, number>;
  };
  overall_score: number; // 0-100
  calculated_at: string; // ISO 8601 date string
}

export interface PerformanceAlert {
  id: string;
  supplier_id: string;
  tenant_id: string;
  metric_type: PerformanceMetricType;
  severity: AlertSeverity;
  threshold_value: number;
  actual_value: number;
  message: string;
  created_at: string;
  resolved_at?: string;
}

// Statistics and Analytics
export interface SupplierStatistics {
  total_suppliers: number;
  active_suppliers: number;
  suppliers_by_type: Record<SupplierType, number>;
  suppliers_by_status: Record<SupplierStatus, number>;
  average_performance_score: number;
  total_purchase_orders: number;
  total_spend_current_period: number;
  top_suppliers_by_spend: {
    supplier_id: string;
    supplier_name: string;
    total_spend: number;
  }[];
}

export interface TopSuppliersResponse {
  suppliers: {
    supplier_id: string;
    supplier_name: string;
    supplier_type: SupplierType;
    total_orders: number;
    total_spend: number;
    performance_score: number;
    last_order_date: string;
  }[];
  period: {
    start_date: string;
    end_date: string;
  };
}

// Query Parameters
export interface SupplierQueryParams {
  search_term?: string;
  supplier_type?: SupplierType;
  status?: SupplierStatus;
  limit?: number;
  offset?: number;
  sort_by?: 'name' | 'created_at' | 'supplier_type' | 'status';
  sort_order?: 'asc' | 'desc';
}

export interface PurchaseOrderQueryParams {
  supplier_id?: string;
  status?: PurchaseOrderStatus;
  priority?: OrderPriority;
  date_from?: string; // ISO 8601 date string
  date_to?: string;   // ISO 8601 date string
  limit?: number;
  offset?: number;
  sort_by?: 'order_date' | 'total_amount' | 'status' | 'required_delivery_date';
  sort_order?: 'asc' | 'desc';
}

export interface DeliveryQueryParams {
  supplier_id?: string;
  purchase_order_id?: string;
  status?: DeliveryStatus;
  scheduled_date_from?: string;
  scheduled_date_to?: string;
  limit?: number;
  offset?: number;
  sort_by?: 'scheduled_date' | 'actual_delivery_date' | 'status';
  sort_order?: 'asc' | 'desc';
}

// API Response Wrappers
export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  limit: number;
  offset: number;
  has_next: boolean;
  has_previous: boolean;
}

export interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
  errors?: string[];
}

// Select option interface for enum helpers
export interface EnumOption {
  value: string | number;
  label: string;
  disabled?: boolean;
  description?: string;
}