import "dotenv/config"
import { ConvexHttpClient } from "convex/browser";

const url = process.env.NEXT_PUBLIC_CONVEX_URL;

if (!url) {
  console.error("Missing NEXT_PUBLIC_CONVEX_URL");
  process.exit(1);
}

const client = new ConvexHttpClient(url);

const tenantId = process.argv[2] ?? "tenant-atlas";

const ensureAdmin = await client.mutation("users:ensureUser", {
  tenantId,
  email: "admin@sistema.dev",
  name: "Administrador",
  role: "ADMIN",
});

console.log("Ensured admin user:", ensureAdmin);

const agents = await client.query("users:listAgents", { tenantId });
console.log("Agents:", agents);

const viewerId = ensureAdmin?._id ?? agents[0]?._id;

if (!viewerId) {
  console.error("Unable to determine viewer id");
  process.exit(1);
}

const tickets = await client.query("tickets:list", {
  tenantId,
  viewerId,
  limit: 10,
});

console.log("Tickets:", tickets);

const dashboard = await client.query("reports:dashboardOverview", {
  tenantId,
  viewerId,
});

console.log("Dashboard:", dashboard);
