export type UserRole = 'hq' | 'regional' | 'store' | 'dept'

export interface AuthUser {
  id: string
  role: UserRole
  storeCode?: string
  region?: string
  dept?: string
  name: string
}

export interface ApiResponse<T = any> {
  success: boolean
  data: T
  meta?: {
    total?: number
    page?: number
    page_size?: number
  }
  error?: string
}

export interface StoreScorecard {
  store_code: string
  store_name: string
  bill_count: number
  active_days: number
  received: number
  avg_daily_received: number
  avg_bill_value: number
  avg_guest_value: number
  discount_rate_pct: number
  theoretical_margin_pct: number
  member_bill_share_pct: number
}

export interface StoreRiskRating extends StoreScorecard {
  anomaly_rate_pct: number
  risk_level: '红色' | '黄色' | '绿色'
  primary_issue: string
}

export interface StorePriority {
  store_code: string
  store_name: string
  action_priority: string
  problem_count: number
  problem_combination: string
  received: number
  scale_tier: string
  business_type: string
}

export interface StoreTask {
  task_id: number
  plan_month: string
  store_code: string
  store_name: string
  priority: string
  problem_indicator: string
  current_value: number | null
  benchmark_value: number | null
  target_value: number | null
  problem_description: string | null
  action_required: string | null
  owner: string
  collaborators: string | null
  deadline: string
  status: string
  process_evidence: string | null
  verification_indicator: string | null
  verification_result: string | null
  incomplete_reason: string | null
  next_step: string | null
  created_at: string
  updated_at: string
}

export interface WeeklyCheck {
  task_id: number
  store_code: string
  store_name: string
  iso_week: number
  this_week_value: number | null
  last_week_value: number | null
  change_direction: 'up' | 'down' | 'flat' | 'unknown'
  consecutive_no_improve_weeks: number
  check_comment: string | null
  checked_by: string | null
  checked_at: string | null
}

export interface MonthlyReview {
  task_id: number
  store_code: string
  store_name: string
  priority: string
  problem_indicator: string
  baseline_value: number | null
  target_value: number | null
  actual_value: number | null
  review_result: '达标' | '改善中' | '未改善'
  revenue_stable: boolean
  margin_improved: boolean
  customer_stable: boolean
  anomaly_decreased: boolean
}

export interface LoopHealth {
  task_generation_rate: number
  store_execution_rate: number
  weekly_check_rate: number
  monthly_review_rate: number
  practice_promotion_rate: number
}

export interface IndicatorDict {
  id: number
  indicator_name: string
  business_definition: string
  formula: string
  data_source: string
  update_frequency: string
  owner: string
  scope: string
  yellow_threshold: number | null
  red_threshold: number | null
  version_date: string
}

export interface StandardizedPractice {
  id: number
  practice_module: string
  benchmark_store_code: string
  benchmark_store_name: string
  key_actions: string
  verification_indicators: string
  status: string
  created_at: string
}

export interface PracticeReplication {
  id: number
  practice_id: number
  trial_store_code: string
  trial_store_name: string
  observation_weeks: number
  before_value: number | null
  after_value: number | null
  revenue_impacted: boolean
  customer_impacted: boolean
  inventory_impacted: boolean
  status: string
}
