import { Injectable } from '@nestjs/common';
import { BaseService } from '../base/base.service';
import { UserProfile, db, ObjectType, Prisma, VisitType } from '@nice/common';
import EventBus from '@server/utils/event-bus';
@Injectable()
export class VisitService extends BaseService<Prisma.VisitDelegate> {
  constructor() {
    super(db, ObjectType.VISIT);
  }
  async create(args: Prisma.VisitCreateArgs, staff?: UserProfile) {
    const { postId, lectureId, messageId } = args.data;
    const visitorId = args.data?.visitorId || staff?.id;
    let result;
    const existingVisit = await db.visit.findFirst({
      where: {
        type: args.data.type,
        // visitorId: visitorId ? visitorId : null,
        OR: [{ postId }, { messageId }],
      },
    });
    if (!existingVisit) {
      result = await super.create(args);
    } else if (args.data.type === VisitType.READED) {
      result = await super.update({
        where: { id: existingVisit.id },
        data: {
          ...args.data,
          views: existingVisit.views + 1,
        },
      });
    }

    if (
      [VisitType.READED, VisitType.LIKE, VisitType.HATE].includes(
        args.data.type as VisitType,
      )
    ) {
      EventBus.emit('updateVisitCount', {
        objectType: ObjectType.POST,
        id: postId,
        visitType: args.data.type, // 直接复用传入的类型
      });
      EventBus.emit('updateTotalCourseViewCount', {
        visitType: args.data.type, // 直接复用传入的类型
      });
    }

    return result;
  }
  async createMany(args: Prisma.VisitCreateManyArgs, staff?: UserProfile) {
    const data = Array.isArray(args.data) ? args.data : [args.data];
    const updatePromises: any[] = [];
    const createData: Prisma.VisitCreateManyInput[] = [];
    await Promise.all(
      data.map(async (item) => {
        if (staff && !item.visitorId) item.visitorId = staff.id;
        const { postId, lectureId, messageId, visitorId } = item;
        const existingVisit = await db.visit.findFirst({
          where: {
            visitorId,
            OR: [{ postId }, { lectureId }, { messageId }],
          },
        });

        if (existingVisit) {
          updatePromises.push(
            super.update({
              where: { id: existingVisit.id },
              data: {
                ...item,
                views: existingVisit.views + 1,
              },
            }),
          );
        } else {
          createData.push(item);
        }
      }),
    );
    // Execute all updates in parallel
    await Promise.all(updatePromises);
    // Create new visits for those not existing
    if (createData.length > 0) {
      return super.createMany({
        ...args,
        data: createData,
      });
    }

    return { count: updatePromises.length }; // Return the number of updates if no new creates
  }
  async deleteMany(args: Prisma.VisitDeleteManyArgs, staff?: UserProfile) {
    // const where = Array.isArray(args.where) ? args.where : [args.where];
    // const updatePromises: any[] = [];
    // const createData: Prisma.VisitCreateManyInput[] = [];
    // super
    // await Promise.all(
    //   data.map(async (item) => {
    //     if (staff && !item.visitorId) item.visitorId = staff.id;
    //     const { postId, messageId, visitorId } = item;
    //     const existingVisit = await db.visit.findFirst({
    //       where: {
    //         visitorId,
    //         OR: [{ postId }, { messageId }],
    //       },
    //     });

    //     if (existingVisit) {
    //       updatePromises.push(
    //         super.update({
    //           where: { id: existingVisit.id },
    //           data: {
    //             ...item,
    //             views: existingVisit.views + 1,
    //           },
    //         }),
    //       );
    //     } else {
    //       createData.push(item);
    //     }
    //   }),
    // );
    // // Execute all updates in parallel
    // await Promise.all(updatePromises);
    // // Create new visits for those not existing
    // if (createData.length > 0) {
    //   return super.createMany({
    //     ...args,
    //     data: createData,
    //   });
    // }
    // return { count: updatePromises.length }; // Return the number of updates if no new creates
    const superDetele = super.deleteMany(args, staff);
    if (args?.where?.postId) {
      if (
        [VisitType.READED, VisitType.LIKE, VisitType.HATE].includes(
          args.where.type as any,
        )
      ) {
        EventBus.emit('updateVisitCount', {
          objectType: ObjectType.POST,
          id: args?.where?.postId as string,
          visitType: args.where.type as any, // 直接复用传入的类型
        });
        EventBus.emit('updateTotalCourseViewCount', {
          visitType: args.where.type as any, // 直接复用传入的类型
        });
      }
    }
    return superDetele;
  }
}
