import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcryptjs'

const prisma = new PrismaClient()

async function main() {
  console.log('🌱 Starting seed...')

  // Create default users
  const adminPassword = await bcrypt.hash('admin123', 12)
  const editorPassword = await bcrypt.hash('editor123', 12)
  const viewerPassword = await bcrypt.hash('viewer123', 12)

  const admin = await prisma.user.upsert({
    where: { email: 'admin@lageplan.local' },
    update: {},
    create: {
      email: 'admin@lageplan.local',
      name: 'Administrator',
      password: adminPassword,
      role: 'SERVER_ADMIN',
    },
  })

  const editor = await prisma.user.upsert({
    where: { email: 'editor@lageplan.local' },
    update: {},
    create: {
      email: 'editor@lageplan.local',
      name: 'Einsatzleiter',
      password: editorPassword,
      role: 'TENANT_ADMIN',
    },
  })

  const viewer = await prisma.user.upsert({
    where: { email: 'viewer@lageplan.local' },
    update: {},
    create: {
      email: 'viewer@lageplan.local',
      name: 'Beobachter',
      password: viewerPassword,
      role: 'OPERATOR',
    },
  })

  console.log('✅ Users created:', { admin: admin.email, editor: editor.email, viewer: viewer.email })

  // Create icon categories with stable IDs (same logic as seed.js)
  const categories = [
    { id: 'feuer', name: 'Feuer', description: 'Brand- und Feuersymbole', sortOrder: 1 },
    { id: 'wasser', name: 'Wasser', description: 'Wasser- und Überflutungssymbole', sortOrder: 2 },
    { id: 'gefahrstoffe', name: 'Gefahrstoffe', description: 'Chemie- und Gefahrstoffsymbole', sortOrder: 3 },
    { id: 'verkehr', name: 'Verkehr', description: 'Verkehrs- und Unfallsymbole', sortOrder: 4 },
    { id: 'personen', name: 'Personen', description: 'Personen- und Rettungssymbole', sortOrder: 5 },
    { id: 'fahrzeuge', name: 'Fahrzeuge', description: 'Einsatzfahrzeuge und Geräte', sortOrder: 6 },
    { id: 'infrastruktur', name: 'Infrastruktur', description: 'Gebäude und Infrastruktursymbole', sortOrder: 7 },
    { id: 'taktik', name: 'Taktik', description: 'Taktische Zeichen und Symbole', sortOrder: 8 },
    { id: 'eigene', name: 'Eigene', description: 'Benutzerdefinierte Symbole', sortOrder: 99 },
  ]

  for (const cat of categories) {
    await prisma.iconCategory.upsert({
      where: { id: cat.id },
      update: { description: cat.description, sortOrder: cat.sortOrder },
      create: cat,
    })
  }

  console.log('✅ Icon categories created:', categories.length)

  // Get category IDs for system icons
  const feuerCat = await prisma.iconCategory.findUnique({ where: { id: 'feuer' } })
  const wasserCat = await prisma.iconCategory.findUnique({ where: { id: 'wasser' } })
  const gefahrstoffeCat = await prisma.iconCategory.findUnique({ where: { id: 'gefahrstoffe' } })
  const verkehrCat = await prisma.iconCategory.findUnique({ where: { id: 'verkehr' } })
  const personenCat = await prisma.iconCategory.findUnique({ where: { id: 'personen' } })
  const fahrzeugeCat = await prisma.iconCategory.findUnique({ where: { id: 'fahrzeuge' } })
  const infrastrukturCat = await prisma.iconCategory.findUnique({ where: { id: 'infrastruktur' } })
  const taktikCat = await prisma.iconCategory.findUnique({ where: { id: 'taktik' } })

  // Create system icons (these use inline SVG data URIs - in production, upload to MinIO)
  const systemIcons = [
    { name: 'Brand', categoryId: feuerCat!.id, fileKey: 'system/fire.svg' },
    { name: 'Rauch', categoryId: feuerCat!.id, fileKey: 'system/smoke.svg' },
    { name: 'Explosion', categoryId: feuerCat!.id, fileKey: 'system/explosion.svg' },
    { name: 'Brandstelle', categoryId: feuerCat!.id, fileKey: 'system/fire-location.svg' },
    { name: 'Überflutung', categoryId: wasserCat!.id, fileKey: 'system/flood.svg' },
    { name: 'Wasserschaden', categoryId: wasserCat!.id, fileKey: 'system/water-damage.svg' },
    { name: 'Hydrant', categoryId: wasserCat!.id, fileKey: 'system/hydrant.svg' },
    { name: 'Gefahrstoff', categoryId: gefahrstoffeCat!.id, fileKey: 'system/hazmat.svg' },
    { name: 'Radioaktiv', categoryId: gefahrstoffeCat!.id, fileKey: 'system/radioactive.svg' },
    { name: 'Giftig', categoryId: gefahrstoffeCat!.id, fileKey: 'system/toxic.svg' },
    { name: 'Unfall', categoryId: verkehrCat!.id, fileKey: 'system/accident.svg' },
    { name: 'Strassensperre', categoryId: verkehrCat!.id, fileKey: 'system/road-block.svg' },
    { name: 'Verletzte Person', categoryId: personenCat!.id, fileKey: 'system/injured.svg' },
    { name: 'Vermisste Person', categoryId: personenCat!.id, fileKey: 'system/missing.svg' },
    { name: 'Sammelplatz', categoryId: personenCat!.id, fileKey: 'system/assembly-point.svg' },
    { name: 'Löschfahrzeug', categoryId: fahrzeugeCat!.id, fileKey: 'system/fire-truck.svg' },
    { name: 'Rettungswagen', categoryId: fahrzeugeCat!.id, fileKey: 'system/ambulance.svg' },
    { name: 'Kommandoposten', categoryId: taktikCat!.id, fileKey: 'system/command-post.svg' },
    { name: 'Einsatzleitung', categoryId: taktikCat!.id, fileKey: 'system/incident-command.svg' },
    { name: 'Absperrung', categoryId: taktikCat!.id, fileKey: 'system/barrier.svg' },
    { name: 'Gebäude', categoryId: infrastrukturCat!.id, fileKey: 'system/building.svg' },
    { name: 'Eingang', categoryId: infrastrukturCat!.id, fileKey: 'system/entrance.svg' },
  ]

  for (const icon of systemIcons) {
    const existing = await prisma.iconAsset.findFirst({
      where: { fileKey: icon.fileKey },
    })
    if (!existing) {
      await prisma.iconAsset.create({
        data: {
          name: icon.name,
          categoryId: icon.categoryId,
          fileKey: icon.fileKey,
          mimeType: 'image/svg+xml',
          isSystem: true,
          width: 48,
          height: 48,
        },
      })
    }
  }

  console.log('✅ System icons created:', systemIcons.length)

  // Create a demo project
  const demoProject = await prisma.project.upsert({
    where: { id: 'demo-project-001' },
    update: {},
    create: {
      id: 'demo-project-001',
      title: 'Demo Einsatz - Wohnungsbrand',
      location: 'Musterstrasse 42, 8000 Zürich',
      description: 'Beispiel-Einsatz zur Demonstration der Funktionen',
      ownerId: editor.id,
      mapCenter: { lng: 8.5417, lat: 47.3769 },
      mapZoom: 17,
    },
  })

  console.log('✅ Demo project created:', demoProject.title)

  console.log('🎉 Seed completed successfully!')
}

main()
  .catch((e) => {
    console.error('❌ Seed failed:', e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })
