import { syncTableUpdates } from "./index";

/**
 * Example usage of syncTableUpdates
 * 
 * Usage from CLI:
 * bun run src/sync-table-updates.ts <table-name> <num-records>
 * 
 * Example:
 * bun run src/sync-table-updates.ts opportunity 100
 * bun run src/sync-table-updates.ts srService 50
 * bun run src/sync-table-updates.ts company 10
 */

const main = async () => {
  const args = process.argv.slice(2);

  if (args.length < 2) {
    console.error("Usage: bun run src/sync-table-updates.ts <table> <numRec>");
    console.error("\nSupported tables:");
    console.error(
      "  - member, cwMember, company, companyAddress, contact, ownerLevel"
    );
    console.error(
      "  - department, productType, productCategory, productSubcategory"
    );
    console.error(
      "  - manufacturer, warehouseBin, productCatalog, productInventory"
    );
    console.error(
      "  - srType, srLocation, srSource, srImpact, srSeverity, srUrgency"
    );
    console.error(
      "  - srBoard, soType, soOppStatus, opportunity, srService, iV_Product"
    );
    console.error("  - ticketNote");
    console.error("\nExample: bun run src/sync-table-updates.ts opportunity 100");
    process.exit(1);
  }

  const table = args[0];
  const numRec = Number.parseInt(args[1], 10);

  if (Number.isNaN(numRec) || numRec <= 0) {
    console.error("Error: numRec must be a positive integer");
    process.exit(1);
  }

  try {
    console.log(`Syncing ${numRec} records from table: ${table}`);
    console.log("Pulling records ordered by lastUpdatedUTC (most recent first)");
    console.log("");

    const result = await syncTableUpdates(table, numRec);

    console.log("\nSync completed!");
    console.log(`Inserted/Updated: ${result.insertedOrUpdated}`);
    console.log(`Skipped: ${result.skipped}`);
    console.log(`Failed: ${result.failed}`);
    console.log(
      `Total: ${result.insertedOrUpdated + result.skipped + result.failed}`
    );
  } catch (error) {
    console.error("Sync failed:", error);
    process.exit(1);
  }
};

main();
