import { PrismaClient } from '@prisma/client';

import { hashToken } from '@hexahost/auth';

/** Stable dev node ID — must match NODE_ID in .env for local agent */
export const DEV_NODE_ID = '00000000-0000-4000-8000-000000000001';

/** Shown once in seed output; hash stored in DB */
export const DEV_NODE_ENROLLMENT_TOKEN =
  'local-dev-enrollment-token-change-me-32chars';

async function main(): Promise<void> {
  const prisma = new PrismaClient();

  try {
    const freePlan = await prisma.plan.upsert({
      where: { slug: 'free' },
      create: {
        name: 'Free',
        slug: 'free',
        description: 'Free tier with limited resources',
        isFree: true,
        isActive: true,
        maxRamMb: 2048,
        maxCpuCores: 1,
        maxStorageMb: 5120,
      },
      update: {
        isActive: true,
      },
    });

    const devNode = await prisma.gameNode.upsert({
      where: { id: DEV_NODE_ID },
      create: {
        id: DEV_NODE_ID,
        name: 'local-dev',
        hostname: 'localhost',
        status: 'OFFLINE',
        maxServers: 10,
        enrollmentTokenHash: hashToken(DEV_NODE_ENROLLMENT_TOKEN),
      },
      update: {
        name: 'local-dev',
        hostname: 'localhost',
        enrollmentTokenHash: hashToken(DEV_NODE_ENROLLMENT_TOKEN),
      },
    });

    console.log('Seed complete:');
    console.log(`  Plan Free: ${freePlan.id}`);
    console.log(`  Dev node:  ${devNode.id} (${devNode.name})`);
    console.log(`  Enrollment token (set NODE_TOKEN): ${DEV_NODE_ENROLLMENT_TOKEN}`);
  } finally {
    await prisma.$disconnect();
  }
}

void main().catch((error: unknown) => {
  console.error(error instanceof Error ? error.message : error);
  process.exit(1);
});
