// 从 Supabase 自动生成的类型中导出
import type { Database, Enums, Tables, TablesInsert, TablesUpdate } from '../supabase/types';

export type { Database, Tables, TablesInsert, TablesUpdate };

// 业务类型别名
export type UserRole = Enums<'app_role'>;
export type PlanStatus = Enums<'plan_status'>;
export type StepType = Enums<'step_type'>;
export type StepStatus = Enums<'step_status'>;
export type FactoryType = Enums<'factory_type'>;
export type WarehouseType = Enums<'warehouse_type'>;
export type PaymentStatus = Enums<'payment_status'>;

// 表行类型别名
export type Company = Tables<'companies'>;
export type Profile = Tables<'profiles'>;
export type CompanyMember = Tables<'company_members'>;
export type ProductionPlan = Tables<'production_plans'>;
export type PlanFactory = Tables<'plan_factories'>;
export type YarnRatio = Tables<'yarn_ratios'>;
export type PlanProcessStep = Tables<'plan_process_steps'>;
export type Warehouse = Tables<'warehouses'>;
export type InventoryRecord = Tables<'inventory_records'>;
export type YarnStock = Tables<'yarn_stock'>;
export type Payment = Tables<'payments'>;
export type Product = Tables<'products'>;
export type ProductInventoryRecord = Tables<'product_inventory_records'>;
export type WashingPlan = Tables<'washing_plans'>;
export type FinishedProduct = Tables<'finished_products'>;
export type WashingPlanCompletion = Tables<'washing_plan_completions'>;
export type FinishedProductInventoryRecord = Tables<'finished_product_inventory_records'>;
export type WashingProcessStep = Tables<'washing_process_steps'>;

// 计划卡片展示类型（前端组合数据）
export interface PlanCardDisplay {
  id: string;
  planCode: string;
  productName: string;
  color: string;
  fabricCode: string;
  colorCode: string;
  status: PlanStatus;
  targetQuantity: number;
  completedQuantity: number;
  startTime: string | null;
  progress: number;
  companyName?: string;
}

// 折叠组展示类型
export interface CollapsibleGroup {
  id: string;
  name: string;
  count: number;
  plans: PlanCardDisplay[];
}

// 认证状态
export interface AuthState {
  isLoggedIn: boolean;
  user: Profile | null;
  company: Company | null;
  currentRole: UserRole | null;
}

// ========== 仓库管理相关类型 ==========

// 产品价格历史
export interface ProductPriceHistory {
  id: string;
  old_price: number;
  new_price: number;
  change_amount: number;
  operator_name: string | null;
  change_reason: string | null;
  created_at: string;
}

// 纱线配比
export interface ProductYarnRatio {
  id: string;
  product_id: string;
  yarn_name: string;
  ratio: number;
  yarn_type: 'warp' | 'weft' | 'all' | string | null;
  created_at: string;
}

// 产品入库记录
export interface ProductInRecord {
  id: string;
  product_id: string;
  company_id: string | null;
  batch_no: string;
  rolls: number;
  meters: number;
  notes: string | null;
  operator_id: string | null;
  warehouse_id: string | null;
  created_at: string;
  // 前端展示用扩展字段
  time?: string;
  plan_code?: string;
  factory_name?: string;
  factory_id?: string;
  source?: 'self' | 'textile';
  price_per_meter?: number;
  price_note?: string | null;
  // 出库记录特有字段
  record_type?: 'in' | 'out';
  outbound_type?: string;
  washing_plan_id?: string | null;
  source_batch_id?: string | null;
  recipient_company_id?: string | null;
  recipient_company_name?: string | null;
}

// 按工厂分组的库存
export interface FactoryInventory {
  factory_id: string;
  factory_name: string;
  rolls: number;
  meters: number;
  records: ProductInRecord[];
}

// 扩展产品类型
export interface ProductWithInventory extends Product {
  factory_inventories?: Record<string, FactoryInventory>;
  total_rolls?: number;
  total_meters?: number;
  in_records?: ProductInRecord[];
  out_records?: ProductInRecord[];
  price_history?: ProductPriceHistory[];
  // 库存统计
  total_in_rolls?: number;
  total_in_meters?: number;
  total_out_rolls?: number;
  total_out_meters?: number;
}

// 入库批次
export interface FabricBatch {
  id: string;
  product_id: string;
  company_id: string;
  batch_no: string;
  rolls: number;
  meters: number;
  notes: string | null;
  created_at: string;
  product?: Product;
  plan_code?: string;
  factory_name?: string;
  factory_id?: string;
  source?: 'self' | 'textile';
}

// ========== 计划管理相关类型 ==========

// 计划流程步骤（扩展）
export interface PlanWithSteps extends ProductionPlan {
  processSteps: PlanProcessStep[];
  inventoryRecords: { time: string; quantity: number; rolls: number | null }[];
  priceHistory?: ProductPriceHistory[];
  yarn_ratios?: YarnRatio[];
  // 产品相关字段
  image_url?: string | null;
  warp_weight?: number | null;
  weft_weight?: number | null;
  total_weight?: number | null;
}

// 计划分组
export interface PlanGroup {
  purchaserId: string;
  purchaserName: string;
  plans: PlanWithSteps[];
}

// 状态映射配置
export interface StatusConfig {
  label: string;
  color: string;
  bgColor: string;
  borderColor: string;
}

// ========== 流程节点相关类型 ==========

export interface ProcessNode {
  id: string;
  name: string;
  status: StepStatus;
  timestamp?: string | null;
  stepType: StepType;
  operatorName?: string;
}

// 步骤类型配置
export interface StepTypeConfig {
  name: string;
  icon: string; // icon name for lookup
}

// ========== 表单相关类型 ==========

export interface YarnInput {
  id: string;
  name: string;
  ratio: number;
}

// ========== 成品仓库相关类型 ==========

// 成品产品
export interface FinishedProductWithInventory extends FinishedProduct {
  inventory_records?: FinishedProductInventoryRecord[];
  total_in_rolls?: number;
  total_in_meters?: number;
  total_out_rolls?: number;
  total_out_meters?: number;
}

// 水洗计划完成表单数据
export interface WashingCompletionFormData {
  washing_plan_id: string;
  actual_washed_meters: number;
  actual_shrinkage_rate: number;
  rolls: number;
  washing_date: string;
  actual_washing_cost?: number;
  unit_cost?: number;
  warehouse_id?: string;
  warehouse_location?: string;
  notes?: string;
}

// ========== 通用工具类型 ==========

export type LoadingState = 'idle' | 'loading' | 'success' | 'error';

export interface PaginationState {
  currentPage: number;
  pageSize: number;
  hasMore: boolean;
  total?: number;
}