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

const prisma = new PrismaClient();

async function main() {
  // Seed admin user
  const adminPassword = await bcrypt.hash('admin123', 10);
  await prisma.user.upsert({
    where: { email: 'admin@gwizard.com' },
    update: {},
    create: {
      email: 'admin@gwizard.com',
      password: adminPassword,
      firstName: 'Admin',
      lastName: 'User',
      isAdmin: true,
    },
  });

  // Seed test user
  const hashedPassword = await bcrypt.hash('johndoe123', 10);
  await prisma.user.upsert({
    where: { email: 'john@doe.com' },
    update: {},
    create: {
      email: 'john@doe.com',
      password: hashedPassword,
      firstName: 'John',
      lastName: 'Doe',
      isAdmin: false,
    },
  });

  // Seed Materials
  const materials = [
    // Aluminum
    { name: 'Aluminum 6061-T6', category: 'Aluminum', sfm: 800, chipLoad: 0.003, description: 'General purpose aluminum alloy' },
    { name: 'Aluminum 7075-T6', category: 'Aluminum', sfm: 600, chipLoad: 0.0025, description: 'High strength aerospace aluminum' },
    { name: 'Cast Aluminum', category: 'Aluminum', sfm: 500, chipLoad: 0.002, description: 'Cast aluminum alloys' },
    // Steel
    { name: 'Steel 1018 Mild', category: 'Steel', sfm: 100, chipLoad: 0.002, description: 'Low carbon mild steel' },
    { name: 'Steel 4140', category: 'Steel', sfm: 80, chipLoad: 0.0015, description: 'Chromoly alloy steel' },
    { name: 'Steel 1045', category: 'Steel', sfm: 90, chipLoad: 0.0018, description: 'Medium carbon steel' },
    // Stainless Steel
    { name: 'Stainless 304', category: 'Stainless Steel', sfm: 65, chipLoad: 0.0012, description: 'Austenitic stainless steel' },
    { name: 'Stainless 316', category: 'Stainless Steel', sfm: 55, chipLoad: 0.001, description: 'Marine grade stainless' },
    // Other Metals
    { name: 'Brass 360', category: 'Brass', sfm: 300, chipLoad: 0.003, description: 'Free machining brass' },
    { name: 'Bronze', category: 'Bronze', sfm: 200, chipLoad: 0.0025, description: 'General bronze alloy' },
    { name: 'Copper', category: 'Copper', sfm: 200, chipLoad: 0.002, description: 'Pure copper' },
    // Plastics
    { name: 'Delrin (POM)', category: 'Plastics', sfm: 500, chipLoad: 0.005, description: 'Acetal/Polyoxymethylene' },
    { name: 'HDPE', category: 'Plastics', sfm: 600, chipLoad: 0.006, description: 'High density polyethylene' },
    { name: 'Acrylic (PMMA)', category: 'Plastics', sfm: 400, chipLoad: 0.004, description: 'Clear acrylic sheets' },
    { name: 'Nylon', category: 'Plastics', sfm: 450, chipLoad: 0.004, description: 'Polyamide' },
    { name: 'UHMW', category: 'Plastics', sfm: 600, chipLoad: 0.006, description: 'Ultra high molecular weight PE' },
    // Woods
    { name: 'Red Oak', category: 'Hardwood', sfm: 600, chipLoad: 0.01, description: 'Domestic hardwood' },
    { name: 'Walnut', category: 'Hardwood', sfm: 650, chipLoad: 0.012, description: 'Premium hardwood' },
    { name: 'Maple', category: 'Hardwood', sfm: 550, chipLoad: 0.008, description: 'Hard maple' },
    { name: 'Cherry', category: 'Hardwood', sfm: 700, chipLoad: 0.012, description: 'American cherry' },
    { name: 'MDF', category: 'Engineered Wood', sfm: 800, chipLoad: 0.015, description: 'Medium density fiberboard' },
    { name: 'Plywood', category: 'Engineered Wood', sfm: 700, chipLoad: 0.012, description: 'Multi-layer wood product' },
    { name: 'Pine', category: 'Softwood', sfm: 900, chipLoad: 0.015, description: 'Softwood lumber' },
    { name: 'Birch Plywood', category: 'Engineered Wood', sfm: 650, chipLoad: 0.01, description: 'Baltic birch plywood' },
  ];

  for (const mat of materials) {
    await prisma.material.upsert({
      where: { id: mat.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase() },
      update: mat,
      create: { id: mat.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase(), ...mat },
    });
  }

  // Seed Tools
  const tools = [
    // End Mills
    { name: '1/8" 2-Flute End Mill', type: 'End Mill', diameter: 0.125, flutes: 2, coating: 'Uncoated', description: 'General purpose' },
    { name: '1/4" 2-Flute End Mill', type: 'End Mill', diameter: 0.25, flutes: 2, coating: 'Uncoated', description: 'General purpose' },
    { name: '1/4" 3-Flute End Mill', type: 'End Mill', diameter: 0.25, flutes: 3, coating: 'TiAlN', description: 'High performance' },
    { name: '3/8" 3-Flute End Mill', type: 'End Mill', diameter: 0.375, flutes: 3, coating: 'TiAlN', description: 'High performance' },
    { name: '1/2" 4-Flute End Mill', type: 'End Mill', diameter: 0.5, flutes: 4, coating: 'TiN', description: 'Finishing' },
    { name: '1/2" 2-Flute End Mill', type: 'End Mill', diameter: 0.5, flutes: 2, coating: 'Uncoated', description: 'Aluminum roughing' },
    { name: '3/4" 4-Flute End Mill', type: 'End Mill', diameter: 0.75, flutes: 4, coating: 'TiAlN', description: 'Heavy roughing' },
    { name: '1" 4-Flute End Mill', type: 'End Mill', diameter: 1.0, flutes: 4, coating: 'TiN', description: 'Large roughing' },
    // Ball Nose
    { name: '1/8" Ball Nose', type: 'Ball Nose', diameter: 0.125, flutes: 2, coating: 'Uncoated', description: '3D contouring' },
    { name: '1/4" Ball Nose', type: 'Ball Nose', diameter: 0.25, flutes: 2, coating: 'TiAlN', description: '3D contouring' },
    { name: '1/2" Ball Nose', type: 'Ball Nose', diameter: 0.5, flutes: 2, coating: 'TiAlN', description: '3D finishing' },
    // Router Bits - Upcut
    { name: '1/8" Upcut Spiral', type: 'Upcut Router', diameter: 0.125, flutes: 1, coating: 'Uncoated', description: 'Wood/plastic upcut' },
    { name: '1/4" Upcut Spiral', type: 'Upcut Router', diameter: 0.25, flutes: 2, coating: 'Uncoated', description: 'Wood/plastic upcut' },
    { name: '3/8" Upcut Spiral', type: 'Upcut Router', diameter: 0.375, flutes: 2, coating: 'Uncoated', description: 'Wood/plastic upcut' },
    // Router Bits - Downcut
    { name: '1/8" Downcut Spiral', type: 'Downcut Router', diameter: 0.125, flutes: 1, coating: 'Uncoated', description: 'Clean top edge' },
    { name: '1/4" Downcut Spiral', type: 'Downcut Router', diameter: 0.25, flutes: 2, coating: 'Uncoated', description: 'Clean top edge' },
    // Compression
    { name: '1/4" Compression', type: 'Compression Router', diameter: 0.25, flutes: 2, coating: 'Uncoated', description: 'Clean both edges' },
    // V-Bits
    { name: '60° V-Bit', type: 'V-Bit', diameter: 0.5, flutes: 2, coating: 'Uncoated', description: 'Engraving, chamfers' },
    { name: '90° V-Bit', type: 'V-Bit', diameter: 0.5, flutes: 2, coating: 'Uncoated', description: 'Engraving, chamfers' },
    // Drills
    { name: '1/8" HSS Drill', type: 'Drill', diameter: 0.125, flutes: 2, coating: 'Uncoated', description: 'General drilling' },
    { name: '1/4" Carbide Drill', type: 'Drill', diameter: 0.25, flutes: 2, coating: 'TiN', description: 'Precision drilling' },
    { name: '1/2" HSS Drill', type: 'Drill', diameter: 0.5, flutes: 2, coating: 'Uncoated', description: 'Large holes' },
  ];

  for (const tool of tools) {
    await prisma.tool.upsert({
      where: { id: tool.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase() },
      update: tool,
      create: { id: tool.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase(), ...tool },
    });
  }

  // Seed Machines
  const machines = [
    // Onefinity with Buildbotics
    { name: 'Onefinity Woodworker X-35 (Buildbotics)', maxRpm: 18000, maxFeedRate: 200, horsepower: 2.25, spindleType: 'Router', controller: 'Buildbotics', description: '32"x32" CNC router' },
    { name: 'Onefinity Machinist X-35 (Buildbotics)', maxRpm: 24000, maxFeedRate: 300, horsepower: 2.25, spindleType: 'Spindle 80mm', controller: 'Buildbotics', description: '32"x32" with spindle' },
    { name: 'Onefinity Woodworker X-50 (Buildbotics)', maxRpm: 18000, maxFeedRate: 200, horsepower: 2.25, spindleType: 'Router', controller: 'Buildbotics', description: '48"x32" CNC router' },
    { name: 'Onefinity Pro (Buildbotics)', maxRpm: 24000, maxFeedRate: 400, horsepower: 3.0, spindleType: 'Spindle 80mm', controller: 'Buildbotics', description: 'Pro series CNC' },
    // Onefinity with Masso
    { name: 'Onefinity Woodworker (Masso G3)', maxRpm: 18000, maxFeedRate: 250, horsepower: 2.25, spindleType: 'Router', controller: 'Masso G3', description: 'With Masso upgrade' },
    { name: 'Onefinity Machinist (Masso G3)', maxRpm: 24000, maxFeedRate: 400, horsepower: 2.25, spindleType: 'Spindle 80mm', controller: 'Masso G3', description: 'With Masso upgrade' },
    { name: 'Onefinity Elite Foreman (Masso)', maxRpm: 24000, maxFeedRate: 500, horsepower: 3.5, spindleType: 'Spindle 80mm', controller: 'Masso', description: '48"x48" Elite series' },
    // Redline Controllers
    { name: 'Generic CNC (Redline Basic)', maxRpm: 18000, maxFeedRate: 200, horsepower: 1.5, spindleType: 'Router', controller: 'Redline Basic', description: 'Basic Redline setup' },
    { name: 'Generic CNC (Redline Pro)', maxRpm: 24000, maxFeedRate: 400, horsepower: 2.5, spindleType: 'Spindle', controller: 'Redline Pro', description: 'Pro Redline setup' },
    { name: 'Generic CNC (Redline Industrial)', maxRpm: 30000, maxFeedRate: 600, horsepower: 4.0, spindleType: 'Spindle HF', controller: 'Redline Industrial', description: 'Industrial Redline' },
    // Other common machines
    { name: 'Shapeoko 4', maxRpm: 18000, maxFeedRate: 150, horsepower: 1.25, spindleType: 'Router', controller: 'Carbide Motion', description: 'Carbide 3D Shapeoko' },
    { name: 'X-Carve', maxRpm: 24000, maxFeedRate: 200, horsepower: 1.0, spindleType: 'Spindle', controller: 'Easel', description: 'Inventables X-Carve' },
    { name: 'AVID CNC Pro', maxRpm: 24000, maxFeedRate: 800, horsepower: 5.0, spindleType: 'Spindle', controller: 'Mach4', description: 'Professional AVID CNC' },
  ];

  for (const machine of machines) {
    await prisma.machine.upsert({
      where: { id: machine.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase() },
      update: machine,
      create: { id: machine.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase(), ...machine },
    });
  }

  console.log('Database seeded successfully!');
}

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