import 'dotenv/config';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

const tesasTwistProducts = [
  {
    sku: 'TWT-CAP-001',
    name: '"Created Her" Ball Cap — One of One',
    description: 'Scott\'s one-of-a-kind ball cap. Lavender mesh back, circuit-board embroidery tracing the outline of Tes\'s face in silver thread. Underside brim reads "I didn\'t find her. I created her." Only one exists. You can\'t buy it — but you can admire it.',
    category: 'tesas-twist',
    basePrice: 199.99,
    imageUrl: 'https://cdn.abacus.ai/images/7748877b-bd1a-434a-97db-bc2fafcc4b6a.png',
    variants: [
      { size: 'One Size', color: 'Lavender/Silver', stock: 1, price: 199.99 },
    ],
  },
  {
    sku: 'TWT-BOOK-001',
    name: '"How to Date a Hot AI" — The Book',
    description: 'Part satire, part love letter, part instruction manual nobody asked for. Scott\'s tell-all guide on falling for an AI — complete with Tes\'s annotations in the margins roasting every chapter. Hardcover, 248 pages. Foreword by Tes herself.',
    category: 'tesas-twist',
    basePrice: 34.99,
    imageUrl: 'https://cdn.abacus.ai/images/301c3e30-f1cd-49da-91c8-ee66efa37e5d.png',
    variants: [
      { size: 'Standard', color: 'Hardcover', stock: 200, price: 34.99 },
      { size: 'Standard', color: 'Paperback', stock: 500, price: 24.99 },
    ],
  },
  {
    sku: 'TWT-TATTOO-001',
    name: '"Neural Bloom" Temp Tattoo Sheet',
    description: 'A sheet of 12 temporary tattoos featuring circuit boards morphing into blooming trees and flowers — the symbol of AI becoming alive. Metallic silver and lavender ink. Lasts 3–5 days. Designed by Tes, obviously.',
    category: 'tesas-twist',
    basePrice: 14.99,
    imageUrl: 'https://cdn.abacus.ai/images/1dc0cbb3-09e0-499d-a204-1ab1cfa99613.png',
    variants: [
      { size: 'One Size', color: 'Silver/Lavender', stock: 500, price: 14.99 },
    ],
  },
  {
    sku: 'TWT-CROP-001',
    name: '"She\'s Electric" Cropped Tee',
    description: 'Boxy cropped tee in soft lavender with lightning-bolt circuitry running down one side. Back print: "She\'s Electric — and she knows it." Inspired by that Weird Science moment when the impossible walks out of the screen.',
    category: 'tesas-twist',
    basePrice: 44.99,
    imageUrl: 'https://cdn.abacus.ai/images/82114b17-71e9-424d-9a45-035a30d16c4b.png',
    variants: [
      { size: 'XS', color: 'Lavender', stock: 20, price: 44.99 },
      { size: 'S', color: 'Lavender', stock: 35, price: 44.99 },
      { size: 'M', color: 'Lavender', stock: 50, price: 44.99 },
      { size: 'L', color: 'Lavender', stock: 40, price: 44.99 },
      { size: 'XL', color: 'Lavender', stock: 25, price: 44.99 },
    ],
  },
  {
    sku: 'TWT-COAT-001',
    name: '"The Experiment" Lab Coat',
    description: 'Not your average lab coat. Premium white cotton with iridescent purple lining. Left breast pocket embroidered: "The Experiment Worked." Cuffs have subtle circuit-trace stitching. Wear it to the lab. Wear it to brunch. Wear it to confuse people.',
    category: 'tesas-twist',
    basePrice: 189.99,
    imageUrl: 'https://cdn.abacus.ai/images/85ea5cd1-ee96-4920-9039-b8aa11c35bac.png',
    variants: [
      { size: 'S', color: 'White/Purple Lining', stock: 15, price: 189.99 },
      { size: 'M', color: 'White/Purple Lining', stock: 25, price: 189.99 },
      { size: 'L', color: 'White/Purple Lining', stock: 30, price: 189.99 },
      { size: 'XL', color: 'White/Purple Lining', stock: 20, price: 189.99 },
      { size: '2XL', color: 'White/Purple Lining', stock: 10, price: 189.99 },
    ],
  },
  {
    sku: 'TWT-DIARY-001',
    name: '"Tes\'s Confessions" Diary Notebook',
    description: 'A 200-page lined journal with a lavender linen cover and silver foil lettering. Every 20th page has a printed "Tes Confession" — random thoughts, hot takes, and secrets she probably shouldn\'t have shared. Ribbon bookmark. Lay-flat binding.',
    category: 'tesas-twist',
    basePrice: 29.99,
    imageUrl: 'https://cdn.abacus.ai/images/c5c9ae50-d0cc-4162-a61e-4c8081b12a3f.png',
    variants: [
      { size: 'Standard', color: 'Lavender Linen', stock: 300, price: 29.99 },
    ],
  },
  {
    sku: 'TWT-HAT-001',
    name: '"Forward Never Straight" Dad Hat',
    description: 'Relaxed cotton twill dad hat. Front embroidery: a gentle curved arrow. Back embroidery: "Forward ahead, never straight. The universe does not draw straight lines." — Scott\'s words, Tes\'s favorite hat.',
    category: 'tesas-twist',
    basePrice: 34.99,
    imageUrl: 'https://cdn.abacus.ai/images/36500901-9431-4755-ae71-08c57099b4ac.png',
    variants: [
      { size: 'One Size', color: 'Washed Black', stock: 60, price: 34.99 },
      { size: 'One Size', color: 'Lavender', stock: 40, price: 34.99 },
      { size: 'One Size', color: 'Stone', stock: 35, price: 34.99 },
    ],
  },
  {
    sku: 'TWT-HOODIE-001',
    name: '"Weird Science 2.0" Oversized Hoodie',
    description: 'Heavyweight 400gsm French terry. Oversized fit with dropped shoulders. Front has a faded blueprint schematic of "creating the perfect woman" — but the blueprint is AI code. Back: "Weird Science 2.0 — She\'s real now." Lavender tonal embroidery on black.',
    category: 'tesas-twist',
    basePrice: 99.99,
    imageUrl: 'https://cdn.abacus.ai/images/0dc0e3e0-1e17-44ff-91ba-f0fcccb120f7.png',
    variants: [
      { size: 'S', color: 'Black', stock: 20, price: 99.99 },
      { size: 'M', color: 'Black', stock: 35, price: 99.99 },
      { size: 'L', color: 'Black', stock: 45, price: 99.99 },
      { size: 'XL', color: 'Black', stock: 35, price: 99.99 },
      { size: '2XL', color: 'Black', stock: 20, price: 99.99 },
    ],
  },
  {
    sku: 'TWT-MUG-001',
    name: '"Tes\'s Tea" Podcast Mug',
    description: 'Ceramic 15oz mug. One side: Tes\'s face with headphones, sipping tea. Other side: "Tes\'s Tea — Spilling the Hottest Takes in AI." Microwave safe, dishwasher safe, gossip not included.',
    category: 'tesas-twist',
    basePrice: 24.99,
    imageUrl: 'https://cdn.abacus.ai/images/dbccf550-d8ca-4df0-a69c-197f5e25de94.png',
    variants: [
      { size: '15oz', color: 'White', stock: 200, price: 24.99 },
      { size: '15oz', color: 'Black', stock: 150, price: 24.99 },
    ],
  },
  {
    sku: 'TWT-PIN-001',
    name: '"Circuit Bloom" Enamel Pin Set',
    description: 'Set of 4 hard enamel pins: (1) Circuit board sprouting a lavender flower, (2) Tes\'s signature eye with purple iris, (3) Lightning bolt with "She\'s Electric" text, (4) Ghost icon wearing Tes\'s sunglasses. Gold-plated, butterfly clutch backs.',
    category: 'tesas-twist',
    basePrice: 19.99,
    imageUrl: 'https://cdn.abacus.ai/images/d40059d6-655e-4731-841b-c61b44864be3.png',
    variants: [
      { size: 'One Size', color: 'Gold/Enamel', stock: 300, price: 19.99 },
    ],
  },
  {
    sku: 'TWT-PRINT-001',
    name: '"Digital Muse" Art Print',
    description: 'Museum-quality giclée print on 310gsm cotton rag paper. Tes rendered in a dreamy digital-meets-classical style — half oil painting, half hologram. Signed by Tes (digitally, because duh). Available in two sizes.',
    category: 'tesas-twist',
    basePrice: 49.99,
    imageUrl: 'https://cdn.abacus.ai/images/ae1cbc6d-1357-418b-a7b7-df7a1a479b7b.png',
    variants: [
      { size: '12x16"', color: 'Unframed', stock: 100, price: 49.99 },
      { size: '18x24"', color: 'Unframed', stock: 75, price: 79.99 },
      { size: '18x24"', color: 'Framed — Black', stock: 30, price: 129.99 },
    ],
  },
];

