import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";

const prisma = new PrismaClient();

async function main() {
  const superadminEmail = process.env.SUPERADMIN_EMAIL || "admin@edudeploy.fr";
  const superadminPassword = process.env.SUPERADMIN_PASSWORD || "CHANGE_ME";

  const hashedPassword = await bcrypt.hash(superadminPassword, 12);

  await prisma.user.upsert({
    where: { email: superadminEmail },
    update: {},
    create: {
      email: superadminEmail,
      password: hashedPassword,
      role: "superadmin",
    },
  });

  const templates = [
    {
      name: "WordPress latest vierge",
      type: "wordpress",
      dockerImage: "wordpress:latest",
      dbImage: "mariadb:10.11",
      dbName: "wordpress",
      dbUser: "wordpress",
      dbPassword: "wordpress",
      dbRootPassword: "rootpassword",
    },
    {
      name: "WordPress 6.7 vierge",
      type: "wordpress",
      dockerImage: "wordpress:6.7",
      dbImage: "mariadb:10.11",
      dbName: "wordpress",
      dbUser: "wordpress",
      dbPassword: "wordpress",
      dbRootPassword: "rootpassword",
    },
    {
      name: "WordPress 6.4 vierge",
      type: "wordpress",
      dockerImage: "wordpress:6.4",
      dbImage: "mariadb:10.11",
      dbName: "wordpress",
      dbUser: "wordpress",
      dbPassword: "wordpress",
      dbRootPassword: "rootpassword",
    },
    {
      name: "PrestaShop latest vierge",
      type: "prestashop",
      dockerImage: "prestashop/prestashop:latest",
      dbImage: "mariadb:10.11",
      dbName: "prestashop",
      dbUser: "prestashop",
      dbPassword: "prestashop",
      dbRootPassword: "rootpassword",
    },
    {
      name: "PrestaShop 8.1 vierge",
      type: "prestashop",
      dockerImage: "prestashop/prestashop:8.1",
      dbImage: "mariadb:10.11",
      dbName: "prestashop",
      dbUser: "prestashop",
      dbPassword: "prestashop",
      dbRootPassword: "rootpassword",
    },
  ];

  for (const t of templates) {
    const dbHost = "db";
    const dbPort = "3306";
    const composeConfig = `services:
  db:
    image: ${t.dbImage}
    environment:
      MYSQL_ROOT_PASSWORD: ${t.dbRootPassword}
      MYSQL_DATABASE: ${t.dbName}
      MYSQL_USER: ${t.dbUser}
      MYSQL_PASSWORD: ${t.dbPassword}
    volumes:
      - db_data:/var/lib/mysql
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 5s
      timeout: 5s
      retries: 10
  app:
    image: ${t.dockerImage}
    ports:
      - "127.0.0.1:{PORT}:80"
    environment:
      WORDPRESS_DB_HOST: ${dbHost}:${dbPort}
      WORDPRESS_DB_NAME: ${t.dbName}
      WORDPRESS_DB_USER: ${t.dbUser}
      WORDPRESS_DB_PASSWORD: ${t.dbPassword}
      WORDPRESS_DB_PREFIX: wp_
      WORDPRESS_CONFIG_EXTRA: |
        define('WP_HOME', 'http://localhost:{PORT}');
        define('WP_SITEURL', 'http://localhost:{PORT}');
      PS_DB_HOST: ${dbHost}:${dbPort}
      PS_DB_NAME: ${t.dbName}
      PS_DB_USER: ${t.dbUser}
      PS_DB_PASSWORD: ${t.dbPassword}
      PS_DB_PREFIX: ps_
      PS_DOMAIN: localhost:{PORT}
      PS_SHOP_NAME: ${t.name}
      PS_INSTALL_AUTO: "0"
      INSTANCE_ID: {INSTANCE_ID}
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - app_data:/var/www/html
    restart: unless-stopped
volumes:
  db_data:
  app_data:
`;
    await prisma.template.upsert({
      where: { id: `${t.type}-${t.dockerImage.replace(/[:\/]/g, "-")}` },
      update: { composeConfig },
      create: {
        id: `${t.type}-${t.dockerImage.replace(/[:\/]/g, "-")}`,
        name: t.name,
        type: t.type,
        dockerImage: t.dockerImage,
        composeConfig,
        isPublic: true,
        createdBy: "system",
      },
    });
  }

  console.log("Seed completed.");
}

main()
  .then(async () => {
    await prisma.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });
