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

const suppliers = [
  {
    supplier_code: "SUP-CLEAN-PRO",
    name: "Distribuidora Clean Pro",
    contact_name: "Laura Mendez",
    phone: "(787) 555-2101",
    email: "ordenes@cleanpropr.com",
    category: "Lavado",
    address: "Carr. 2 Km 14.5, Bayamon, PR",
    notes: "Detergentes liquidos, suavizantes y productos de alto consumo.",
    status: "active"
  },
  {
    supplier_code: "SUP-QUIM-CARIBE",
    name: "Quimicos del Caribe",
    contact_name: "Rafael Torres",
    phone: "(787) 555-3308",
    email: "ventas@quimicoscaribe.com",
    category: "Quimicos",
    address: "Zona Industrial Minillas, Bayamon, PR",
    notes: "Detergente en polvo, cloro y productos industriales.",
    status: "active"
  },
  {
    supplier_code: "SUP-LAUNDRY-PR",
    name: "Laundry Supply PR",
    contact_name: "Carla Rivera",
    phone: "(787) 555-7710",
    email: "supply@laundrypr.com",
    category: "Tratamiento",
    address: "Ave. Ponce de Leon 1201, San Juan, PR",
    notes: "Removedores, desengrasantes y productos para manchas.",
    status: "active"
  },
  {
    supplier_code: "SUP-EMPAQUES-UNI",
    name: "Empaques Universal",
    contact_name: "Miguel Arroyo",
    phone: "(787) 555-4902",
    email: "pedidos@empaquesuniversal.com",
    category: "Empaque",
    address: "Carr. 1 Km 23.8, Caguas, PR",
    notes: "Bolsas plasticas, ganchos y materiales de empaque.",
    status: "active"
  },
  {
    supplier_code: "SUP-PAPEL-CENTRAL",
    name: "Papeleria Central",
    contact_name: "Nadia Soto",
    phone: "(787) 555-6114",
    email: "compras@papeleriacentralpr.com",
    category: "Operacion",
    address: "Calle Comercio 45, Guaynabo, PR",
    notes: "Rollos termicos, recibos, etiquetas y papeleria.",
    status: "active"
  },
  {
    supplier_code: "SUP-SASTRERIA",
    name: "Sastreria Supply",
    contact_name: "Jose Medina",
    phone: "(787) 555-8755",
    email: "ventas@sastreriasupply.com",
    category: "Sastreria",
    address: "Calle Luna 88, San Juan, PR",
    notes: "Hilos, cierres, agujas y accesorios de costura.",
    status: "active"
  },
  {
    supplier_code: "SUP-HIGIENE-TOTAL",
    name: "Higiene Total",
    contact_name: "Patricia Colon",
    phone: "(787) 555-1440",
    email: "servicio@higienetotalpr.com",
    category: "Limpieza",
    address: "Ave. Main 302, Carolina, PR",
    notes: "Desinfectantes, guantes, productos de limpieza y seguridad.",
    status: "active"
  },
  {
    supplier_code: "SUP-LABEL-PRO",
    name: "Label Pro Textile",
    contact_name: "Victor Cruz",
    phone: "(787) 555-9012",
    email: "labels@labelprotextile.com",
    category: "Etiquetas",
    address: "Zona Industrial Amelia, Guaynabo, PR",
    notes: "Etiquetas resistentes al lavado, barcode y consumibles para impresoras.",
    status: "active"
  },
  {
    supplier_code: "SUP-MAQUINAS-PR",
    name: "Maquinas Laundry PR",
    contact_name: "Esteban Rios",
    phone: "(787) 555-7003",
    email: "soporte@maquinaslaundrypr.com",
    category: "Mantenimiento",
    address: "Carr. 167 Km 9.2, Toa Baja, PR",
    notes: "Servicio tecnico, piezas y mantenimiento preventivo.",
    status: "active"
  },
  {
    supplier_code: "SUP-UNIFORMES-CARIBE",
    name: "Uniformes Caribe",
    contact_name: "Maribel Figueroa",
    phone: "(787) 555-2230",
    email: "cotizaciones@uniformescaribe.com",
    category: "Uniformes",
    address: "Plaza Industrial Local 6, Caguas, PR",
    notes: "Uniformes del personal, delantales y camisas bordadas.",
    status: "pending"
  },
  {
    supplier_code: "SUP-ATH-POS",
    name: "ATH POS Services",
    contact_name: "Soporte Comercial",
    phone: "(787) 555-8181",
    email: "comercios@athposservices.com",
    category: "Pagos",
    address: "Hato Rey, San Juan, PR",
    notes: "Terminales de pago, ATH Movil Business y conciliacion.",
    status: "active"
  },
  {
    supplier_code: "SUP-TRANS-EXPRESS",
    name: "Transporte Express PR",
    contact_name: "Omar Santiago",
    phone: "(787) 555-6609",
    email: "rutas@transporteexpresspr.com",
    category: "Logistica",
    address: "Ave. Kennedy 500, San Juan, PR",
    notes: "Apoyo en rutas de pickup y delivery cuando hay alto volumen.",
    status: "inactive"
  }
];

async function ensureSuppliersTable() {
  await pool.query(`
    CREATE TABLE IF NOT EXISTS suppliers (
      id INT AUTO_INCREMENT PRIMARY KEY,
      supplier_code VARCHAR(50) NOT NULL UNIQUE,
      name VARCHAR(160) NOT NULL,
      contact_name VARCHAR(120) NULL,
      phone VARCHAR(40) NULL,
      email VARCHAR(120) NULL,
      category VARCHAR(100) NOT NULL DEFAULT 'General',
      address VARCHAR(255) NULL,
      notes TEXT NULL,
      status VARCHAR(20) NOT NULL DEFAULT 'active',
      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 ensureSuppliersTable();
    console.log("Seeding suppliers...");

    for (const supplier of suppliers) {
      await pool.query(
        `
        INSERT INTO suppliers
          (supplier_code, name, contact_name, phone, email, category, address, notes, status, created_at)
        VALUES
          (:supplier_code, :name, :contact_name, :phone, :email, :category, :address, :notes, :status, NOW())
        ON DUPLICATE KEY UPDATE
          name = VALUES(name),
          contact_name = VALUES(contact_name),
          phone = VALUES(phone),
          email = VALUES(email),
          category = VALUES(category),
          address = VALUES(address),
          notes = VALUES(notes),
          status = VALUES(status)
        `,
        supplier
      );
      console.log(`Seeded: ${supplier.supplier_code} - ${supplier.name}`);
    }

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

seed();
