import { seederAdmin, seederAdminRole, seederAnnouncement, seederAnnouncementMember, seederDesa, seederDiscussion, seederDiscussionMember, seederDivision, seederDivisionMember, seederGroup, seederPosition, seederProject, seederProjectMember, seederProjectTask, seederSetting, seederTheme, seederUser, seederUserRole } from '@/module/seeder';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient()

// DATA YG DI SEEDER MERUPAKAN DATA REAL(DARMASABA) & DATA DUMMY (MANDALA)
// DATA JSON GABUNGAN (REAL & DUMMY) ADALAH adminRole, admin, theme, desa, group, position, user, userRole, user, dan setting
// Selain table yg disebutkan, data lainnya merupakan data dummy

async function main() {
   // ADMIN ROLE
   for (let data of seederAdminRole) {
      await prisma.adminRole.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name
         },
         create: {
            id: data.id,
            name: data.name,
         },
      })
   }

   // ADMIN
   for (let data of seederAdmin) {
      await prisma.admin.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name,
            idAdminRole: data.idAdminRole,
            phone: data.phone,
            email: data.email,
            gender: data.gender
         },
         create: {
            id: data.id,
            idAdminRole: data.idAdminRole,
            phone: data.phone,
            email: data.email,
            gender: data.gender,
            name: data.name
         },
      })
   }

   // THEME
   for (let data of seederTheme) {
      await prisma.colorTheme.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name,
            utama: data.utama,
            bgUtama: data.bgUtama,
            bgIcon: data.bgIcon,
            bgFiturHome: data.bgFiturHome,
            bgFiturDivision: data.bgFiturDivisi,
            bgTotalKegiatan: data.bgTotalKegiatan
         },
         create: {
            id: data.id,
            name: data.name,
            utama: data.utama,
            bgUtama: data.bgUtama,
            bgIcon: data.bgIcon,
            bgFiturHome: data.bgFiturHome,
            bgFiturDivision: data.bgFiturDivisi,
            bgTotalKegiatan: data.bgTotalKegiatan
         }
      })
   }

   // DESA
   for (let data of seederDesa) {
      await prisma.village.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name,
            desc: data.desc,
            idTheme: "theme1"
         },
         create: {
            id: data.id,
            name: data.name,
            desc: data.desc,
            idTheme: "theme1"
         }
      })
   }

   // GROUP
   for (let data of seederGroup) {
      await prisma.group.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name,
            idVillage: data.idVillage
         },
         create: {
            id: data.id,
            name: data.name,
            idVillage: data.idVillage
         }
      })
   }

   // POSITION
   for (let data of seederPosition) {
      await prisma.position.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name,
            idGroup: data.idGroup
         },
         create: {
            id: data.id,
            name: data.name,
            idGroup: data.idGroup
         }
      })
   }

   // USER ROLE
   for (let data of seederUserRole) {
      await prisma.userRole.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name
         },
         create: {
            id: data.id,
            name: data.name,
            desc: data.desc
         },
      })
   }

   // USER
   for (let data of seederUser) {
      await prisma.user.upsert({
         where: {
            id: data.id
         },
         update: {
            idVillage: data.idVillage,
            idGroup: data.idGroup,
            idPosition: data.idPosition,
            idUserRole: data.idUserRole,
            // nik: data.nik,
            name: data.name,
            // phone: data.phone,
            // email: data.email,
            gender: data.gender
         },
         create: {
            id: data.id,
            idVillage: data.idVillage,
            idGroup: data.idGroup,
            idPosition: data.idPosition,
            idUserRole: data.idUserRole,
            nik: data.nik,
            name: data.name,
            phone: data.phone,
            email: data.email,
            gender: data.gender
         },
      })
   }


   // DISCUSSION
   for (let data of seederDiscussion) {
      await prisma.discussion.upsert({
         where: {
            id: data.id
         },
         update: {
            idVillage: data.idVillage,
            idGroup: data.idGroup,
            title: data.title,
            desc: data.desc,
            status: data.status,
            createdBy: data.createdBy
         },
         create: {
            id: data.id,
            idVillage: data.idVillage,
            idGroup: data.idGroup,
            title: data.title,
            desc: data.desc,
            status: data.status,
            createdBy: data.createdBy
         },
      })
   }


   // DISSCUSSION MEMBER
   for (let data of seederDiscussionMember) {
      await prisma.discussionMember.upsert({
         where: {
            id: data.id
         },
         update: {
            idDiscussion: data.idDiscussion,
            idUser: data.idUser
         },
         create: {
            id: data.id,
            idDiscussion: data.idDiscussion,
            idUser: data.idUser
         },
      })
   }

   // PROJECT
   for (let data of seederProject) {
      await prisma.project.upsert({
         where: {
            id: data.id
         },
         update: {
            idVillage: data.idVillage,
            idGroup: data.idGroup,
            title: data.title,
            desc: data.desc,
            status: data.status,
            createdBy: data.createdBy
         },
         create: {
            id: data.id,
            idVillage: data.idVillage,
            idGroup: data.idGroup,
            title: data.title,
            desc: data.desc,
            status: data.status,
            createdBy: data.createdBy
         },
      })
   }

   // PROJECT MEMBER
   for (let data of seederProjectMember) {
      await prisma.projectMember.upsert({
         where: {
            id: data.id
         },
         update: {
            idProject: data.idProject,
            idUser: data.idUser,
            isLeader: data.isLeader
         },
         create: {
            id: data.id,
            idProject: data.idProject,
            idUser: data.idUser,
            isLeader: data.isLeader
         },
      })
   }

   // PROJECT TASK
   for (let data of seederProjectTask) {
      await prisma.projectTask.upsert({
         where: {
            id: data.id
         },
         update: {
            idProject: data.idProject,
            title: data.title,
            desc: data.desc,
            status: data.status,
            dateStart: new Date(data.dateStart),
            dateEnd: new Date(data.dateEnd)
         },
         create: {
            id: data.id,
            idProject: data.idProject,
            title: data.title,
            desc: data.desc,
            status: data.status,
            dateStart: new Date(data.dateStart),
            dateEnd: new Date(data.dateEnd)
         },
      })
   }

   // DIVISION
   for (let data of seederDivision) {
      await prisma.division.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name,
            desc: data.desc,
            createdBy: data.createdBy
         },
         create: {
            id: data.id,
            idVillage: data.idVillage,
            idGroup: data.idGroup,
            name: data.name,
            desc: data.desc,
            createdBy: data.createdBy,
            isActive: true
         }
      })
   }

   // DIVISION MEMBER
   for (let data of seederDivisionMember) {
      await prisma.divisionMember.upsert({
         where: {
            id: data.id
         },
         update: {
            idUser: data.idUser,
            isAdmin: data.isAdmin,
            isLeader: data.isLeader
         },
         create: {
            id: data.id,
            idDivision: data.idDivision,
            idUser: data.idUser,
            isAdmin: data.isAdmin,
            isLeader: data.isLeader,
            isActive: true
         }
      })
   }

   // ANNOUNCEMENT
   for (let data of seederAnnouncement) {
      await prisma.announcement.upsert({
         where: {
            id: data.id
         },
         update: {
            title: data.title,
            desc: data.desc,
            createdBy: data.createdBy
         },
         create: {
            id: data.id,
            idVillage: data.idVillage,
            title: data.title,
            desc: data.desc,
            createdBy: data.createdBy,
            isActive: true
         }
      })
   }

   // ANNOUNCEMENT MEMBER
   for (let data of seederAnnouncementMember) {
      await prisma.announcementMember.upsert({
         where: {
            id: data.id
         },
         update: {
            idAnnouncement: data.idAnnouncement,
            idGroup: data.idGroup,
            idDivision: data.idDivision
         },
         create: {
            id: data.id,
            idAnnouncement: data.idAnnouncement,
            idGroup: data.idGroup,
            idDivision: data.idDivision,
            isActive: true
         }
      })
   }

   // SETTING
   for (let data of seederSetting) {
      await prisma.setting.upsert({
         where: {
            id: data.id
         },
         update: {
            name: data.name,
         },
         create: {
            id: data.id,
            name: data.name,
            value: data.value
         }
      })
   }
}

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