import { db } from './src/lib/db';

const defaultServices = [
  {
    title: 'LAYANAN SPKT',
    subtitle: 'Pelayanan Terpadu',
    description: 'SPKT adalah unit pelayanan kepolisian terpadu',
    linkType: 'SUBMENU',
    order: 1,
    isActive: true,
    parentId: null
  },
  {
    title: 'LAYANAN LALU LINTAS',
    subtitle: 'SATUAN LALU LINTAS',
    description: 'Pelayanan perizinan dan informasi lalu lintas',
    linkType: 'SUBMENU',
    order: 2,
    isActive: true,
    parentId: null
  },
  {
    title: 'LAYANAN SKCK',
    subtitle: 'SATUAN INTELKAM',
    description: 'SKCK adalah surat keterangan yang diterbitkan oleh polisi',
    linkType: 'SUBMENU',
    order: 3,
    isActive: true,
    parentId: null
  },
  {
    title: 'LIVE CHAT',
    subtitle: 'POLRES WONOSOBO',
    description: 'Layanan komunikasi online dengan Polres Wonosobo',
    linkType: 'EXTERNAL',
    linkUrl: 'https://wa.me/6281234567890',
    order: 4,
    isActive: true,
    parentId: null
  }
];

const submenuItems = [
  // SPKT Submenu
  {
    title: 'Laporan/Pengaduan',
    description: 'Laporan pengaduan masyarakat',
    linkType: 'INTERNAL',
    linkPage: '/laporan-pengaduan',
    order: 1,
    isActive: true
  },
  {
    title: 'Laporan Kehilangan',
    description: 'Laporan kehilangan barang atau dokumen',
    linkType: 'INTERNAL',
    linkPage: '/laporan-kehilangan',
    order: 2,
    isActive: true
  },
  {
    title: 'Izin Keramaian',
    description: 'Perizinan kegiatan keramaian',
    linkType: 'INTERNAL',
    linkPage: '/izin-keramaian',
    order: 3,
    isActive: true
  },
  // Lalu Lintas Submenu
  {
    title: 'STNK',
    description: 'Pelayanan STNK tahunan',
    linkType: 'INTERNAL',
    linkPage: '/stnk',
    order: 1,
    isActive: true
  },
  {
    title: 'SIM',
    description: 'Pembuatan dan perpanjangan SIM',
    linkType: 'INTERNAL',
    linkPage: '/sim',
    order: 2,
    isActive: true
  },
  {
    title: 'Info Laka',
    description: 'Informasi kecelakaan lalu lintas',
    linkType: 'INTERNAL',
    linkPage: '/info-laka',
    order: 3,
    isActive: true
  },
  {
    title: 'Info Arus Lalin',
    description: 'Informasi kondisi arus lalu lintas',
    linkType: 'INTERNAL',
    linkPage: '/info-arus-lalin',
    order: 4,
    isActive: true
  },
  // SKCK Submenu
  {
    title: 'Pendaftaran Online',
    description: 'Pendaftaran SKCK secara online',
    linkType: 'INTERNAL',
    linkPage: '/skck-pendaftaran',
    order: 1,
    isActive: true
  },
  {
    title: 'Syarat dan Prosedur',
    description: 'Informasi syarat dan prosedur pembuatan SKCK',
    linkType: 'INTERNAL',
    linkPage: '/skck-syarat',
    order: 2,
    isActive: true
  }
];

async function seedServices() {
  try {
    console.log('Seeding default services...');

    // Clear existing services
    await db.serviceLink.deleteMany();
    console.log('Cleared existing services');

    // Create main services
    const createdServices = [];
    for (const service of defaultServices) {
      const created = await db.serviceLink.create({
        data: service
      });
      createdServices.push(created);
      console.log(`Created service: ${created.title}`);
    }

    // Create submenu items
    for (let i = 0; i < submenuItems.length; i++) {
      const submenu = submenuItems[i];
      let parentId;

      // Assign parent based on index
      if (i < 3) {
        parentId = createdServices[0].id; // SPKT
      } else if (i < 7) {
        parentId = createdServices[1].id; // Lalu Lintas
      } else {
        parentId = createdServices[2].id; // SKCK
      }

      const created = await db.serviceLink.create({
        data: {
          ...submenu,
          parentId
        }
      });
      console.log(`Created submenu: ${created.title} (parent: ${createdServices.find(s => s.id === parentId)?.title})`);
    }

    console.log('Services seeded successfully!');
  } catch (error) {
    console.error('Error seeding services:', error);
  } finally {
    await db.$disconnect();
  }
}

seedServices();