import postgres from "postgres";
import { readFileSync } from "fs";
import { join } from "path";

async function push() {
  const databaseUrl = process.env.DATABASE_URL;
  if (!databaseUrl) {
    console.error("DATABASE_URL environment variable is required");
    process.exit(1);
  }

  const client = postgres(databaseUrl);

  try {
    console.log("Pushing services table migration...");

    // Create the services table
    await client`
      CREATE TABLE IF NOT EXISTS services (
        id text PRIMARY KEY,
        name text NOT NULL,
        description text,
        unit_price numeric(10, 2) NOT NULL,
        category text,
        active boolean DEFAULT true NOT NULL,
        migrated_from text,
        migrated_id text,
        created_at timestamp with time zone DEFAULT now() NOT NULL
      )
    `;

    console.log("✓ services table created successfully");
    process.exit(0);
  } catch (err: unknown) {
    if (err instanceof Error) {
      if (err.message.includes("already exists")) {
        console.log("✓ services table already exists (skipped)");
        process.exit(0);
      }
      console.error("Error pushing migration:", err.message);
    } else {
      console.error("Unknown error:", err);
    }
    process.exit(1);
  }
}

push();
