import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { seedStaffActivityTypes, seedStaffAddressTypes, seedStaffTeamExamples } from "./lib/seeds.js";
import { appendWidgetsToRoles } from "@open-mercato/core/modules/dashboards/lib/role-widgets";
const TIMESHEETS_DASHBOARD_WIDGET_IDS = [
  "staff.timesheets.timeReporting",
  "staff.timesheets.hoursByProject"
];
function parseArgs(rest) {
  const args = {};
  for (let i = 0; i < rest.length; i += 1) {
    const part = rest[i];
    if (!part) continue;
    if (part.startsWith("--")) {
      const [rawKey, rawValue] = part.slice(2).split("=");
      if (rawValue !== void 0) args[rawKey] = rawValue;
      else if (rest[i + 1] && !rest[i + 1].startsWith("--")) {
        args[rawKey] = rest[i + 1];
        i += 1;
      }
    }
  }
  return args;
}
const seedExamplesCommand = {
  command: "seed-examples",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato staff seed-examples --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    const scope = { tenantId, organizationId };
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await seedStaffTeamExamples(tem, scope);
      });
      console.log("\u{1F9E9} Staff team examples seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedActivityTypesCommand = {
  command: "seed-activity-types",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato staff seed-activity-types --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    const scope = { tenantId, organizationId };
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await seedStaffActivityTypes(tem, scope);
      });
      console.log("\u{1F5C2}\uFE0F  Staff activity types seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedAddressTypesCommand = {
  command: "seed-address-types",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato staff seed-address-types --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    const scope = { tenantId, organizationId };
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await seedStaffAddressTypes(tem, scope);
      });
      console.log("\u{1F3E0} Staff address types seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedTimesheetsWidgetsCommand = {
  command: "seed-timesheets-widgets",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato staff seed-timesheets-widgets --tenant <tenantId> --org <organizationId>");
      console.error("Backfills timesheets dashboard widgets (timeReporting, hoursByProject) to existing tenant roles.");
      process.exit(1);
      return;
    }
    const container = await createRequestContainer();
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await appendWidgetsToRoles(tem, {
          tenantId,
          organizationId,
          roleNames: ["superadmin", "admin", "employee"],
          widgetIds: TIMESHEETS_DASHBOARD_WIDGET_IDS
        });
      });
      console.log("\u{1F4CA} Timesheets dashboard widgets seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
var cli_default = [seedActivityTypesCommand, seedAddressTypesCommand, seedExamplesCommand, seedTimesheetsWidgetsCommand];
export {
  cli_default as default
};
//# sourceMappingURL=cli.js.map
