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

const prisma = new PrismaClient();

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

  // Create admin user
  const adminPasswordHash = await bcrypt.hash('novinet01', 10);
  const admin = await prisma.user.upsert({
    where: { email: 'admin@znakovni.hr' },
    update: {
      passwordHash: adminPasswordHash, // Update password if user exists
    },
    create: {
      email: 'admin@znakovni.hr',
      displayName: 'Administrator',
      passwordHash: adminPasswordHash,
      role: 'ADMIN',
      isActive: true,
      authProvider: 'local',
    },
  });

  console.log('✅ Created admin user:', admin.email);
  console.log('   Email: admin@znakovni.hr');
  console.log('   Password: novinet01');

  // Create a demo regular user
  const demoPasswordHash = await bcrypt.hash('demo123', 10);
  const demoUser = await prisma.user.upsert({
    where: { email: 'demo@znakovni.hr' },
    update: {},
    create: {
      email: 'demo@znakovni.hr',
      displayName: 'Demo User',
      passwordHash: demoPasswordHash,
      role: 'USER',
      isActive: true,
      authProvider: 'local',
    },
  });

  console.log('✅ Created demo user:', demoUser.email);
  console.log('   Email: demo@znakovni.hr');
  console.log('   Password: demo123');

  // Create sample terms
  console.log('🌱 Creating sample terms...');

  const sampleTerms = [
    {
      wordText: 'Dobar dan',
      normalizedText: 'dobar dan',
      wordType: 'PHRASE',
      cefrLevel: 'A1',
      shortDescription: 'Pozdrav koji se koristi tijekom dana',
      tags: JSON.stringify(['pozdrav', 'osnovno']),
    },
    {
      wordText: 'Hvala',
      normalizedText: 'hvala',
      wordType: 'INTERJECTION',
      cefrLevel: 'A1',
      shortDescription: 'Izraz zahvalnosti',
      tags: JSON.stringify(['zahvala', 'osnovno']),
    },
    {
      wordText: 'Molim',
      normalizedText: 'molim',
      wordType: 'INTERJECTION',
      cefrLevel: 'A1',
      shortDescription: 'Izraz ljubaznosti pri traženju nečega',
      tags: JSON.stringify(['ljubaznost', 'osnovno']),
    },
    {
      wordText: 'Kuća',
      normalizedText: 'kuca',
      wordType: 'NOUN',
      cefrLevel: 'A1',
      shortDescription: 'Zgrada u kojoj ljudi žive',
      tags: JSON.stringify(['dom', 'zgrada']),
    },
    {
      wordText: 'Škola',
      normalizedText: 'skola',
      wordType: 'NOUN',
      cefrLevel: 'A1',
      shortDescription: 'Ustanova za obrazovanje',
      tags: JSON.stringify(['obrazovanje', 'ustanova']),
    },
    {
      wordText: 'Učiti',
      normalizedText: 'uciti',
      wordType: 'VERB',
      cefrLevel: 'A1',
      shortDescription: 'Stjecati znanje ili vještine',
      tags: JSON.stringify(['obrazovanje', 'aktivnost']),
    },
    {
      wordText: 'Jesti',
      normalizedText: 'jesti',
      wordType: 'VERB',
      cefrLevel: 'A1',
      shortDescription: 'Uzimati hranu',
      tags: JSON.stringify(['hrana', 'aktivnost']),
    },
    {
      wordText: 'Piti',
      normalizedText: 'piti',
      wordType: 'VERB',
      cefrLevel: 'A1',
      shortDescription: 'Uzimati tekućinu',
      tags: JSON.stringify(['piće', 'aktivnost']),
    },
    {
      wordText: 'Voda',
      normalizedText: 'voda',
      wordType: 'NOUN',
      cefrLevel: 'A1',
      shortDescription: 'Bezbojna tekućina neophodna za život',
      tags: JSON.stringify(['piće', 'osnovno']),
    },
    {
      wordText: 'Lijep',
      normalizedText: 'lijep',
      wordType: 'ADJECTIVE',
      cefrLevel: 'A1',
      shortDescription: 'Privlačan, ugodan za gledanje',
      tags: JSON.stringify(['opis', 'pozitivno']),
    },
  ];

  for (const termData of sampleTerms) {
    // Check if term already exists
    const existing = await prisma.term.findFirst({
      where: {
        normalizedText: termData.normalizedText,
      },
    });

    if (!existing) {
      const term = await prisma.term.create({
        data: termData,
      });
      console.log(`  ✅ Created term: ${term.wordText}`);
    } else {
      console.log(`  ⏭️  Term already exists: ${termData.wordText}`);
    }
  }

  console.log('✅ Seed completed successfully!');
}

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

