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';

export const DEV_NODE_ID_2 = '00000000-0000-4000-8000-000000000002';

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

export const DEV_NODE_2_ENROLLMENT_TOKEN =
  'local-dev-node2-enrollment-token-32ch';

export const DEV_WHMCS_INTEGRATION_ID = 'local-dev-whmcs';

export const DEV_WHMCS_API_SECRET =
  'local-dev-whmcs-api-secret-32chars-minimum';

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,
        maxServersPerUser: 3,
        idleTimeoutMinutes: 10,
        idleGraceAfterStartMinutes: 2,
        idleCountdownSeconds: 60,
        queuePriority: 0,
        monthlyCreditGrant: 5000,
      },
      update: {
        isActive: true,
        maxServersPerUser: 3,
        idleTimeoutMinutes: 10,
        idleGraceAfterStartMinutes: 2,
        idleCountdownSeconds: 60,
        queuePriority: 0,
        monthlyCreditGrant: 5000,
      },
    });

    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,
        maxRamMb: 16384,
        platformRamMb: 2048,
        portRangeStart: 25565,
        portRangeEnd: 25664,
        enrollmentTokenHash: hashToken(DEV_NODE_ENROLLMENT_TOKEN),
      },
      update: {
        name: 'local-dev',
        hostname: 'localhost',
        maxRamMb: 16384,
        platformRamMb: 2048,
        portRangeStart: 25565,
        portRangeEnd: 25664,
        enrollmentTokenHash: hashToken(DEV_NODE_ENROLLMENT_TOKEN),
      },
    });

    const devNode2 = await prisma.gameNode.upsert({
      where: { id: DEV_NODE_ID_2 },
      create: {
        id: DEV_NODE_ID_2,
        name: 'local-dev-2',
        hostname: 'localhost',
        status: 'OFFLINE',
        maxServers: 10,
        maxRamMb: 16384,
        platformRamMb: 2048,
        enrollmentTokenHash: hashToken(DEV_NODE_2_ENROLLMENT_TOKEN),
      },
      update: {
        name: 'local-dev-2',
        hostname: 'localhost',
        maxRamMb: 16384,
        platformRamMb: 2048,
        enrollmentTokenHash: hashToken(DEV_NODE_2_ENROLLMENT_TOKEN),
      },
    });

    const whmcsInstallation = await prisma.whmcsInstallation.upsert({
      where: { integrationId: DEV_WHMCS_INTEGRATION_ID },
      create: {
        integrationId: DEV_WHMCS_INTEGRATION_ID,
        name: 'Local Development',
        apiSecret: DEV_WHMCS_API_SECRET,
        isActive: true,
      },
      update: {
        apiSecret: DEV_WHMCS_API_SECRET,
        isActive: true,
      },
    });

    console.log('Seed complete:');
    console.log(`  Plan Free: ${freePlan.id}`);
    console.log(`  Dev node:  ${devNode.id} (${devNode.name})`);
    console.log(`  Dev node 2: ${devNode2.id} (${devNode2.name})`);
    console.log(`  Enrollment token (set NODE_TOKEN): ${DEV_NODE_ENROLLMENT_TOKEN}`);
    console.log(`  Node 2 enrollment token: ${DEV_NODE_2_ENROLLMENT_TOKEN}`);
    console.log(`  WHMCS integration: ${whmcsInstallation.integrationId}`);
    console.log(`  WHMCS API secret: ${DEV_WHMCS_API_SECRET}`);
  } finally {
    await prisma.$disconnect();
  }
}

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