import sequelize from "@/lib/sequelize";
import Model3D from "@/models/Model3D";
import Hotspot from "@/models/Hotspot";

const models = [
  {
    id: 'kidney',
    title: 'Automated Supermarket Kiosk',
    description: 'This self-service kiosk features a digital display with touchscreen capabilities...',
    speachtext: 'This self-service kiosk features a digital display with touchscreen capabilities...',
    image: '/images/kidney.png',
    modelSrc: '/assets/kidney.glb',
    category: 'Microbiology',
    program: 'B.Tech',
    size: '84.58 MB',
    date: 'Jan 28, 2026',
    stackCount: 7,
    hotspots: [
      {
        position: '0.0023977800979993772 0.15188296254679487 -0.0037125437117008697',
        normal: '-0.2607027611678235 0.20385461235882174 0.9436510834728626',
        title: 'Arota',
        description: 'Filters blood and removes waste.',
      },
      {
        position: '-0.007516617592739063 0.14105889723183535 -0.0013936913564640916',
        normal: '0.09549487072136117 0.24684320945429242 0.964338716225899',
        title: 'Right Kidney',
        description: 'Maintains fluid balance.',
      },
      {
        position: '0.008118954210167348 0.14322039248683746 -0.002361464017771057',
        normal: '0.09959891801053512 0.4871400077765798 0.8676258804084654',
        title: 'Right Kidney',
        description: 'Maintains fluid balance.',
      },
    ],
  },
  {
    id: 'globe',
    title: 'Earth_ Detailed Terrestrial',
    description: 'Earth_ Detailed Terrestrial Globe..',
      speachtext: 'Earth_ Detailed Terrestrial Globe.',
    image: '/images/globe.png',
    modelSrc: '/assets/globe.glb',
    category: 'Geography',
    program: 'SYBSC (CS)',
    size: '3.16 MB',
    date: 'Jan 17, 2026',
    stackCount: 7,
    tag: 'nox',
    hotspots: [
      {
        position: '0.6248049436821395 0.5574017531775765 -0.10045698822176828',
        normal: '0.9722436071768805 -0.1611811085519079 -0.16959663484180904',
        title: 'Water Surface',
      },
      {
        position: '0.5756336102993389 0.8348656582605798 0.2120849581769921',
        normal: '0.899699319471531 0.2939315436312065 0.3227156367470292',
        title: 'Earth',
      },
    ],
  },
  {
    id: 'keyboard',
    title: 'Keyboard Pseudomonas',
    description: 'A keyboard is an input device used to enter text, numbers, and comman..',
    speachtext: 'A keyboard is an input device used to enter text, numbers, and commands.',
    image: '/images/keyboard.png',
    modelSrc: '/assets/keyboard.glb',
    category: 'Computer Science',
    program: 'B.S.C',
    size: '81.75 MB',
    date: 'Jan 17, 2026',
    stackCount: 3,
    hotspots: [],
  },
  {
    id: 'bacterium',
    title: 'Taxoplasma Gondii Structure',
    description: 'The bacterium features a distinct rod-like shape and a textured...',
    image: '/images/bacterium.png',
    modelSrc: '/assets/bacterium.glb',
    category: 'Microbiology',
    program: 'B.S.C',
    size: '27.57 MB',
    date: 'Jun 14, 2025',
    stackCount: 12,
    hotspots: [
      {
        position: '653.6417366512724 -170.8185997326823 -344.13086330968537',
        normal: '0.6327663522353819 0.7737557002143103 0.030147302774727262',
        title: 'pilus',
      },
      {
        position: '414.91647990673005 -76.64563680998182 -360.32441008609624',
        normal: '-0.625234638474148 0.4953190128251862 0.6031092126522232',
        title: 'Cell membrane',
      },
    ],
  },
];

async function main() {
  try {
    console.log("🌱 Seeding database...");

    // Sync tables (dev only)
    await sequelize.sync({ alter: true });

    // Seed Model3D and Hotspots
    for (const model of models) {
      const createdModel = await Model3D.create({
        title: model.title,
        description: model.description,
        speachtext: model?.speachtext ?? "",
        image: model.image,
        modelSrc: model.modelSrc,
        category: model.category,
        program: model.program,
        size: model.size,
        date: model.date,
        stackCount: model.stackCount,
        tag: model.tag ?? null,
      });

      // Seed hotspots
      if (model.hotspots && model.hotspots.length > 0) {
        const hotspotsData = model.hotspots.map((hotspot) => ({
          modelId: createdModel.id,
          position: hotspot.position,
          normal: hotspot.normal,
          title: hotspot.title,
          description: null,
        }));

        await Hotspot.bulkCreate(hotspotsData, { ignoreDuplicates: true });
      }
    }

    console.log("✅ Database seeding finished!");
  } catch (error) {
    console.error("❌ Error seeding database:", error);
  } finally {
    await sequelize.close();
  }
}

// Run seed
main();