// Admin Control Panel Types - State Machine & Control Events

// ============================================================================
// System Phase State Machine
// ============================================================================

export type SystemPhase = 'IDLE' | 'VOTING' | 'LOTTERY' | 'RESULTS';

export type VotingSubPhase = 'CLOSED' | 'OPEN' | 'PAUSED';

export type LotterySubPhase =
  | 'IDLE'           // Waiting to start
  | 'GALAXY'         // Showing participants
  | 'STORM'          // Rolling animation
  | 'REVEAL'         // Revealing winners
  | 'COMPLETE';      // Winners displayed

export type LotteryRound = 1 | 2 | 3 | 4;

// ============================================================================
// Admin State
// ============================================================================

export interface AdminState {
  systemPhase: SystemPhase;
  voting: VotingState;
  lottery: LotteryState;
  music: MusicState;
  lastUpdated: number;
}

export interface VotingState {
  subPhase: VotingSubPhase;
  openedAt?: number;
  closedAt?: number;
  totalVotes: number;
  // 节目投票控制
  currentProgramId: string | null;     // 当前投票节目 ID
  currentProgramIndex: number;          // 当前节目序号 (0-based index)
  programs: VotingProgram[];            // 节目列表（已排序）
  allowLateCatch: boolean;              // 补投票开关（默认 true）
  votingStartedAt?: number;             // 当前节目投票开始时间（用于计时）
}

export type ProgramVotingStatus = 'pending' | 'voting' | 'completed';

export interface VotingProgram {
  id: string;
  name: string;
  teamName: string;
  order: number;                        // 初始顺序
  status: ProgramVotingStatus;          // 投票状态
  votes: number;                        // 票数
  stamps: VoteStamp[];                  // 已盖的戳（用于大屏展示）
}

export interface VoteStamp {
  id: string;
  userName: string;
  department: string;
  ticketType: string;
  x: number;                            // 随机 X 位置 (0-100%)
  y: number;                            // 随机 Y 位置 (0-100%)
  rotation: number;                     // 随机旋转角度
  timestamp: number;
  date: string;                         // 格式化日期 YYYY.MM.DD
}

export interface LotteryState {
  round: LotteryRound;
  subPhase: LotterySubPhase;
  prizeLevel: string;
  prizeName: string;
  winnerCount: number;
  currentWinners: LotteryWinner[];
  startedAt?: number;
  stormStartedAt?: number;  // For safety delay calculation
}

export interface LotteryWinner {
  id: string;
  name: string;
  department: string;
}

export interface MusicState {
  isPlaying: boolean;
  track: 'bgm' | 'lottery' | 'fanfare' | 'none';
  volume: number;
}

// ============================================================================
// Admin Control Payloads
// ============================================================================

export interface PhaseChangePayload {
  phase: SystemPhase;
  timestamp: number;
}

export interface VotingControlPayload {
  action: 'open' | 'close' | 'pause' | 'resume';
  timestamp: number;
}

export interface LotteryControlPayload {
  action: 'start_galaxy' | 'start_storm' | 'stop_reveal' | 'next_round' | 'reset';
  round?: LotteryRound;
  winnerIds?: string[];  // For reveal action
  timestamp: number;
}

export interface EmergencyResetPayload {
  scope: 'all' | 'voting' | 'lottery';
  confirmCode: string;  // Safety: require confirmation code
  timestamp: number;
}

export interface MusicControlPayload {
  action: 'play' | 'stop' | 'change_track';
  track?: MusicState['track'];
  volume?: number;
}

export interface AdminStateSyncPayload {
  state: AdminState;
  requestedBy?: string;
}

// ============================================================================
// Prize Configuration
// ============================================================================

export interface PrizeConfig {
  round: LotteryRound;
  level: string;
  name: string;
  winnerCount: number;
  zodiacFilter?: string;  // Round 4: 'horse'
}

export const PRIZE_CONFIG: PrizeConfig[] = [
  { round: 1, level: '特等奖', name: 'iPhone 16 Pro Max', winnerCount: 1 },
  { round: 2, level: '一等奖', name: 'iPad Pro', winnerCount: 3 },
  { round: 3, level: '二等奖', name: 'AirPods Pro', winnerCount: 5 },
  { round: 4, level: '三等奖', name: '京东卡 500元', winnerCount: 10, zodiacFilter: 'horse' },
];

// Default programs for voting
export const DEFAULT_PROGRAMS: VotingProgram[] = [
  { id: 'p1', name: '龙腾四海', teamName: '市场部', order: 1, status: 'pending', votes: 0, stamps: [] },
  { id: 'p2', name: '金马奔腾', teamName: '技术部', order: 2, status: 'pending', votes: 0, stamps: [] },
  { id: 'p3', name: '春风得意', teamName: '人力资源部', order: 3, status: 'pending', votes: 0, stamps: [] },
  { id: 'p4', name: '鸿运当头', teamName: '财务部', order: 4, status: 'pending', votes: 0, stamps: [] },
  { id: 'p5', name: '马到成功', teamName: '运营部', order: 5, status: 'pending', votes: 0, stamps: [] },
  { id: 'p6', name: '一马当先', teamName: '产品部', order: 6, status: 'pending', votes: 0, stamps: [] },
  { id: 'p7', name: '万马奔腾', teamName: '设计部', order: 7, status: 'pending', votes: 0, stamps: [] },
  { id: 'p8', name: '龙马精神', teamName: '销售部', order: 8, status: 'pending', votes: 0, stamps: [] },
];

// ============================================================================
// Initial State
// ============================================================================

export const INITIAL_ADMIN_STATE: AdminState = {
  systemPhase: 'IDLE',
  voting: {
    subPhase: 'CLOSED',
    totalVotes: 0,
    currentProgramId: null,
    currentProgramIndex: -1,
    programs: DEFAULT_PROGRAMS,
    allowLateCatch: true,
  },
  lottery: {
    round: 1,
    subPhase: 'IDLE',
    prizeLevel: '特等奖',
    prizeName: 'iPhone 16 Pro Max',
    winnerCount: 1,
    currentWinners: [],
  },
  music: {
    isPlaying: false,
    track: 'none',
    volume: 0.7,
  },
  lastUpdated: Date.now(),
};
