import type { CheckinExercise } from '@/store/checkinSlice';

export type ClassicalLevel = 'beginner' | 'intermediate' | 'advanced';

export type BuildOptions = {
  level?: ClassicalLevel;
  withSectionRests?: boolean; // 大段之间插入休息项
  restSeconds?: number; // 休息秒数
  withNotes?: boolean; // 插入提示/备注项
};

function restItem(idx: number, sec: number): CheckinExercise {
  return {
    key: `rest_${idx}`,
    name: `间隔休息 ${sec}s`,
    category: '休息',
    itemType: 'rest',
    sets: 0,
    restSec: sec,
  };
}

function noteItem(idx: number, text: string): CheckinExercise {
  return {
    key: `note_${idx}`,
    name: '提示',
    category: '备注',
    itemType: 'note',
    sets: 0,
    note: text,
  };
}

// 将图片中的“经典排课思路”转为结构化的动作清单（偏改革床序列，但以通用名称表示）
export function buildClassicalSession(options: BuildOptions = {}): { items: CheckinExercise[]; note: string } {
  const level = options.level ?? 'beginner';
  const withRests = options.withSectionRests ?? true;
  const restSec = Math.max(10, Math.min(120, options.restSeconds ?? 30));
  const withNotes = options.withNotes ?? true;

  const items: CheckinExercise[] = [];
  let noteText = '经典普拉提排课（根据学员情况可删减与调序）';

  const pushSectionRest = () => { if (withRests) items.push(restItem(items.length, restSec)); };
  const pushNote = (text: string) => { if (withNotes) items.push(noteItem(items.length, text)); };

  // 1) 垫上热身/呼吸
  pushNote('垫上热身：在仰卧位找到中立位，呼吸练习配合上举落下手臂');
  items.push({ key: 'breathing', name: '呼吸练习', category: '热身', itemType: 'exercise', sets: 1, durationSec: 60 });
  pushSectionRest();

  // 2) 正式核心床练习（节选/映射）
  // Footwork 系列（每项10次）
  pushNote('Footwork：脚趾、脚跟、V型脚、宽距，各10次');
  const footworkReps = 10;
  items.push({ key: 'footwork_toes', name: 'Footwork - 脚趾', category: '下肢与核心', sets: 1, reps: footworkReps });
  items.push({ key: 'footwork_heels', name: 'Footwork - 脚跟', category: '下肢与核心', sets: 1, reps: footworkReps });
  items.push({ key: 'footwork_v', name: 'Footwork - V型脚', category: '下肢与核心', sets: 1, reps: footworkReps });
  items.push({ key: 'footwork_wide', name: 'Footwork - 宽距', category: '下肢与核心', sets: 1, reps: footworkReps });
  items.push({ key: 'hundred', name: '百次拍击 (Hundred)', category: '核心', sets: 1, reps: 100 });
  items.push({ key: 'coordination', name: '协调 (Coordination)', category: '核心', sets: 1, reps: 6 });
  items.push({ key: 'short_spine', name: '短脊柱按摩 (Short Spine Massage)', category: '脊柱与柔韧', sets: 1, reps: 6 });
  items.push({ key: 'hug_a_tree', name: '抱树 (Hug a Tree)', category: '肩带与核心', sets: 1, reps: 10 });
  // Stomach Massage
  items.push({ key: 'stomach_round', name: '腹部按摩 - 背部弯曲', category: '核心与髋', sets: 1, reps: 6 });
  items.push({ key: 'stomach_chest_up', name: '腹部按摩 - 挺胸', category: '核心与髋', sets: 1, reps: 6 });
  // Short Box Abdominals 变式（每个3-4次）
  const sbaReps = level === 'advanced' ? 4 : 3;
  items.push({ key: 'short_box_round', name: '短箱腹肌 - 背部弯曲', category: '核心', sets: 1, reps: sbaReps });
  items.push({ key: 'short_box_flat', name: '短箱腹肌 - 背部平直', category: '核心', sets: 1, reps: sbaReps });
  items.push({ key: 'short_box_oblique', name: '短箱腹肌 - 斜向', category: '核心', sets: 1, reps: sbaReps });
  items.push({ key: 'short_box_tree', name: '短箱腹肌 - 爬树', category: '核心', sets: 1, reps: sbaReps });
  pushSectionRest();

  // 3) Long Stretch Series（每组4次；Elephant 6次）
  const lssReps = 4;
  items.push({ key: 'long_stretch', name: 'Long Stretch', category: '平衡与支撑', sets: 1, reps: lssReps });
  items.push({ key: 'up_stretch', name: 'Up Stretch', category: '平衡与支撑', sets: 1, reps: lssReps });
  items.push({ key: 'down_stretch', name: 'Down Stretch', category: '平衡与支撑', sets: 1, reps: lssReps });
  items.push({ key: 'elephant', name: 'Elephant', category: '后链与拉伸', sets: 1, reps: 6 });
  pushSectionRest();

  // 4) 半圆、长脊柱按摩
  items.push({ key: 'semi_circle', name: '半圆 (Semi-Circle) 每个方向', category: '脊柱与胸椎', sets: 1, reps: 6 });
  items.push({ key: 'long_spine', name: '长脊柱按摩 (Long Spine Massage) 每个方向', category: '脊柱与柔韧', sets: 1, reps: 3 });
  pushSectionRest();

  // 5) 膝跪伸展（Round/Flat/Knees Off）
  items.push({ key: 'knee_stretch_round', name: '膝跪伸展 - 弯背', category: '核心与髋', sets: 1, reps: 10 });
  items.push({ key: 'knee_stretch_flat', name: '膝跪伸展 - 平背', category: '核心与髋', sets: 1, reps: 15 });
  items.push({ key: 'knee_stretch_knees_off', name: '膝跪伸展 - 双膝离地', category: '核心与髋', sets: 1, reps: level === 'advanced' ? 8 : 6 });
  pushSectionRest();

  // 6) 跑步、骨盆抬起
  items.push({ key: 'running', name: '原地跑步 (Running in Place)', category: '下肢与心肺', sets: 1, reps: 25 });
  items.push({ key: 'pelvic_lift', name: '骨盆抬起 (Pelvic Lift)', category: '后链', sets: 1, reps: 10 });
  pushSectionRest();

  // 7) 站姿与划船、Mermaid
  items.push({ key: 'standing_knees_straight', name: '站姿 - 双膝伸直', category: '平衡与体态', sets: 1, reps: 6 });
  items.push({ key: 'standing_knees_bent', name: '站姿 - 双膝弯曲', category: '平衡与体态', sets: 1, reps: 6 });
  items.push({ key: 'rowing_front_1', name: '划船 正面1', category: '肩带与核心', sets: 1, reps: 4 });
  items.push({ key: 'rowing_front_2', name: '划船 正面2', category: '肩带与核心', sets: 1, reps: 4 });
  items.push({ key: 'mermaid', name: '美人鱼 (Mermaid)', category: '侧链与拉伸', sets: 1, reps: 3 });

  // 结尾提示
  pushNote('完成后侧坐于床侧边上，向前倾伸展背部，向后细伸脊椎并站起');

  return { items, note: noteText };
}


