import { Database, eq } from "../src/drizzle/index.js"
import { BillingTable } from "../src/schema/billing.sql.js"

const workspaceID = process.argv[2]

if (!workspaceID) {
  console.error("Usage: bun script/foo.ts <workspaceID>")
  process.exit(1)
}

console.log(`Onboarding to Black waitlist`)

const billing = await Database.use((tx) =>
  tx
    .select({
      subscriptionPlan: BillingTable.subscriptionPlan,
      timeSubscriptionBooked: BillingTable.timeSubscriptionBooked,
    })
    .from(BillingTable)
    .where(eq(BillingTable.workspaceID, workspaceID))
    .then((rows) => rows[0]),
)

if (!billing?.timeSubscriptionBooked) {
  console.error(`Error: Workspace is not on the waitlist`)
  process.exit(1)
}

await Database.use((tx) =>
  tx
    .update(BillingTable)
    .set({
      timeSubscriptionSelected: new Date(),
    })
    .where(eq(BillingTable.workspaceID, workspaceID)),
)

console.log(`Done`)
