import {
  AppConfigSlug,
  BaseSetting,
  db,
  PostType,
  VisitType,
} from '@nice/common';
export async function updateTotalCourseViewCount(type: VisitType) {
  const posts = await db.post.findMany({
    where: {
      // type: { in: [PostType.COURSE, PostType.LECTURE,] },
      deletedAt: null,
    },
    select: { id: true, type: true },
  });

  const courseIds = posts
    .filter((post) => post.type === PostType.COURSE)
    .map((course) => course.id);
  const lectures = posts.filter((post) => post.type === PostType.LECTURE);
  const totalViews = await db.visit.aggregate({
    _sum: {
      views: true,
    },
    where: {
      postId: { in: posts.map((post) => post.id) },
      type: type,
    },
  });
  const appConfig = await db.appConfig.findFirst({
    where: {
      slug: AppConfigSlug.BASE_SETTING,
    },
    select: {
      id: true,
      meta: true,
    },
  });
  const staffs = await db.staff.count({
    where: { deletedAt: null },
  });

  const baseSeting = appConfig.meta as BaseSetting;
  await db.appConfig.update({
    where: {
      slug: AppConfigSlug.BASE_SETTING,
    },
    data: {
      meta: {
        ...baseSeting,
        appConfig: {
          ...(baseSeting?.appConfig || {}),
          statistics: {
            reads: totalViews._sum.views || 0,
            courses: courseIds?.length || 0,
            staffs: staffs || 0,
            lectures: lectures?.length || 0,
          },
        },
      },
    },
  });
}
export async function updatePostViewCount(id: string, type: VisitType) {
  const post = await db.post.findFirst({
    where: { id },
    select: { id: true, meta: true, type: true },
  });
  const metaFieldMap = {
    [VisitType.READED]: 'views',
    [VisitType.LIKE]: 'likes',
    [VisitType.HATE]: 'hates',
  };
  if (post?.type === PostType.LECTURE) {
    const courseAncestry = await db.postAncestry.findFirst({
      where: {
        descendantId: post?.id,
        ancestor: {
          type: PostType.COURSE,
        },
      },
      select: { id: true, ancestorId: true },
    });
    const course = { id: courseAncestry.ancestorId };
    const lecturesAncestry = await db.postAncestry.findMany({
      where: { ancestorId: course.id, descendant: { type: PostType.LECTURE } },
      select: {
        id: true,
        descendantId: true,
      },
    });
    const lectures = lecturesAncestry.map((ancestry) => ({
      id: ancestry.descendantId,
    }));
    const courseViews = await db.visit.aggregate({
      _sum: {
        views: true,
      },
      where: {
        postId: {
          in: [course.id, ...lectures.map((lecture) => lecture.id)],
        },
        type: type,
      },
    });
    await db.post.update({
      where: { id: course.id },
      data: {
        [metaFieldMap[type]]: courseViews._sum.views || 0,
        meta: {
          ...((post?.meta as any) || {}),
          [metaFieldMap[type]]: courseViews._sum.views || 0,
        },
      },
    });
  }
  const totalViews = await db.visit.aggregate({
    _sum: {
      views: true,
    },
    where: {
      postId: id,
      type: type,
    },
  });
  await db.post.update({
    where: { id },
    data: {
      [metaFieldMap[type]]: totalViews._sum.views || 0,
      meta: {
        ...((post?.meta as any) || {}),
        [metaFieldMap[type]]: totalViews._sum.views || 0,
      },
    },
  });
}
