/**
 * Sales API Types
 *
 * These types mirror the backend Pydantic schemas exactly.
 * Backend schemas location: services/sales/app/schemas/
 *
 * @see services/sales/app/schemas/sales.py - Sales data schemas
 * @see services/sales/app/api/sales_operations.py - Import and validation operations
 *
 * NOTE: Product references changed to inventory_product_id (references inventory service)
 * product_name and product_category are DEPRECATED - use inventory service instead
 */

// ===== SALES DATA SCHEMAS =====
// Mirror: SalesDataCreate from sales.py:48

export interface SalesDataCreate {
  // Product reference - REQUIRED reference to inventory service
  inventory_product_id: string;

  quantity_sold: number;  // gt=0
  unit_price?: number | null;  // ge=0
  revenue: number;  // gt=0
  cost_of_goods?: number | null;  // ge=0
  discount_applied?: number;  // ge=0, le=100, Default: 0

  location_id?: string | null;  // max_length=100
  sales_channel?: string;  // Default: "in_store", one of: in_store, online, delivery, wholesale
  source?: string;  // Default: "manual", one of: manual, pos, online, import, api, csv

  notes?: string | null;
  weather_condition?: string | null;  // max_length=50
  is_holiday?: boolean;  // Default: false
  is_weekend?: boolean;  // Default: false

  // Optional - set automatically if not provided
  tenant_id?: string | null;
  date: string;  // datetime
}

// Mirror: SalesDataUpdate from sales.py:54
export interface SalesDataUpdate {
  // Note: product_name, product_category, product_sku DEPRECATED - use inventory service

  quantity_sold?: number | null;  // gt=0
  unit_price?: number | null;  // ge=0
  revenue?: number | null;  // gt=0
  cost_of_goods?: number | null;  // ge=0
  discount_applied?: number | null;  // ge=0, le=100

  location_id?: string | null;
  sales_channel?: string | null;

  notes?: string | null;
  weather_condition?: string | null;
  is_holiday?: boolean | null;
  is_weekend?: boolean | null;

  validation_notes?: string | null;
  is_validated?: boolean | null;
}

// Mirror: SalesDataResponse from sales.py:79
export interface SalesDataResponse {
  id: string;
  tenant_id: string;
  date: string;  // datetime

  // Product reference - links to inventory service
  inventory_product_id: string;

  quantity_sold: number;
  unit_price: number | null;
  revenue: number;
  cost_of_goods: number | null;
  discount_applied: number;

  location_id: string | null;
  sales_channel: string;
  source: string;

  notes: string | null;
  weather_condition: string | null;
  is_holiday: boolean;
  is_weekend: boolean;

  is_validated: boolean;  // Default: false
  validation_notes: string | null;

  created_at: string;
  updated_at: string;
  created_by: string | null;

  profit_margin: number | null;  // Calculated field
}

// Mirror: SalesDataQuery from sales.py:98
export interface SalesDataQuery {
  start_date?: string | null;
  end_date?: string | null;

  // Note: product_name and product_category DEPRECATED
  // Use inventory_product_id or join with inventory service
  inventory_product_id?: string | null;  // Filter by specific inventory product

  location_id?: string | null;
  sales_channel?: string | null;
  source?: string | null;
  is_validated?: boolean | null;

  limit?: number;  // ge=1, le=1000, Default: 50
  offset?: number;  // ge=0, Default: 0

  order_by?: string;  // Default: "date"
  order_direction?: 'asc' | 'desc';  // Default: "desc"
}

// ===== ANALYTICS SCHEMAS =====
// Mirror: SalesAnalytics from sales.py:129

export interface SalesAnalytics {
  total_revenue: number;
  total_quantity: number;
  total_transactions: number;
  average_transaction_value: number;
  top_products: Array<Record<string, any>>;
  sales_by_channel: Record<string, any>;
  sales_by_day: Array<Record<string, any>>;
}

// Mirror: ProductSalesAnalytics from sales.py:140
export interface ProductSalesAnalytics {
  inventory_product_id: string;  // Reference to inventory service product
  // Note: product_name fetched from inventory service using inventory_product_id
  total_revenue: number;
  total_quantity: number;
  total_transactions: number;
  average_price: number;
  growth_rate: number | null;
}

// ===== OPERATIONS SCHEMAS =====
// From sales_operations.py

export interface SalesValidationRequest {
  record_id: string;
  validation_notes?: string | null;
}

export interface ProductSalesQuery {
  inventory_product_id: string;
  start_date?: string | null;
  end_date?: string | null;
}

// ===== IMPORT/VALIDATION SCHEMAS =====
// From sales_operations.py and data_import_service

export interface ImportValidationRequest {
  tenant_id: string;
  data?: string;  // JSON string of records
  data_format?: 'json' | 'csv' | 'excel';
  records?: Array<Record<string, any>>;
}

export interface ImportValidationResult {
  is_valid: boolean;
  total_records: number;
  valid_records: number;
  invalid_records: number;
  errors: Array<{
    row?: number;
    field?: string;
    message: string;
    value?: any;
  }>;
  warnings: Array<{
    row?: number;
    field?: string;
    message: string;
    value?: any;
  }>;
  summary: {
    total_rows: number;
    valid_rows: number;
    invalid_rows: number;
    columns_found: string[];
    missing_required_fields?: string[];
    duplicate_records?: number;
  };
}

export interface ImportExecutionRequest {
  tenant_id: string;
  file?: File;
  data?: Array<Record<string, any>>;
  file_format?: 'json' | 'csv' | 'excel';
  validation_result?: ImportValidationResult;
}

export interface ImportExecutionResult {
  success: boolean;
  total_records: number;
  imported_records: number;
  failed_records: number;
  errors: Array<{
    row?: number;
    message: string;
    data?: any;
  }>;
  imported_ids: string[];
  execution_time_ms: number;
}

export interface ImportTemplateRequest {
  format: 'csv' | 'json' | 'excel';
}

export interface ImportTemplateResponse {
  template_url?: string;
  template_data?: any;
  format: string;
  columns: Array<{
    name: string;
    type: string;
    required: boolean;
    example?: any;
    description?: string;
  }>;
  sample_data?: Array<Record<string, any>>;
}

// ===== FILTER TYPES =====

export interface SalesRecordFilters {
  start_date?: string | null;
  end_date?: string | null;
  inventory_product_id?: string | null;
  location_id?: string | null;
  sales_channel?: string | null;
  source?: string | null;
  is_validated?: boolean | null;
  limit?: number;
  offset?: number;
  order_by?: string;
  order_direction?: 'asc' | 'desc';
}

// ===== CONSTANTS =====

export const SALES_CHANNELS = ['in_store', 'online', 'delivery', 'wholesale'] as const;
export type SalesChannel = typeof SALES_CHANNELS[number];

export const SALES_SOURCES = ['manual', 'pos', 'online', 'import', 'api', 'csv'] as const;
export type SalesSource = typeof SALES_SOURCES[number];

export const IMPORT_FORMATS = ['json', 'csv', 'excel'] as const;
export type ImportFormat = typeof IMPORT_FORMATS[number];
