import { NextResponse } from 'next/server'
import { db } from '@/lib/db'

// POST /api/pos/seed - Seed sample data for testing
export async function POST() {
  try {
    // Create sample clients
    const clients = await Promise.all([
      db.client.create({
        data: {
          nom: 'Dupont',
          prenom: 'Jean',
          email: 'jean.dupont@email.com',
          telephone: '0612345678',
          ville: 'Paris'
        }
      }),
      db.client.create({
        data: {
          nom: 'Martin',
          prenom: 'Marie',
          email: 'marie.martin@email.com',
          telephone: '0623456789',
          ville: 'Lyon'
        }
      }),
      db.client.create({
        data: {
          nom: 'Bernard',
          prenom: 'Pierre',
          telephone: '0634567890',
          ville: 'Marseille'
        }
      })
    ])

    // Create sample products
    const products = await Promise.all([
      // Montures
      db.produit.create({
        data: {
          reference: 'MNT001',
          designation: 'Monture Ray-Ban Aviator',
          categorie: 'MONTURE',
          prixAchatHT: 80,
          prixVenteTTC: 150,
          tva: 20,
          stock: 15,
          stockMin: 5,
          marque: 'Ray-Ban',
          typeMonture: 'COMPLET',
          materiau: 'Métal',
          couleur: 'Or',
          emplacement: 'A1-01'
        }
      }),
      db.produit.create({
        data: {
          reference: 'MNT002',
          designation: 'Monture Oakley Holbrook',
          categorie: 'MONTURE',
          prixAchatHT: 60,
          prixVenteTTC: 120,
          tva: 20,
          stock: 20,
          stockMin: 5,
          marque: 'Oakley',
          typeMonture: 'COMPLET',
          materiau: 'Acétate',
          couleur: 'Noir',
          emplacement: 'A1-02'
        }
      }),
      db.produit.create({
        data: {
          reference: 'MNT003',
          designation: 'Monture Vogue VO5352S',
          categorie: 'MONTURE',
          prixAchatHT: 45,
          prixVenteTTC: 95,
          tva: 20,
          stock: 25,
          stockMin: 5,
          marque: 'Vogue',
          typeMonture: 'NATUREL',
          materiau: 'Acétate',
          couleur: 'Rouge',
          emplacement: 'A1-03'
        }
      }),
      // Verres
      db.produit.create({
        data: {
          reference: 'VRG001',
          designation: 'Verre Simple Vision 1.5',
          categorie: 'VERRE',
          prixAchatHT: 15,
          prixVenteTTC: 45,
          tva: 20,
          stock: 100,
          stockMin: 20,
          typeVerre: 'SIMPLE',
          indice: 1.5,
          emplacement: 'B1-01'
        }
      }),
      db.produit.create({
        data: {
          reference: 'VRG002',
          designation: 'Verre Bifocal 1.5',
          categorie: 'VERRE',
          prixAchatHT: 30,
          prixVenteTTC: 75,
          tva: 20,
          stock: 50,
          stockMin: 10,
          typeVerre: 'BIFOCAL',
          indice: 1.5,
          emplacement: 'B1-02'
        }
      }),
      db.produit.create({
        data: {
          reference: 'VRG003',
          designation: 'Verre Progressif 1.6',
          categorie: 'VERRE',
          prixAchatHT: 60,
          prixVenteTTC: 150,
          tva: 20,
          stock: 30,
          stockMin: 10,
          typeVerre: 'PROGRESSIF',
          indice: 1.6,
          emplacement: 'B1-03'
        }
      }),
      // Lentilles
      db.produit.create({
        data: {
          reference: 'LEN001',
          designation: 'Lentilles Journalières',
          categorie: 'LENTILLE',
          prixAchatHT: 0.20,
          prixVenteTTC: 0.50,
          tva: 20,
          stock: 500,
          stockMin: 100,
          emplacement: 'C1-01'
        }
      }),
      db.produit.create({
        data: {
          reference: 'LEN002',
          designation: 'Lentilles Mensuelles',
          categorie: 'LENTILLE',
          prixAchatHT: 8,
          prixVenteTTC: 20,
          tva: 20,
          stock: 100,
          stockMin: 20,
          emplacement: 'C1-02'
        }
      }),
      // Accessoires
      db.produit.create({
        data: {
          reference: 'ACC001',
          designation: 'Étui Lunettes Luxe',
          categorie: 'ACCESSOIRE',
          prixAchatHT: 10,
          prixVenteTTC: 25,
          tva: 20,
          stock: 40,
          stockMin: 10,
          emplacement: 'D1-01'
        }
      }),
      db.produit.create({
        data: {
          reference: 'ACC002',
          designation: 'Kit Nettoyage',
          categorie: 'ACCESSOIRE',
          prixAchatHT: 3,
          prixVenteTTC: 8,
          tva: 20,
          stock: 80,
          stockMin: 20,
          emplacement: 'D1-02'
        }
      }),
      db.produit.create({
        data: {
          reference: 'ACC003',
          designation: 'Chaînette Lunettes',
          categorie: 'ACCESSOIRE',
          prixAchatHT: 2,
          prixVenteTTC: 6,
          tva: 20,
          stock: 60,
          stockMin: 15,
          emplacement: 'D1-03'
        }
      })
    ])

    return NextResponse.json({
      message: 'Sample data seeded successfully',
      clients: clients.length,
      products: products.length
    })
  } catch (error) {
    console.error('Error seeding data:', error)
    return NextResponse.json(
      { error: 'Failed to seed data' },
      { status: 500 }
    )
  }
}
