import { pool } from "./config/db.js";

const inventoryItems = [
  {
    product_code: "DET-LIQ-001",
    product_name: "Detergente liquido concentrado",
    categoria: "Lavado",
    stock: 8,
    min_stock: 3,
    unit_cost: 18.5,
    unit: "galon",
    supplier: "Distribuidora Clean Pro"
  },
  {
    product_code: "DET-POW-002",
    product_name: "Detergente en polvo industrial",
    categoria: "Lavado",
    stock: 120,
    min_stock: 40,
    unit_cost: 1.35,
    unit: "libra",
    supplier: "Quimicos del Caribe"
  },
  {
    product_code: "SOFT-001",
    product_name: "Suavizante floral",
    categoria: "Lavado",
    stock: 5,
    min_stock: 4,
    unit_cost: 15.75,
    unit: "galon",
    supplier: "Distribuidora Clean Pro"
  },
  {
    product_code: "BLEACH-001",
    product_name: "Blanqueador cloro",
    categoria: "Lavado",
    stock: 6,
    min_stock: 3,
    unit_cost: 9.25,
    unit: "galon",
    supplier: "Quimicos del Caribe"
  },
  {
    product_code: "STAIN-001",
    product_name: "Removedor de manchas multiuso",
    categoria: "Tratamiento",
    stock: 4,
    min_stock: 2,
    unit_cost: 22,
    unit: "galon",
    supplier: "Laundry Supply PR"
  },
  {
    product_code: "DEG-001",
    product_name: "Desengrasante textil",
    categoria: "Tratamiento",
    stock: 2,
    min_stock: 3,
    unit_cost: 26,
    unit: "galon",
    supplier: "Laundry Supply PR"
  },
  {
    product_code: "BAG-PL-001",
    product_name: "Bolsas plasticas para ropa",
    categoria: "Empaque",
    stock: 450,
    min_stock: 150,
    unit_cost: 0.08,
    unit: "unidad",
    supplier: "Empaques Universal"
  },
  {
    product_code: "HANGER-001",
    product_name: "Ganchos metalicos",
    categoria: "Empaque",
    stock: 320,
    min_stock: 100,
    unit_cost: 0.12,
    unit: "unidad",
    supplier: "Empaques Universal"
  },
  {
    product_code: "TAG-001",
    product_name: "Etiquetas para ordenes",
    categoria: "Operacion",
    stock: 900,
    min_stock: 250,
    unit_cost: 0.02,
    unit: "unidad",
    supplier: "Papeleria Central"
  },
  {
    product_code: "ROLL-001",
    product_name: "Rollo termico para recibos",
    categoria: "Operacion",
    stock: 18,
    min_stock: 8,
    unit_cost: 2.4,
    unit: "rollo",
    supplier: "Papeleria Central"
  },
  {
    product_code: "THREAD-BLK",
    product_name: "Hilo negro industrial",
    categoria: "Sastreria",
    stock: 7,
    min_stock: 3,
    unit_cost: 3.5,
    unit: "unidad",
    supplier: "Sastreria Supply"
  },
  {
    product_code: "ZIP-STD-001",
    product_name: "Cierres estandar surtidos",
    categoria: "Sastreria",
    stock: 35,
    min_stock: 20,
    unit_cost: 0.75,
    unit: "unidad",
    supplier: "Sastreria Supply"
  },
  {
    product_code: "NEEDLE-001",
    product_name: "Agujas para maquina industrial",
    categoria: "Sastreria",
    stock: 12,
    min_stock: 10,
    unit_cost: 0.45,
    unit: "unidad",
    supplier: "Sastreria Supply"
  },
  {
    product_code: "SANIT-001",
    product_name: "Desinfectante de superficies",
    categoria: "Limpieza",
    stock: 3,
    min_stock: 4,
    unit_cost: 11,
    unit: "galon",
    supplier: "Higiene Total"
  },
  {
    product_code: "GLOVE-001",
    product_name: "Guantes nitrilo caja",
    categoria: "Limpieza",
    stock: 9,
    min_stock: 5,
    unit_cost: 6.2,
    unit: "caja",
    supplier: "Higiene Total"
  }
];

async function ensureProductsTable() {
  await pool.query(`
    CREATE TABLE IF NOT EXISTS products (
      id INT AUTO_INCREMENT PRIMARY KEY,
      product_code VARCHAR(50) NOT NULL UNIQUE,
      product_name VARCHAR(160) NOT NULL,
      categoria VARCHAR(100) NOT NULL DEFAULT 'General',
      stock DECIMAL(10,2) NOT NULL DEFAULT 0,
      min_stock DECIMAL(10,2) NOT NULL DEFAULT 0,
      unit_cost DECIMAL(10,2) NOT NULL DEFAULT 0,
      unit VARCHAR(30) NOT NULL DEFAULT 'unidad',
      supplier VARCHAR(160) NULL,
      is_active TINYINT(1) NOT NULL DEFAULT 1,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  `);
}

async function seed() {
  try {
    await ensureProductsTable();
    console.log("Seeding inventory products...");

    for (const item of inventoryItems) {
      await pool.query(
        `
        INSERT INTO products
          (product_code, product_name, categoria, stock, min_stock, unit_cost, unit, supplier, is_active, created_at)
        VALUES
          (:product_code, :product_name, :categoria, :stock, :min_stock, :unit_cost, :unit, :supplier, 1, NOW())
        ON DUPLICATE KEY UPDATE
          product_name = VALUES(product_name),
          categoria = VALUES(categoria),
          stock = VALUES(stock),
          min_stock = VALUES(min_stock),
          unit_cost = VALUES(unit_cost),
          unit = VALUES(unit),
          supplier = VALUES(supplier),
          is_active = 1
        `,
        item
      );
      console.log(`Seeded: ${item.product_code} - ${item.product_name}`);
    }

    console.log(`Inventory seeding complete: ${inventoryItems.length} products.`);
    process.exit(0);
  } catch (error) {
    console.error("Inventory seeding failed:", error);
    process.exit(1);
  }
}

seed();
