import {
  findInitiative,
  getInitiativePhases,
  getInitiativeProgress,
  getInitiativeWUs
} from "./chunk-MZ7RKIX4.js";
import {
  OUTPUT_FORMATS
} from "./chunk-NAP6CFSO.js";
import {
  WU_OPTIONS,
  createWUParser,
  runCLI
} from "./chunk-2GXVIN57.js";
import {
  WU_STATUS
} from "./chunk-V6OJGLBA.js";
import {
  die
} from "./chunk-RXRKBBSM.js";

// src/initiative-status.ts
function asString(value) {
  return typeof value === "string" ? value : "";
}
function asStringArray(value) {
  return Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
}
function normalizeLifecycleStatus(value) {
  return typeof value === "string" ? value.trim().toLowerCase() : "";
}
function toInitiativePhases(doc) {
  return Array.isArray(doc.phases) ? doc.phases : [];
}
function hasIncompletePhase(phases) {
  if (!Array.isArray(phases) || phases.length === 0) {
    return false;
  }
  return phases.some((phase) => {
    if (phase == null || typeof phase !== "object") {
      return true;
    }
    return normalizeLifecycleStatus(phase.status) !== WU_STATUS.DONE;
  });
}
function hasAllLinkedWUsDone(progress) {
  if (!progress) {
    return false;
  }
  return progress.total > 0 && progress.done === progress.total;
}
function hasIncompleteLinkedWUs(progress) {
  if (!progress) {
    return false;
  }
  return progress.total > 0 && progress.done < progress.total;
}
function deriveInitiativeLifecycleStatus(status, phases, progress) {
  const normalizedStatus = normalizeLifecycleStatus(status);
  if (hasAllLinkedWUsDone(progress)) {
    return WU_STATUS.DONE;
  }
  if (normalizedStatus === WU_STATUS.DONE && hasIncompletePhase(phases)) {
    return WU_STATUS.IN_PROGRESS;
  }
  if (normalizedStatus === WU_STATUS.DONE && hasIncompleteLinkedWUs(progress)) {
    return WU_STATUS.IN_PROGRESS;
  }
  return normalizedStatus || WU_STATUS.IN_PROGRESS;
}
function getWUBlockers(doc) {
  const blockedBy = asStringArray(doc.blocked_by);
  if (blockedBy.length > 0) {
    return blockedBy;
  }
  return asStringArray(doc.dependencies);
}
function hasPhaseWUStatus(phaseWUs, status) {
  return phaseWUs.some((wu) => normalizeLifecycleStatus(wu.doc.status) === status);
}
function areAllPhaseWUsDone(phaseWUs) {
  return phaseWUs.length > 0 && phaseWUs.every((wu) => normalizeLifecycleStatus(wu.doc.status) === WU_STATUS.DONE);
}
function deriveInitiativePhaseStatus(status, phaseWUs) {
  if (areAllPhaseWUsDone(phaseWUs)) {
    return WU_STATUS.DONE;
  }
  if (hasPhaseWUStatus(phaseWUs, WU_STATUS.IN_PROGRESS)) {
    return WU_STATUS.IN_PROGRESS;
  }
  if (hasPhaseWUStatus(phaseWUs, WU_STATUS.BLOCKED)) {
    return WU_STATUS.BLOCKED;
  }
  if (hasPhaseWUStatus(phaseWUs, WU_STATUS.READY)) {
    return WU_STATUS.READY;
  }
  return normalizeLifecycleStatus(status) || WU_STATUS.IN_PROGRESS;
}
function priorityRank(priority) {
  const p = String(priority || "").toUpperCase();
  const map = { P0: 0, P1: 1, P2: 2, P3: 3 };
  return p in map ? map[p] : 9;
}
function isRunnableReady(wu, wuById) {
  if (normalizeLifecycleStatus(wu.doc.status) !== WU_STATUS.READY) return false;
  const blockers = getWUBlockers(wu.doc);
  return blockers.every((blockerId) => {
    const blocker = wuById.get(blockerId);
    return !blocker || normalizeLifecycleStatus(blocker.doc.status) === WU_STATUS.DONE;
  });
}
function renderDetailed(initiative, useColor) {
  const { id, doc } = initiative;
  const progress = getInitiativeProgress(id);
  const wus = getInitiativeWUs(id);
  const wuById = new Map(wus.map((wu) => [wu.id, wu]));
  const phaseGroups = getInitiativePhases(id);
  const phases = toInitiativePhases(doc);
  const status = deriveInitiativeLifecycleStatus(doc.status, phases, progress);
  const docTitle = asString(doc.title);
  const rawStatus = asString(doc.status);
  const normalizedRawStatus = normalizeLifecycleStatus(rawStatus);
  console.log(`
${id}: ${docTitle || "(untitled)"}`);
  console.log(
    `Status: ${status} | Progress: ${progress.percentage}% (${progress.done}/${progress.total} WUs)`
  );
  if (status !== normalizedRawStatus) {
    console.log(
      `Lifecycle mismatch: metadata status '${rawStatus}' is inconsistent with phase state; reporting '${status}'.`
    );
  }
  console.log("\nStart here:");
  console.log(
    `  1) Plan waves (recommended): pnpm orchestrate:initiative --initiative ${id} --dry-run`
  );
  const runnableReady = wus.filter((wu) => isRunnableReady(wu, wuById)).sort((a, b) => {
    const byPriority = priorityRank(a.doc.priority) - priorityRank(b.doc.priority);
    if (byPriority !== 0) return byPriority;
    return a.id.localeCompare(b.id);
  });
  if (runnableReady.length > 0) {
    const next = runnableReady[0];
    if (next) {
      console.log(
        `  2) Start next WU: pnpm wu:claim --id ${next.id} --lane "${asString(next.doc.lane)}"`
      );
      console.log(`     Then cd into the created worktree (shown by wu:claim output)`);
    }
  } else if (progress.inProgress > 0) {
    console.log(
      `  2) Continue: ${progress.inProgress} WU(s) already in progress (finish those first)`
    );
  } else if (progress.blocked > 0) {
    console.log(`  2) Unblock: ${progress.blocked} WU(s) blocked (resolve blockers first)`);
  } else {
    console.log("  2) No runnable ready WUs found (check dependencies/blocked_by)");
  }
  const description = asString(doc.description);
  if (description) {
    console.log(`
Description: ${description}`);
  }
  const owner = asString(doc.owner);
  if (owner) {
    console.log(`Owner: ${owner}`);
  }
  const targetDate = asString(doc.target_date);
  if (targetDate) {
    console.log(`Target: ${targetDate}`);
  }
  if (phases.length > 0) {
    console.log("\nPhases:");
    for (const phase of phases) {
      const phaseWUs = phaseGroups.get(phase.id) || [];
      const phaseStatus = formatStatus(
        deriveInitiativePhaseStatus(phase.status, phaseWUs),
        useColor
      );
      const phaseTitle = asString(phase.title) || `Phase ${phase.id}`;
      console.log(
        `  ${phase.id}. ${phaseTitle.padEnd(30)} [${phaseStatus}] ${phaseWUs.length} WUs`
      );
    }
    const unassigned = phaseGroups.get(null) || [];
    if (unassigned.length > 0) {
      console.log(`  -. Unassigned                       ${unassigned.length} WUs`);
    }
  }
  console.log("\nWork Units by Status:");
  const byStatus = groupWUsByStatus(wus);
  for (const [status2, statusWUs] of Object.entries(byStatus)) {
    if (statusWUs.length > 0) {
      console.log(`  ${capitalizeFirst(status2)} (${statusWUs.length}):`);
      for (const wu of statusWUs.slice(0, 5)) {
        console.log(`    - ${wu.id}: ${truncate(asString(wu.doc.title), 50)}`);
      }
      if (statusWUs.length > 5) {
        console.log(`    ... and ${statusWUs.length - 5} more`);
      }
    }
  }
  const successMetrics = asStringArray(doc.success_metrics);
  if (successMetrics.length > 0) {
    console.log("\nSuccess Metrics:");
    for (const metric of successMetrics) {
      console.log(`  - ${metric}`);
    }
  }
  console.log("");
}
function renderJSON(initiative) {
  const { id, doc } = initiative;
  const progress = getInitiativeProgress(id);
  const wus = getInitiativeWUs(id);
  const phaseGroups = getInitiativePhases(id);
  const phases = toInitiativePhases(doc);
  const status = deriveInitiativeLifecycleStatus(doc.status, phases, progress);
  const output = {
    id,
    slug: asString(doc.slug),
    title: asString(doc.title),
    description: asString(doc.description),
    status,
    rawStatus: asString(doc.status),
    priority: asString(doc.priority),
    owner: asString(doc.owner),
    created: asString(doc.created),
    targetDate: asString(doc.target_date),
    progress: {
      percentage: progress.percentage,
      done: progress.done,
      inProgress: progress.inProgress,
      ready: progress.ready,
      blocked: progress.blocked,
      total: progress.total
    },
    phases: phases.map((phase) => {
      const phaseWUs = phaseGroups.get(phase.id) || [];
      const phaseStatus = deriveInitiativePhaseStatus(phase.status, phaseWUs);
      return {
        id: phase.id,
        title: asString(phase.title),
        status: phaseStatus,
        wuCount: phaseWUs.length,
        wus: phaseWUs.map((w) => ({
          id: w.id,
          title: asString(w.doc.title),
          status: normalizeLifecycleStatus(w.doc.status)
        }))
      };
    }),
    workUnits: wus.map((w) => ({
      id: w.id,
      title: asString(w.doc.title),
      status: normalizeLifecycleStatus(w.doc.status),
      phase: w.doc.phase ?? null
    })),
    successMetrics: asStringArray(doc.success_metrics),
    labels: asStringArray(doc.labels)
  };
  console.log(JSON.stringify(output, null, 2));
}
function groupWUsByStatus(wus) {
  const groups = {
    [WU_STATUS.DONE]: [],
    [WU_STATUS.IN_PROGRESS]: [],
    [WU_STATUS.READY]: [],
    [WU_STATUS.BLOCKED]: [],
    other: []
  };
  for (const wu of wus) {
    const status = normalizeLifecycleStatus(wu.doc.status);
    switch (status) {
      case WU_STATUS.DONE:
        groups[WU_STATUS.DONE].push(wu);
        break;
      case WU_STATUS.IN_PROGRESS:
        groups[WU_STATUS.IN_PROGRESS].push(wu);
        break;
      case WU_STATUS.READY:
        groups[WU_STATUS.READY].push(wu);
        break;
      case WU_STATUS.BLOCKED:
        groups[WU_STATUS.BLOCKED].push(wu);
        break;
      default:
        groups.other.push(wu);
        break;
    }
  }
  return groups;
}
function formatStatus(status, useColor) {
  if (!useColor) return status;
  return status;
}
function truncate(str, maxLen) {
  if (!str) return "";
  return str.length > maxLen ? `${str.substring(0, maxLen - 3)}...` : str;
}
function capitalizeFirst(str) {
  return str.charAt(0).toUpperCase() + str.slice(1).replace(/_/g, " ");
}
async function main() {
  const args = createWUParser({
    name: "initiative-status",
    description: "Show detailed initiative view with phases and WUs",
    options: [WU_OPTIONS.id, WU_OPTIONS.format, WU_OPTIONS.color],
    required: [],
    allowPositionalId: true
  });
  const initRef = args.id;
  if (!initRef) {
    die("Initiative ID or slug is required.\n\nUsage: pnpm initiative:status INIT-001");
  }
  const initiative = findInitiative(initRef);
  if (!initiative) {
    die(
      `Initiative not found: ${initRef}

Run 'pnpm initiative:list' to see available initiatives.`
    );
  }
  const format = args.format || OUTPUT_FORMATS.TABLE;
  switch (format) {
    case OUTPUT_FORMATS.JSON:
      renderJSON(initiative);
      break;
    default:
      renderDetailed(initiative, Boolean(args.color));
      break;
  }
}
if (import.meta.main) {
  void runCLI(main);
}

export {
  deriveInitiativeLifecycleStatus,
  deriveInitiativePhaseStatus,
  main
};
