export type TaskStatus = "pending" | "in_progress" | "completed" | "blocked";
export type TaskKind = "task" | "subtask" | "subsubtask";

export type TaskGroup = {
  id: string;
  name: string;
  description: string;
  createdAt: string;
  updatedAt: string;
};

export type TaskItem = {
  id: string;
  groupId: string;
  parentId: string | null;
  kind: TaskKind;
  title: string;
  description: string;
  status: TaskStatus;
  startAt: string | null;
  dueAt: string | null;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
};

export type Milestone = {
  id: string;
  groupId: string;
  taskId: string | null;
  title: string;
  occursAt: string;
  createdAt: string;
};

export type ExecutionReport = {
  id: string;
  taskId: string;
  title: string;
  body: string;
  status: string;
  createdAt: string;
};

export type Timeline = {
  group: TaskGroup;
  tasks: TaskItem[];
  milestones: Milestone[];
  reports: ExecutionReport[];
};

export type TaskGroupOverview = {
  group: TaskGroup;
  taskCount: number;
  subtaskCount: number;
  subsubtaskCount: number;
  completedCount: number;
  reportCount: number;
  startAt: string | null;
  dueAt: string | null;
};

export type TaskTreeCommand =
  | { operation: "health" }
  | { operation: "group.list" }
  | { operation: "group.overview" }
  | { operation: "group.get"; groupId: string }
  | { operation: "group.create"; name: string; description?: string }
  | { operation: "group.delete"; groupId: string }
  | { operation: "group.stats"; groupId: string }
  | { operation: "group.import-markdown"; sourcePath: string; sourceModifiedAt: string; markdown: string; reports: Array<{ href: string; sourcePath: string; body: string; modifiedAt: string }>; groupName?: string; dryRun?: boolean }
  | { operation: "task.list"; groupId: string }
  | { operation: "task.get"; taskId: string }
  | { operation: "task.create"; groupId: string; title: string; description?: string; startAt?: string; dueAt?: string; parentId?: string }
  | { operation: "task.create-batch"; groupId: string; titles: string[]; parentId?: string; description?: string; startAt?: string; dueAt?: string }
  | { operation: "task.update"; taskId: string; title?: string; description?: string; status?: TaskStatus; startAt?: string | null; dueAt?: string | null }
  | { operation: "task.delete"; taskId: string }
  | { operation: "task.start"; taskId: string }
  | { operation: "task.complete"; taskId: string }
  | { operation: "milestone.create"; groupId: string; title: string; occursAt: string; taskId?: string }
  | { operation: "report.list"; taskId: string }
  | { operation: "report.get"; reportId: string }
  | { operation: "report.write"; taskId: string; title: string; body: string; status?: string }
  | { operation: "report.create"; taskId: string; title: string; body: string; status?: string }
  | { operation: "timeline.get"; groupId: string }
  | { operation: "backup.create"; dryRun?: boolean; reason?: string }
  | { operation: "backup.status" }
  | { operation: "backup.verify" }
  | { operation: "backup.restore"; confirm?: boolean; expectedSha256?: string }
  | { operation: "workflow.start"; taskId: string };

export type TaskTreeResult = {
  ok: boolean;
  operation: TaskTreeCommand["operation"];
  data?: unknown;
  error?: { code: string; message: string; detail?: unknown };
};
