import dotenv from 'dotenv';
import path from 'path';
import { PrismaClient } from '@prisma/client';
import { DEFAULT_ROLE_PERMISSIONS } from '../src/constants/permissions';

dotenv.config({ path: path.resolve(__dirname, '../../.env') });

const prisma = new PrismaClient();

async function main() {
  const relays = [
    { url: 'wss://relay.damus.io', priority: 1 },
    { url: 'wss://nos.lol', priority: 2 },
    { url: 'wss://relay.nostr.band', priority: 3 },
  ];

  for (const relay of relays) {
    await prisma.relay.upsert({
      where: { url: relay.url },
      update: {},
      create: relay,
    });
  }

  const settings = [
    { key: 'site_title', value: 'Belgian Bitcoin Embassy' },
    { key: 'site_tagline', value: 'Your gateway to Bitcoin in Belgium' },
    { key: 'telegram_link', value: 'https://t.me/belgianbitcoinembassy' },
    { key: 'nostr_link', value: '' },
    { key: 'x_link', value: '' },
    { key: 'youtube_link', value: '' },
    { key: 'discord_link', value: '' },
    { key: 'linkedin_link', value: '' },
  ];

  for (const setting of settings) {
    await prisma.setting.upsert({
      where: { key: setting.key },
      update: {},
      create: setting,
    });
  }

  const categories = [
    { name: 'Bitcoin', slug: 'bitcoin', sortOrder: 1 },
    { name: 'Lightning', slug: 'lightning', sortOrder: 2 },
    { name: 'Privacy', slug: 'privacy', sortOrder: 3 },
    { name: 'Education', slug: 'education', sortOrder: 4 },
    { name: 'Community', slug: 'community', sortOrder: 5 },
  ];

  for (const category of categories) {
    await prisma.category.upsert({
      where: { slug: category.slug },
      update: {},
      create: category,
    });
  }

  const defaultOrganizer = await prisma.organizer.upsert({
    where: { slug: 'belgian-bitcoin-embassy' },
    update: {},
    create: {
      id: '00000000-0000-4000-8000-000000000001',
      name: 'Belgian Bitcoin Embassy',
      slug: 'belgian-bitcoin-embassy',
    },
  });

  const existingMeetup = await prisma.meetup.findFirst({
    where: { title: 'Monthly Bitcoin Meetup' },
  });

  if (!existingMeetup) {
    await prisma.meetup.create({
      data: {
        title: 'Monthly Bitcoin Meetup',
        description:
          'Join us for our monthly Bitcoin meetup! We discuss the latest developments, share knowledge, and connect with fellow Bitcoiners in Belgium.',
        date: '2025-02-15',
        time: '19:00',
        location: 'Brussels, Belgium',
        link: 'https://meetup.com/example',
        status: 'PUBLISHED',
        featured: true,
        organizerId: defaultOrganizer.id,
      },
    });
  }

  // Seed default role permissions. This only fills in missing rows so it never
  // clobbers permissions an operator has customized at runtime.
  for (const [role, permissions] of Object.entries(DEFAULT_ROLE_PERMISSIONS)) {
    for (const permission of permissions) {
      await prisma.rolePermission.upsert({
        where: { role_permission: { role, permission } },
        update: {},
        create: { role, permission },
      });
    }
  }

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

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