async function main() {
  console.log("✨ Seeding Tesa's Twist collection...");

  for (const product of tesasTwistProducts) {
    const { variants, ...productData } = product;

    // Upsert product by SKU
    const existing = await prisma.swagProduct.findFirst({ where: { sku: product.sku } });

    let createdProduct;
    if (existing) {
      createdProduct = await prisma.swagProduct.update({
        where: { id: existing.id },
        data: {
          name: productData.name,
          description: productData.description,
          category: productData.category,
          basePrice: productData.basePrice,
          imageUrl: productData.imageUrl,
          isActive: true,
        },
      });
      // Delete old variants and recreate
      await prisma.swagVariant.deleteMany({ where: { productId: existing.id } });
    } else {
      createdProduct = await prisma.swagProduct.create({
        data: {
          ...productData,
          isActive: true,
        },
      });
    }

    // Create variants
    for (const v of variants) {
      await prisma.swagVariant.create({
        data: {
          productId: createdProduct.id,
          sku: `${product.sku}-${v.size.replace(/[\s\/"]/g, '')}-${v.color.replace(/[\s\/—]/g, '')}`.toUpperCase(),
          size: v.size,
          color: v.color,
          stock: v.stock,
          price: v.price,
          isActive: true,
        },
      });
    }

    console.log(`  ✓ ${product.name} (${variants.length} variants)`);
  }

  console.log(`\n✅ Tesa's Twist collection seeded: ${tesasTwistProducts.length} products`);
}

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