/**
 * Seed SVG icons and categories ONLY — does NOT touch tenants, users, or projects.
 * Used when upgrading an existing database to new SVG icons.
 */
const { PrismaClient } = require('@prisma/client')
const fs = require('fs')
const path = require('path')

const prisma = new PrismaClient()

async function main() {
  console.log('🎨 Seeding SVG icons only...')

  const catDefs = [
    { name: 'Feuer / Brand', description: 'Feuer, Brand, Brandschutz', sortOrder: 1,
      patterns: ['Feuer', 'Brand', 'Explosion', 'Entrauchung', 'Rauch', 'Kamin', 'Luefter', 'Sprinkler'] },
    { name: 'Wasser', description: 'Wasser, Hydranten, Leitungen', sortOrder: 2,
      patterns: ['Wasser', 'Hydrant', 'Reservoir', 'Druckleitung', 'Druckleistung', 'Transportleitung',
                 'Schieber', 'Wasserloeschposten', 'Wasserversorgung', 'Wasserleitung', 'Ueberschwemmung',
                 'Offener_Wasserverlauf', 'Moeglicher_Wasserbezugsort', 'Stehendes_Gewaesser',
                 'Kleinloeschgeraet', 'Gefahr_durch_Loeschen_mit_Wasser'] },
    { name: 'Gefahren / Stoffe', description: 'Gefahrstoffe, Chemie, ABC', sortOrder: 3,
      patterns: ['Gefaehrlich', 'Chemie', 'Chemikalien', 'Biologisch', 'Radioaktiv', 'Gas', 'Elektri',
                 'Elektrotableau', 'Achtung', 'Leitungsdaehte'] },
    { name: 'Rettung / Personen', description: 'Rettung, Sanität, Personen', sortOrder: 4,
      patterns: ['Rettung', 'Retten', 'Sanitaet', 'Patienten', 'Sammelplatz', 'Sammelstelle',
                 'Totensammelstelle', 'Sprungretter', 'Helikopterlandeplatz'] },
    { name: 'Leitern / Geräte', description: 'Leitern, Fahrzeuge, Geräte', sortOrder: 5,
      patterns: ['Leiter', 'Anhaengeleiter', 'Strebenleiter', 'ADL', 'TLF', 'HRF', 'SWHP'] },
    { name: 'Gebäude / Schäden', description: 'Gebäude, Zerstörung, Schäden', sortOrder: 6,
      patterns: ['Beschaedigung', 'Teilzerstoerung', 'Totalzerstoerung', 'Decke_eingestuerzt',
                 'Umfassungswaende', 'AnzahlGeschosse', 'Eingang', 'Treppen', 'Lift', 'Bruecke',
                 'Rutschgebiet', 'Strasse', 'Bahnlin'] },
    { name: 'Einsatzführung', description: 'Führung, Organisation, Kommunikation', sortOrder: 7,
      patterns: ['Einsatzleiter', 'KP_Font', 'Offizier', 'Beobachtungsposten', 'Kontrollstelle',
                 'Funk', 'Informationszentrum', 'Medienkontaktstelle', 'Fernsignaltableau',
                 'Materialdepot', 'Schluesseldepot', 'Abschnitt', 'Absperrung'] },
    { name: 'Organisationen', description: 'Feuerwehr, Polizei, Armee, Zivilschutz', sortOrder: 8,
      patterns: ['Feuerwehr', 'Polizei', 'Armee', 'Zivilschutz', 'Chemiewehr', 'MS_de', 'Anmarsch'] },
    { name: 'Entwicklung / Taktik', description: 'Entwicklungsgrenzen, Ausbreitung', sortOrder: 9,
      patterns: ['Entwicklungsgrenze', 'Horizontale_Entwicklung', 'Vertikale_Entwicklung',
                 'VertikaleEntwicklung', 'Unfall'] },
    { name: 'Verschiedenes', description: 'Weitere Signaturen', sortOrder: 10,
      patterns: ['Massstab', 'Nordrichtung', 'Windrichtung'] },
  ]

  // NOTE: We intentionally do NOT delete old system icons here.
  // TenantSymbol rows reference IconAsset.id via foreign key.
  // Deleting would either break references (tenant symbols become 404s)
  // or cascade-delete tenant symbols. Instead we upsert by fileKey.

  // Upsert global categories (preserves tenant categories)
  const catMap = {}
  for (const def of catDefs) {
    const catId = def.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase()
    const cat = await prisma.iconCategory.upsert({
      where: { id: catId },
      update: { name: def.name, description: def.description, sortOrder: def.sortOrder, isGlobal: true },
      create: {
        id: catId,
        name: def.name,
        description: def.description,
        sortOrder: def.sortOrder,
        isGlobal: true,
        tenantId: null,
      },
    })
    catMap[def.name] = cat
  }
  console.log(`✅ ${Object.keys(catMap).length} icon categories upserted`)

  function findCategory(filename) {
    for (const def of catDefs) {
      for (const pattern of def.patterns) {
        if (filename.includes(pattern)) return catMap[def.name]
      }
    }
    return catMap['Verschiedenes']
  }

  // Load SVG icons from public/signaturen/
  const signaturenDir = path.join(process.cwd(), 'public', 'signaturen')
  let svgFiles = []
  try {
    svgFiles = fs.readdirSync(signaturenDir).filter(f => f.endsWith('.svg')).sort()
  } catch (e) {
    console.warn('⚠️ public/signaturen/ not found')
    return
  }

  let created = 0
  let updated = 0
  for (const file of svgFiles) {
    let name = file.replace('.svg', '')
    name = name.replace(/_de$/i, '').replace(/_DE$/i, '').replace(/-de$/i, '')
    name = name.replace(/[_-]/g, ' ').replace(/\s+/g, ' ').trim()

    const fileKey = `signaturen/${file}`
    const category = findCategory(file)

    const existing = await prisma.iconAsset.findFirst({ where: { fileKey } })
    if (existing) {
      await prisma.iconAsset.update({
        where: { id: existing.id },
        data: {
          name,
          categoryId: category.id,
          mimeType: 'image/svg+xml',
          isSystem: true,
          isActive: true,
          width: 48,
          height: 48,
        },
      })
      updated++
    } else {
      await prisma.iconAsset.create({
        data: {
          name,
          categoryId: category.id,
          fileKey,
          mimeType: 'image/svg+xml',
          isSystem: true,
          width: 48,
          height: 48,
        },
      })
      created++
    }
  }

  console.log(`✅ FKS Signaturen: ${created} created, ${updated} updated (${svgFiles.length} total)`)
}

main()
  .then(async () => { await prisma.$disconnect() })
  .catch(async (e) => {
    console.error(e)
    await prisma.$disconnect()
    process.exit(1)
  })
