/**
 * Example: Calculate and display critical path for a project
 *
 * Usage:
 *   npx tsx examples/critical-path.ts <project-id>
 */

import { createGanttClientFromEnv } from "../index.js";

async function main(): Promise<void> {
  const projectId = process.argv[2];

  if (!projectId) {
    console.error("Usage: npx tsx examples/critical-path.ts <project-id>");
    process.exit(1);
  }

  const client = createGanttClientFromEnv();

  console.log(`Calculating critical path for project ${projectId}...\n`);

  const criticalPath = await client.calculateCriticalPath(projectId);

  console.log(`Critical Path (${criticalPath.totalDuration} days):`);
  console.log("=".repeat(50));

  for (const item of criticalPath.path) {
    const statusIcon =
      item.task.status === "COMPLETED" ? "✓" : item.task.status === "IN_PROGRESS" ? "⊙" : "□";
    console.log(`${statusIcon} ${item.task.title}`);
    console.log(`   Duration: ${item.duration} days`);
    console.log(`   Cumulative: ${item.cumulativeDuration} days`);
    console.log(`   Status: ${item.task.status}`);

    if (item.task.metadata.dependencies && item.task.metadata.dependencies.length > 0) {
      console.log(`   Depends on: ${item.task.metadata.dependencies.length} task(s)`);
    }

    console.log("");
  }

  if (criticalPath.nonCriticalTasks.length > 0) {
    console.log("\nNon-Critical Tasks (can be delayed):");
    console.log("=".repeat(50));

    for (const item of criticalPath.nonCriticalTasks.sort((a, b) => a.slack - b.slack)) {
      console.log(`- ${item.task.title}`);
      console.log(`  Slack: ${item.slack} days`);
      console.log(`  Status: ${item.task.status}`);
      console.log("");
    }
  }
}

main().catch((error) => {
  console.error("Error:", error.message);
  process.exit(1);
});
