// 使用者相關類型
export interface User {
  id: number;
  email: string;
  display_name?: string;
  language: string;
  role: 'admin' | 'user';
  created_at: string;
}

export interface LoginRequest {
  email: string;
  password: string;
}

export interface LoginResponse {
  access_token: string;
  token_type: string;
  user: User;
}

// DIT 資料類型
export interface DitRecord {
  id: number;
  op_id: string;
  op_name?: string;
  erp_account?: string;
  customer: string;
  pn: string;
  eau: number;
  stage: string;
  date: string;
  created_at: string;
}

// 樣品資料類型
export interface SampleRecord {
  id: number;
  sample_id: string;
  order_no: string;
  oppy_no?: string;
  cust_id?: string;
  customer: string;
  pn: string;
  qty: number;
  date: string;
  created_at: string;
}

// 訂單資料類型
export interface OrderRecord {
  id: number;
  order_id: string;
  order_no: string;
  cust_id?: string;
  customer: string;
  pn: string;
  qty: number;
  status: string;
  amount: number;
  created_at: string;
}

// 比對結果類型
export interface MatchResult {
  id: number;
  dit_id: number;
  target_type: 'SAMPLE' | 'ORDER';
  target_id: number;
  score: number;
  match_priority: number;
  match_source: string;
  reason: string;
  status: 'pending' | 'accepted' | 'rejected' | 'auto_matched';
  dit: DitRecord;
  target: SampleRecord | OrderRecord;
}

// 審核動作
export type ReviewAction = 'accept' | 'reject';

// Dashboard KPI
export interface DashboardKPI {
  total_dit: number;
  sample_rate: number;
  hit_rate: number;
  fulfillment_rate: number;
  no_order_sample_rate: number;
  total_revenue: number;
}

// 漏斗數據
export interface FunnelData {
  name: string;
  value: number;
  fill: string;
}

// 歸因明細
export interface AttributionRow {
  dit: DitRecord;
  sample?: SampleRecord;
  order?: OrderRecord;
  match_source?: string;
  attributed_qty: number;
  fulfillment_rate: number;
}

// Excel 上傳預覽
export interface ParsedFile {
  file_id: string;
  file_type: 'dit' | 'sample' | 'order';
  filename: string;
  row_count: number;
  header_row: number;
  preview: Record<string, unknown>[];
}

// Lab 分析相關類型
export interface LabKPI {
  converted_count: number;
  avg_velocity: number;
  conversion_rate: number;
  orphan_count: number;
  no_dit_count: number;
  high_qty_no_order_count: number;
}

export interface ScatterPoint {
  customer: string;
  pn: string;
  sample_qty: number;
  order_qty: number;
}

export interface OrphanSample {
  customer: string;
  pn: string;
  days_since_sent: number;
  order_no: string;
  date: string;
  sample_qty: number;
}

export interface NoDitSample {
  sample_id: string;
  customer: string;
  pn: string;
  order_no: string;
  date: string;
  qty: number;
}

export interface HighQtyNoOrderSample {
  sample_id: string;
  customer: string;
  pn: string;
  order_no: string;
  date: string;
  qty: number;
  days_since_sent: number;
}

export interface ConversionRecord {
  customer: string;
  pn: string;
  sample_date: string;
  sample_qty: number;
  order_date: string;
  order_qty: number;
  days_to_convert: number;
}

// API 響應包裝
export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  message?: string;
  error?: string;
}
