import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { getRedisUrl, getRedisUrlOrThrow } from "@open-mercato/shared/lib/redis/connection";
import { WorkflowDefinition } from "./data/entities.js";
import { BusinessRule } from "@open-mercato/core/modules/business_rules/data/entities";
import {
  invalidateBusinessRuleDiscoveryCache,
  resolveBusinessRuleDiscoveryCache
} from "@open-mercato/core/modules/business_rules/lib/rule-engine";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function parseArgs(args) {
  const result = {};
  for (let i = 0; i < args.length; i += 2) {
    const key = args[i]?.replace(/^-+/, "");
    const value = args[i + 1];
    if (key && value) {
      result[key] = value;
    }
  }
  return result;
}
const seedDemo = {
  command: "seed-demo",
  async run() {
    console.log('\u2139\uFE0F  The "workflows.checkout-demo" workflow is now code-defined.');
    console.log("   No seeding required \u2014 it is auto-registered by the workflows module");
    console.log("   (see packages/core/src/modules/workflows/workflows.ts).");
    console.log("");
    console.log("   Visit /backend/definitions to view it as a Code-defined workflow.");
  }
};
const seedDemoWithRules = {
  command: "seed-demo-with-rules",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? "");
    const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato workflows seed-demo-with-rules --tenant <tenantId> --org <organizationId>");
      console.error("   or: mercato workflows seed-demo-with-rules -t <tenantId> -o <organizationId>");
      return;
    }
    console.log("\u{1F9E9} Seeding checkout-demo guard rules (workflow itself is code-defined)...\n");
    try {
      const { resolve } = await createRequestContainer();
      const em = resolve("em");
      const cache = resolveBusinessRuleDiscoveryCache(resolve);
      const { BusinessRule: BusinessRule2 } = await import("../business_rules/data/entities.js");
      const rulesPath = path.join(__dirname, "examples", "guard-rules-example.json");
      const rulesData = JSON.parse(fs.readFileSync(rulesPath, "utf8"));
      let seededCount = 0;
      let skippedCount = 0;
      for (const ruleData of rulesData) {
        const existing = await em.findOne(BusinessRule2, {
          ruleId: ruleData.ruleId,
          tenantId,
          organizationId
        });
        if (existing) {
          console.log(`  \u2298 Guard rule '${ruleData.ruleId}' already exists`);
          skippedCount++;
          continue;
        }
        const rule = em.create(BusinessRule2, {
          ...ruleData,
          tenantId,
          organizationId
        });
        await em.persist(rule).flush();
        await invalidateBusinessRuleDiscoveryCache(cache, tenantId, organizationId);
        console.log(`  \u2713 Seeded guard rule: ${rule.ruleName}`);
        seededCount++;
      }
      console.log(`
\u2705 Checkout-demo guard rules seeded successfully!`);
      console.log(`  - Workflow: workflows.checkout-demo (code-defined)`);
      console.log(`  - Guard rules seeded: ${seededCount}`);
      console.log(`  - Guard rules skipped: ${skippedCount}`);
    } catch (error) {
      console.error("Error seeding demo guard rules:", error);
      throw error;
    }
  }
};
const seedSalesPipeline = {
  command: "seed-sales-pipeline",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? "");
    const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato workflows seed-sales-pipeline --tenant <tenantId> --org <organizationId>");
      return;
    }
    try {
      const { resolve } = await createRequestContainer();
      const em = resolve("em");
      const pipelinePath = path.join(__dirname, "examples", "sales-pipeline-definition.json");
      const pipelineData = JSON.parse(fs.readFileSync(pipelinePath, "utf8"));
      const existing = await em.findOne(WorkflowDefinition, {
        workflowId: pipelineData.workflowId,
        tenantId,
        organizationId
      });
      if (existing) {
        console.log(`\u2139\uFE0F  Sales pipeline workflow '${pipelineData.workflowId}' already exists (ID: ${existing.id})`);
        return;
      }
      const workflow = em.create(WorkflowDefinition, {
        ...pipelineData,
        tenantId,
        organizationId
      });
      await em.persist(workflow).flush();
      console.log(`\u2705 Seeded sales pipeline workflow: ${workflow.workflowName}`);
      console.log(`  - ID: ${workflow.id}`);
      console.log(`  - Workflow ID: ${workflow.workflowId}`);
      console.log(`  - Version: ${workflow.version}`);
      console.log(`  - Steps: ${workflow.definition.steps.length}`);
      console.log(`  - Transitions: ${workflow.definition.transitions.length}`);
      console.log(`  - Activities: ${workflow.definition.transitions.reduce((sum, t) => sum + (t.activities?.length || 0), 0)}`);
      console.log("");
      console.log("Sales pipeline workflow is ready!");
    } catch (error) {
      console.error("Error seeding sales pipeline workflow:", error);
      throw error;
    }
  }
};
const seedSimpleApproval = {
  command: "seed-simple-approval",
  async run() {
    console.log('\u2139\uFE0F  The "workflows.simple-approval" workflow is now code-defined.');
    console.log("   No seeding required \u2014 it is auto-registered by the workflows module");
    console.log("   (see packages/core/src/modules/workflows/workflows.ts).");
    console.log("");
    console.log("   Visit /backend/definitions to view it as a Code-defined workflow.");
  }
};
const seedOrderApproval = {
  command: "seed-order-approval",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? "");
    const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato workflows seed-order-approval --tenant <tenantId> --org <organizationId>");
      return;
    }
    try {
      const { resolve } = await createRequestContainer();
      const em = resolve("em");
      const cache = resolveBusinessRuleDiscoveryCache(resolve);
      const guardRulesPath = path.join(__dirname, "examples", "order-approval-guard-rules.json");
      const guardRulesData = JSON.parse(fs.readFileSync(guardRulesPath, "utf8"));
      let rulesSeeded = 0;
      let rulesSkipped = 0;
      for (const rule of guardRulesData) {
        const existingRule = await em.findOne(BusinessRule, {
          ruleId: rule.ruleId,
          tenantId,
          organizationId
        });
        if (existingRule) {
          rulesSkipped++;
          continue;
        }
        const newRule = em.create(BusinessRule, {
          ...rule,
          tenantId,
          organizationId
        });
        em.persist(newRule);
        console.log(`  \u2713 Seeded guard rule: ${rule.ruleName}`);
        rulesSeeded++;
      }
      if (rulesSeeded > 0) {
        await em.flush();
        await invalidateBusinessRuleDiscoveryCache(cache, tenantId, organizationId);
      }
      console.log(`\u2705 Seeded order approval guard rules`);
      console.log(`  - Guard rules seeded: ${rulesSeeded}`);
      console.log(`  - Guard rules skipped: ${rulesSkipped}`);
      console.log("");
      console.log('Note: The "sales.order-approval" workflow definition is now code-defined');
      console.log("(see packages/core/src/modules/sales/workflows.ts) and no longer needs DB seeding.");
    } catch (error) {
      console.error("Error seeding order approval workflow:", error);
      throw error;
    }
  }
};
const startWorker = {
  command: "start-worker",
  async run(rest) {
    const args = parseArgs(rest);
    const concurrency = parseInt(args.concurrency ?? args.c ?? "5");
    const strategy = process.env.QUEUE_STRATEGY === "async" ? "async" : "local";
    console.log("[Workflow Worker] Starting activity worker...");
    console.log(`[Workflow Worker] Strategy: ${strategy}`);
    if (strategy === "local") {
      const pollMs = process.env.LOCAL_QUEUE_POLL_MS || "5000";
      console.log(`[Workflow Worker] Polling interval: ${pollMs}ms`);
      console.log("[Workflow Worker] NOTE: Local strategy is for development only.");
      console.log("[Workflow Worker] Use QUEUE_STRATEGY=async with Redis for production.");
    } else {
      console.log(`[Workflow Worker] Concurrency: ${concurrency}`);
      console.log(`[Workflow Worker] Redis: ${getRedisUrl("QUEUE") ?? "(not configured)"}`);
    }
    try {
      const container = await createRequestContainer();
      const em = container.resolve("em");
      const { runWorker } = await import("@open-mercato/queue/worker");
      const { createActivityWorkerHandler } = await import("./lib/activity-worker-handler.js");
      const { WORKFLOW_ACTIVITIES_QUEUE_NAME } = await import("./lib/activity-queue-types.js");
      const handler = createActivityWorkerHandler(em, container);
      await runWorker({
        queueName: WORKFLOW_ACTIVITIES_QUEUE_NAME,
        handler,
        connection: strategy === "async" ? {
          url: getRedisUrlOrThrow("QUEUE")
        } : void 0,
        concurrency,
        gracefulShutdown: true
      });
    } catch (error) {
      console.error("[Workflow Worker] Failed to start worker:", error);
      throw error;
    }
  }
};
const seedAll = {
  command: "seed-all",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? "");
    const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato workflows seed-all --tenant <tenantId> --org <organizationId>");
      return;
    }
    console.log("\u{1F9E9} Seeding all example workflows...\n");
    try {
      await seedDemoWithRules.run(rest);
      console.log("");
      await seedSalesPipeline.run(rest);
      console.log("");
      await seedSimpleApproval.run(rest);
      console.log("");
      await seedOrderApproval.run(rest);
      console.log("");
      console.log("\u2705 All example workflows seeded successfully!");
    } catch (error) {
      console.error("Error seeding workflows:", error);
      throw error;
    }
  }
};
const processActivities = {
  command: "process-activities",
  async run(rest) {
    const args = parseArgs(rest);
    const limit = parseInt(args.limit ?? args.l ?? "0");
    console.log("[Workflow Activities] Processing pending activities...");
    if (limit > 0) {
      console.log(`[Workflow Activities] Limit: ${limit} jobs`);
    }
    try {
      const container = await createRequestContainer();
      const em = container.resolve("em");
      const { createQueue } = await import("@open-mercato/queue");
      const { createActivityWorkerHandler } = await import("./lib/activity-worker-handler.js");
      const { WORKFLOW_ACTIVITIES_QUEUE_NAME } = await import("./lib/activity-queue-types.js");
      const queue = createQueue(WORKFLOW_ACTIVITIES_QUEUE_NAME, "local");
      const handler = createActivityWorkerHandler(em, container);
      const initialCounts = await queue.getJobCounts();
      console.log(`[Workflow Activities] Pending jobs: ${initialCounts.waiting}`);
      if (initialCounts.waiting === 0) {
        console.log("[Workflow Activities] No jobs to process");
        await queue.close();
        return;
      }
      const result = await queue.process(handler, limit > 0 ? { limit } : void 0);
      console.log(`
[Workflow Activities] \u2713 Processed ${result.processed} activities`);
      if (result.failed > 0) {
        console.log(`[Workflow Activities] \u2717 Failed: ${result.failed} activities`);
      }
      const finalCounts = await queue.getJobCounts();
      if (finalCounts.waiting > 0) {
        console.log(`[Workflow Activities] Remaining: ${finalCounts.waiting} jobs`);
      }
      await queue.close();
    } catch (error) {
      console.error("[Workflow Activities] Error processing activities:", error);
      throw error;
    }
  }
};
const workflowsCliCommands = [
  startWorker,
  processActivities,
  seedDemo,
  seedDemoWithRules,
  seedSalesPipeline,
  seedSimpleApproval,
  seedOrderApproval,
  seedAll
];
var cli_default = workflowsCliCommands;
export {
  cli_default as default
};
//# sourceMappingURL=cli.js.map
