import dotenv from 'dotenv';
dotenv.config();

import mongoose from 'mongoose';
import Admin from '../models/Admin.js';
import Product from '../models/Product.js';
import StoreSettings from '../models/StoreSettings.js';

const sampleProducts = [
  {
    name: 'USB-C Cable 1m', category: 'Electronics', purchasePrice: 80, sellingPrice: 150, currentStock: 50, lowStockThreshold: 10, trackingType: 'QUANTITY', unit: 'pcs',
    variants: [{ variantId: 'v-usb-c-standard', label: 'Standard', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 80, sellingPrice: 150, currentStock: 50, lowStockThreshold: 10 }],
  },
  {
    name: 'Wireless Mouse', category: 'Electronics', purchasePrice: 250, sellingPrice: 450, currentStock: 30, lowStockThreshold: 5, trackingType: 'QUANTITY', unit: 'pcs',
    variants: [{ variantId: 'v-mouse-standard', label: 'Standard', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 250, sellingPrice: 450, currentStock: 30, lowStockThreshold: 5 }],
  },
  {
    name: 'Basmati Rice 5kg', category: 'Groceries', purchasePrice: 350, sellingPrice: 450, currentStock: 25, lowStockThreshold: 10, trackingType: 'WEIGHT', unit: 'pack',
    variants: [{ variantId: 'v-rice-5kg', label: '5 kg', measurementValue: 5, measurementUnit: 'kg', sellingUnit: 'pack', purchasePrice: 350, sellingPrice: 450, currentStock: 25, lowStockThreshold: 10 }],
  },
  {
    name: 'Sunflower Oil 1L', category: 'Groceries', purchasePrice: 120, sellingPrice: 160, currentStock: 40, lowStockThreshold: 8, trackingType: 'VOLUME', unit: 'bottle',
    variants: [{ variantId: 'v-oil-1l', label: '1 L', measurementValue: 1, measurementUnit: 'L', sellingUnit: 'bottle', purchasePrice: 120, sellingPrice: 160, currentStock: 40, lowStockThreshold: 8 }],
  },
  {
    name: 'A4 Paper Ream', category: 'Stationery', purchasePrice: 180, sellingPrice: 280, currentStock: 15, lowStockThreshold: 5, trackingType: 'QUANTITY', unit: 'pack',
    variants: [{ variantId: 'v-a4-standard', label: 'Standard', measurementValue: null, measurementUnit: null, sellingUnit: 'pack', purchasePrice: 180, sellingPrice: 280, currentStock: 15, lowStockThreshold: 5 }],
  },
  {
    name: 'Gel Pen Pack (10)', category: 'Stationery', purchasePrice: 90, sellingPrice: 150, currentStock: 20, lowStockThreshold: 10, trackingType: 'QUANTITY', unit: 'pack',
    variants: [{ variantId: 'v-gelpen-standard', label: 'Standard', measurementValue: null, measurementUnit: null, sellingUnit: 'pack', purchasePrice: 90, sellingPrice: 150, currentStock: 20, lowStockThreshold: 10 }],
  },
  {
    name: 'Cotton T-Shirt', category: 'Clothing', purchasePrice: 200, sellingPrice: 400, currentStock: 52, lowStockThreshold: 5, trackingType: 'SIZE', sizeSystem: 'CLOTHING',
    sizeVariants: [
      { size: 'S', stock: 10, lowStockThreshold: 3 },
      { size: 'M', stock: 20, lowStockThreshold: 5 },
      { size: 'L', stock: 15, lowStockThreshold: 5 },
      { size: 'XL', stock: 7, lowStockThreshold: 3 },
    ],
    variants: [
      { variantId: 'v-tshirt-s', label: 'S', size: 'S', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 200, sellingPrice: 400, currentStock: 10, lowStockThreshold: 3 },
      { variantId: 'v-tshirt-m', label: 'M', size: 'M', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 200, sellingPrice: 400, currentStock: 20, lowStockThreshold: 5 },
      { variantId: 'v-tshirt-l', label: 'L', size: 'L', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 200, sellingPrice: 400, currentStock: 15, lowStockThreshold: 5 },
      { variantId: 'v-tshirt-xl', label: 'XL', size: 'XL', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 200, sellingPrice: 400, currentStock: 7, lowStockThreshold: 3 },
    ],
  },
  {
    name: 'Steel Water Bottle', category: 'Home & Kitchen', purchasePrice: 150, sellingPrice: 300, currentStock: 3, lowStockThreshold: 5, trackingType: 'QUANTITY', unit: 'pcs',
    variants: [{ variantId: 'v-bottle-standard', label: 'Standard', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 150, sellingPrice: 300, currentStock: 3, lowStockThreshold: 5 }],
  },
  {
    name: 'Non-Stick Frying Pan', category: 'Home & Kitchen', purchasePrice: 400, sellingPrice: 700, currentStock: 8, lowStockThreshold: 3, trackingType: 'QUANTITY', unit: 'pcs',
    variants: [{ variantId: 'v-pan-standard', label: 'Standard', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 400, sellingPrice: 700, currentStock: 8, lowStockThreshold: 3 }],
  },
  {
    name: 'Notebook 200 pages', category: 'Stationery', purchasePrice: 40, sellingPrice: 80, currentStock: 60, lowStockThreshold: 15, trackingType: 'QUANTITY', unit: 'pcs',
    variants: [{ variantId: 'v-notebook-standard', label: 'Standard', measurementValue: null, measurementUnit: null, sellingUnit: 'pcs', purchasePrice: 40, sellingPrice: 80, currentStock: 60, lowStockThreshold: 15 }],
  },
];

const seed = async () => {
  try {
    await mongoose.connect(process.env.MONGODB_URI);
    console.log('Connected to MongoDB');

    await Admin.deleteMany({});
    await Product.deleteMany({});
    await StoreSettings.deleteMany({});

    const admin = await Admin.create({
      name: 'Admin',
      email: process.env.SEED_ADMIN_EMAIL || 'admin@store.com',
      password: process.env.SEED_ADMIN_PASSWORD || 'admin123',
      role: 'ADMIN',
    });
    console.log(`Admin created: ${admin.email}`);

    const products = await Product.insertMany(sampleProducts);
    console.log(`${products.length} products created`);

    const settings = await StoreSettings.create({
      storeName: 'My Store',
      storeAddress: '123 Main Street, City, Country',
      contactDetails: '+91 98765 43210',
    });
    console.log('Store settings created');

    await mongoose.disconnect();
    console.log('Seed completed successfully!');
    process.exit(0);
  } catch (error) {
    console.error('Seed failed:', error.message);
    await mongoose.disconnect();
    process.exit(1);
  }
};

seed();
