import { prisma } from "../../config/prisma";
import {
  ApprovalStatus,
  DocumentStatus,
  Priority,
  TaskStatus,
} from "@prisma/client";

export function verifyServiceInOrg(serviceId: string, organizationId: string) {
  return prisma.service.findFirst({
    where: { id: serviceId, organizationId },
    include: { serviceDocuments: true },
  });
}

export async function getOrCreateYearFolder(clientId: string, year: number) {
  let folder = await prisma.documentFolder.findFirst({
    where: { clientId, year, parentId: null },
  });
  if (!folder) {
    folder = await prisma.documentFolder.create({
      data: { clientId, year, name: String(year) },
    });
  }
  return folder;
}

export function createTaskWithDocuments(data: {
  organizationId: string;
  clientId: string;
  serviceId: string;
  title: string;
  description?: string;
  dueDate?: Date;
  folderId: string;
  documents: {
    serviceDocumentId: string | null;
    fileName: string;
    fileUrl: string;
    fileType?: string;
    fileSizeKb?: number;
    uploadedByUserId: string;
  }[];
}) {
  return prisma.$transaction(async (tx) => {
    const task = await tx.task.create({
      data: {
        organizationId: data.organizationId,
        clientId: data.clientId,
        serviceId: data.serviceId,
        title: data.title,
        description: data.description,
        dueDate: data.dueDate,
        status: "PENDING",
        // assignedEmployeeId intentionally left null
      },
    });

    if (data.documents.length) {
      await tx.document.createMany({
        data: data.documents.map((d) => ({
          clientId: data.clientId,
          folderId: data.folderId,
          taskId: task.id,
          serviceDocumentId: d.serviceDocumentId,
          fileName: d.fileName,
          fileUrl: d.fileUrl,
          fileType: d.fileType,
          fileSizeKb: d.fileSizeKb,
          uploadedByUserId: d.uploadedByUserId,
        })),
      });
    }

    return tx.task.findUnique({
      where: { id: task.id },
      include: {
        service: true,
        documents: { include: { serviceDocument: true } },
      },
    });
  });
}

export async function findTasksByClient(
  clientId: string,
  page: number,
  limit: number,
  search?: string,
  status?: TaskStatus,
  priority?: Priority,
) {
  const where: any = { clientId, ...(status ? { status } : {}) };
  if (status) where.status = status;
  if (priority) where.priority = priority;
  if (search) {
    where.OR = [
      { title: { contains: search } },
      { description: { contains: search } },
      { service: { name: { contains: search } } },
    ];
  }
  const [data, total] = await Promise.all([
    prisma.task.findMany({
      where,
      orderBy: { createdAt: "desc" },
      skip: (page - 1) * limit,
      take: limit,
      include: {
        service: true,
        documents: { include: { serviceDocument: true } },
        invoice: { select: { id: true, status: true, totalAmount: true } },
      },
    }),
    prisma.task.count({ where }),
  ]);
  return { data, total };
}

// ---------- REPOSITORY (task.repo.ts) ----------
export async function findClientTaskForApproval(
  taskId: string,
  clientId: string,
) {
  return prisma.task.findFirst({
    where: { id: taskId, clientId, status: TaskStatus.CLIENT_APPROVAL },
  });
}

export function decideTaskByClient(
  taskId: string,
  clientId: string,
  status: Extract<ApprovalStatus, "APPROVED" | "REJECTED">,
  remarks?: string,
) {
  return prisma.$transaction(async (tx) => {
    await tx.taskApproval.upsert({
      where: { taskId_stage: { taskId, stage: "CLIENT" } },
      create: {
        taskId,
        stage: "CLIENT",
        status,
        remarks,
        actedAt: new Date(),
        // if your TaskApproval model has a client-specific FK (e.g. approvedByClientId),
        // set it here instead of leaving this blank
      },
      update: {
        status,
        remarks,
        actedAt: new Date(),
      },
    });

    return tx.task.update({
      where: { id: taskId },
      data: {
        status:
          status === "APPROVED"
            ? TaskStatus.FILING_PENDING
            : TaskStatus.IN_PROGRESS,
      },
      include: { documents: true, approvals: true },
    });
  });
}

export function findTaskById(id: string, clientId: string) {
  return prisma.task.findFirst({
    where: { id, clientId },
    include: {
      service: true,
      documents: { include: { serviceDocument: true } },
      invoice: {
        select: {
          id: true,
          invoiceNumber: true,
          status: true,
          totalAmount: true,
          payments: {
            orderBy: { createdAt: "desc" },
            select: {
              id: true,
              amount: true,
              method: true,
              status: true,
              transactionRef: true,
              paidAt: true,
              createdAt: true,
            },
          },
        },
      },
    },
  });
}

export function updateTaskWithDocuments(
  taskId: string,
  clientId: string,
  data: {
    title?: string;
    description?: string;
    dueDate?: Date;
    status?: TaskStatus;
  },
  folderId: string | null,
  newDocuments: {
    serviceDocumentId: string | null;
    fileName: string;
    fileUrl: string;
    fileType?: string;
    fileSizeKb?: number;
    uploadedByUserId: string;
  }[],
  removeDocumentIds: string[],
) {
  return prisma.$transaction(async (tx) => {
    if (removeDocumentIds.length) {
      await tx.document.deleteMany({
        where: { id: { in: removeDocumentIds }, taskId, clientId },
      });
    }

    if (newDocuments.length && folderId) {
      const requestedIds = newDocuments
        .map((d) => d.serviceDocumentId)
        .filter((id): id is string => Boolean(id));

      const validReferencedDocs = requestedIds.length
        ? await tx.serviceDocument.findMany({
            where: { id: { in: requestedIds } },
            select: { id: true },
          })
        : [];

      const validIds = new Set(validReferencedDocs.map((doc) => doc.id));

      const safeDocuments = newDocuments.map((d) => ({
        ...d,
        serviceDocumentId:
          d.serviceDocumentId && validIds.has(d.serviceDocumentId)
            ? d.serviceDocumentId
            : null,
      }));

      await tx.document.createMany({
        data: safeDocuments.map((d) => ({
          clientId,
          folderId,
          taskId,
          serviceDocumentId: d.serviceDocumentId,
          fileName: d.fileName,
          fileUrl: d.fileUrl,
          fileType: d.fileType,
          fileSizeKb: d.fileSizeKb,
          uploadedByUserId: d.uploadedByUserId,
        })),
      });
    }

    await tx.task.update({ where: { id: taskId }, data });

    return tx.task.findUnique({
      where: { id: taskId },
      include: { service: true, documents: true },
    });
  });
}

export function deleteTask(taskId: string) {
  return prisma.$transaction(async (tx) => {
    await tx.document.deleteMany({ where: { taskId } });
    await tx.task.delete({ where: { id: taskId } });
  });
}

// ── ADMIN ────────────────────────────────────────────────────

export async function findTasksByOrg(
  organizationId: string,
  page: number,
  limit: number,
  filters: {
    status?: TaskStatus;
    search?: string;
    clientId?: string;
    departmentId?: string;
    assignedEmployeeId?: string;
    priority?: string;
  },
) {
  const where: any = { organizationId };
  if (filters.status) where.status = filters.status;
  if (filters.clientId) where.clientId = filters.clientId;
  if (filters.departmentId) where.departmentId = filters.departmentId;
  if (filters.assignedEmployeeId)
    where.assignedEmployeeId = filters.assignedEmployeeId;
  if (filters.priority) where.priority = filters.priority;
  if (filters.search) {
    where.OR = [
      { title: { contains: filters.search } },
      { client: { name: { contains: filters.search } } },
      { client: { email: { contains: filters.search } } },
      { service: { name: { contains: filters.search } } },
    ];
  }
  const [data, total] = await Promise.all([
    prisma.task.findMany({
      where,
      orderBy: { createdAt: "desc" },
      skip: (page - 1) * limit,
      take: limit,
      include: {
        client: {
          select: { id: true, name: true, email: true, companyName: true },
        },
        service: { select: { id: true, name: true, category: true } },
        department: { select: { id: true, name: true } },
        assignedEmployee: {
          select: { id: true, name: true, email: true, role: true },
        },
        documents: {
          select: {
            id: true,
            fileName: true,
            fileUrl: true,
            fileType: true,
            fileSizeKb: true,
            status: true,
            createdAt: true,
          },
        },
        approvals: true,
        invoice: { select: { id: true, status: true, totalAmount: true } },
      },
    }),
    prisma.task.count({ where }),
  ]);
  return { data, total };
}

export function findTaskByIdForAdmin(taskId: string, organizationId: string) {
  return prisma.task.findFirst({
    where: { id: taskId, organizationId },
    include: {
      client: {
        select: {
          id: true,
          name: true,
          email: true,
          companyName: true,
          phone: true,
        },
      },
      service: { select: { id: true, name: true, category: true } },
      department: { select: { id: true, name: true } },
      assignedEmployee: {
        select: {
          id: true,
          name: true,
          email: true,
          role: true,
          departmentId: true,
        },
      },
      documents: {
        select: {
          id: true,
          fileName: true,
          fileUrl: true,
          fileType: true,
          fileSizeKb: true,
          status: true,
          createdAt: true,
          serviceDocumentId: true,
        },
      },
      approvals: true,
      invoice: { select: { id: true, status: true, totalAmount: true } },
    },
  });
}

export function adminUpdateTask(
  taskId: string,
  data: {
    status?: TaskStatus;
    assignedEmployeeId?: string | null;
    departmentId?: string | null;
    priority?: Priority;
    dueDate?: Date;
    title?: string;
    description?: string;
  },
) {
  return prisma.task.update({
    where: { id: taskId },
    data,
    include: {
      client: { select: { id: true, name: true, email: true } },
      service: { select: { id: true, name: true } },
      department: { select: { id: true, name: true } },
      assignedEmployee: {
        select: { id: true, name: true, email: true, role: true },
      },
      documents: true,
    },
  });
}

export function findActiveEmployeesByOrg(organizationId: string) {
  return prisma.employee.findMany({
    where: { organizationId, status: "ACTIVE" },
    select: {
      id: true,
      name: true,
      email: true,
      role: true,
      departmentId: true,
      department: { select: { id: true, name: true } },
    },
    orderBy: { name: "asc" },
  });
}

export async function findEmployeeByUserId(userId: string) {
  return prisma.employee.findUnique({
    where: { userId },
    select: {
      id: true,
    },
  });
}
export async function findTaskForEmployee(taskId: string, employeeId: string) {
  return prisma.task.findFirst({
    where: {
      id: taskId,
      assignedEmployeeId: employeeId,
    },
    include: {
      client: {
        select: {
          id: true,
          name: true,
          email: true,
          companyName: true,
        },
      },
      service: {
        select: {
          id: true,
          name: true,
          category: true,
        },
      },
      department: {
        select: {
          id: true,
          name: true,
        },
      },
      assignedEmployee: {
        select: {
          id: true,
          name: true,
          email: true,
          role: true,
        },
      },
      documents: {
        select: {
          id: true,
          fileName: true,
          fileUrl: true,
          fileType: true,
          fileSizeKb: true,
          status: true,
          createdAt: true,
        },
      },
      approvals: true,
    },
  });
}

export async function findTasksByEmployee(
  employeeId: string,
  page: number,
  limit: number,
  search: string,
  status?: TaskStatus,
  priority?: Priority,
) {
  const where: any = {
    assignedEmployeeId: employeeId,
  };

  if (status) {
    where.status = status;
  }
  if (priority) where.priority = priority;

  if (search?.trim()) {
    where.OR = [
      { title: { contains: search } },
      { client: { name: { contains: search } } },
      { client: { email: { contains: search } } },
      { client: { companyName: { contains: search } } },
      { service: { name: { contains: search } } },
    ];
  }

  const [data, total] = await Promise.all([
    prisma.task.findMany({
      where,
      orderBy: {
        createdAt: "desc",
      },
      skip: (page - 1) * limit,
      take: limit,
      include: {
        client: {
          select: {
            id: true,
            name: true,
            email: true,
            companyName: true,
          },
        },
        service: {
          select: {
            id: true,
            name: true,
            category: true,
          },
        },
        department: {
          select: {
            id: true,
            name: true,
          },
        },
        documents: {
          select: {
            id: true,
            fileName: true,
            fileUrl: true,
            status: true,
          },
        },
      },
    }),
    prisma.task.count({ where }),
  ]);

  return { data, total };
}

export async function findEmployeeTask(taskId: string, employeeId: string) {
  return prisma.task.findFirst({
    where: {
      id: taskId,
      assignedEmployeeId: employeeId,
    },
  });
}

// export async function verifyDocuments(
//   taskId: string,
//   documentIds: string[],
//   status: "VERIFIED" | "REJECTED",
//   remarks?: string,
// ) {
//   return prisma.$transaction(async (tx) => {
//     await tx.document.updateMany({
//       where: {taskId,id: { in: documentIds,},},
//       data: {
//         status,
//         aiVerificationNotes: remarks,
//       },
//     });

//     const task = await tx.task.update({
//       where: {
//         id: taskId,
//       },
//       data: {
//         status: status === "VERIFIED" ? "EMPLOYEE_DONE" : "IN_PROGRESS",
//       },
//       include: {
//         documents: true,
//       },
//     });

//     return task;
//   });
// }

export async function verifyDocuments(
  taskId: string,
  documentIds: string[],
  // status: "VERIFIED" | "REJECTED",
  status: Extract<DocumentStatus, "VERIFIED" | "REJECTED">,
  remarks?: string,
) {
  return prisma.$transaction(async (tx) => {
    await tx.document.updateMany({
      where: { taskId, id: { in: documentIds } },
      data: { status, aiVerificationNotes: remarks },
    });

    const allDocs = await tx.document.findMany({
      where: { taskId },
      select: { status: true },
    });

    const allDecided = allDocs.every(
      (d) => d.status === "VERIFIED" || d.status === "REJECTED",
    );
    const newTaskStatus = allDecided ? "EMPLOYEE_DONE" : "IN_PROGRESS";

    const task = await tx.task.update({
      where: { id: taskId },
      data: { status: newTaskStatus },
      include: { documents: true },
    });

    return task;
  });
}

export function findActiveEmployeesByDepartment(
  organizationId: string,
  departmentId: string,
  search?: string,
) {
  const where: any = { organizationId, departmentId, status: "ACTIVE" };
  if (search) {
    where.OR = [
      { name: { contains: search } },
      { email: { contains: search } },
    ];
  }
  return prisma.employee.findMany({
    where,
    select: {
      id: true,
      name: true,
      email: true,
      role: true,
      departmentId: true,
      department: { select: { id: true, name: true } },
    },
    orderBy: { name: "asc" },
  });
}

export function findActiveClientsByOrg(
  organizationId: string,
  search?: string,
  page = 1,
  limit = 10,
) {
  const where: any = { organizationId, status: { in: ["ACTIVE", "INVITED"] } };
  if (search) {
    where.OR = [
      { name: { contains: search } },
      { email: { contains: search } },
      { companyName: { contains: search } },
    ];
  }
  return Promise.all([
    prisma.client.findMany({
      where,
      select: {
        id: true,
        name: true,
        email: true,
        companyName: true,
        status: true,
      },
      orderBy: { name: "asc" },
      skip: (page - 1) * limit,
      take: limit,
    }),
    prisma.client.count({ where }),
  ]).then(([data, total]) => ({ data, total }));
}

export function adminDeleteTask(taskId: string, organizationId: string) {
  return prisma.$transaction(async (tx) => {
    const task = await tx.task.findFirst({
      where: { id: taskId, organizationId },
    });
    if (!task) return null;
    await tx.taskApproval.deleteMany({ where: { taskId } });
    await tx.document.deleteMany({ where: { taskId } });
    await tx.invoice.deleteMany({ where: { taskId } });
    await tx.task.delete({ where: { id: taskId } });
    return true;
  });
}

export function adminCreateTaskWithDocuments(data: {
  organizationId: string;
  clientId: string;
  serviceId: string;
  departmentId?: string;
  assignedEmployeeId?: string;
  title: string;
  description?: string;
  dueDate?: Date;
  priority?: Priority;
  folderId: string;
  documents: {
    serviceDocumentId: string | null;
    fileName: string;
    fileUrl: string;
    fileType?: string;
    fileSizeKb?: number;
    uploadedByUserId: string;
  }[];
}) {
  return prisma.$transaction(async (tx) => {
    const task = await tx.task.create({
      data: {
        organizationId: data.organizationId,
        clientId: data.clientId,
        serviceId: data.serviceId,
        departmentId: data.departmentId,
        assignedEmployeeId: data.assignedEmployeeId,
        title: data.title,
        description: data.description,
        dueDate: data.dueDate,
        priority: data.priority ?? "MEDIUM",
        status: "PENDING",
      },
    });

    if (data.documents.length) {
      await tx.document.createMany({
        data: data.documents.map((d) => ({
          clientId: data.clientId,
          folderId: data.folderId,
          taskId: task.id,
          serviceDocumentId: d.serviceDocumentId,
          fileName: d.fileName,
          fileUrl: d.fileUrl,
          fileType: d.fileType,
          fileSizeKb: d.fileSizeKb,
          uploadedByUserId: d.uploadedByUserId,
        })),
      });
    }

    return tx.task.findUnique({
      where: { id: task.id },
      include: {
        client: {
          select: { id: true, name: true, email: true, companyName: true },
        },
        service: { select: { id: true, name: true, category: true } },
        department: { select: { id: true, name: true } },
        assignedEmployee: { select: { id: true, name: true, email: true } },
        documents: { include: { serviceDocument: true } },
      },
    });
  });
}

export function approveTaskByEmployee(
  taskId: string,
  employeeId: string,
  remarks?: string,
) {
  return prisma.$transaction(async (tx) => {
    await tx.taskApproval.upsert({
      where: { taskId_stage: { taskId, stage: "EMPLOYEE" } },
      create: {
        taskId,
        stage: "EMPLOYEE",
        status: "APPROVED",
        approvedByEmployeeId: employeeId,
        remarks,
        actedAt: new Date(),
      },
      update: {
        status: "APPROVED",
        approvedByEmployeeId: employeeId,
        remarks,
        actedAt: new Date(),
      },
    });

    return tx.task.update({
      where: { id: taskId },
      data: { status: "MANAGER_APPROVAL" },
      include: { documents: true, approvals: true },
    });
  });
}

export async function findManagerByUserId(userId: string) {
  return prisma.employee.findUnique({
    where: { userId },
    select: { id: true, organizationId: true, departmentId: true, role: true },
  });
}

// Manager get tasks for approval
// export async function findTasksForManager(
//   organizationId: string,
//   page: number,
//   limit: number,
//   status?: TaskStatus,
//   departmentId?: string | null,
// ) {
//   const where: any = { organizationId };
//   if (status) where.status = status;
//   if (departmentId) where.departmentId = departmentId;

//   const [data, total] = await Promise.all([
//     prisma.task.findMany({
//       where,
//       orderBy: { createdAt: "desc" },
//       skip: (page - 1) * limit,
//       take: limit,
//       include: {
//         client: {
//           select: { id: true, name: true, email: true, companyName: true },
//         },
//         service: { select: { id: true, name: true, category: true } },
//         department: { select: { id: true, name: true } },
//         assignedEmployee: {
//           select: { id: true, name: true, email: true, role: true },
//         },
//         documents: {
//           select: {
//             id: true,
//             fileName: true,
//             fileUrl: true,
//             fileType: true,
//             fileSizeKb: true,
//             status: true,
//             createdAt: true,
//           },
//         },
//         approvals: true,
//       },
//     }),
//     prisma.task.count({ where }),
//   ]);
//   return { data, total };
// }

export async function findTasksForManager(
  organizationId: string,
  page: number,
  limit: number,
  search: string,
  status?: TaskStatus,
  departmentId?: string | null,
  priority?: Priority,
) {
  const where: any = {
    organizationId,
  };

  if (status) where.status = status;
  if (departmentId) where.departmentId = departmentId;
  if (priority) where.priority = priority;

  if (search?.trim()) {
    where.OR = [
      { title: { contains: search } },
      { client: { name: { contains: search } } },
      { client: { email: { contains: search } } },
      { client: { companyName: { contains: search } } },
      { service: { name: { contains: search } } },
    ];
  }

  const [data, total] = await Promise.all([
    prisma.task.findMany({
      where,
      orderBy: { createdAt: "desc" },
      skip: (page - 1) * limit,
      take: limit,
      include: {
        client: {
          select: {
            id: true,
            name: true,
            email: true,
            companyName: true,
          },
        },
        service: {
          select: {
            id: true,
            name: true,
            category: true,
          },
        },
        department: {
          select: {
            id: true,
            name: true,
          },
        },
        assignedEmployee: {
          select: {
            id: true,
            name: true,
            email: true,
            role: true,
          },
        },
        documents: {
          select: {
            id: true,
            fileName: true,
            fileUrl: true,
            fileType: true,
            fileSizeKb: true,
            status: true,
            createdAt: true,
          },
        },
        approvals: true,
      },
    }),
    prisma.task.count({ where }),
  ]);

  return { data, total };
}

export async function findTaskForManager(
  taskId: string,
  organizationId: string,
  departmentId?: string | null,
) {
  return prisma.task.findFirst({
    where: {
      id: taskId,
      organizationId,
      ...(departmentId ? { departmentId } : {}),
    },
    include: {
      client: {
        select: {
          id: true,
          name: true,
          email: true,
          companyName: true,
        },
      },
      service: {
        select: {
          id: true,
          name: true,
          category: true,
        },
      },
      department: {
        select: {
          id: true,
          name: true,
        },
      },
      assignedEmployee: {
        select: {
          id: true,
          name: true,
          email: true,
          role: true,
        },
      },
      documents: {
        select: {
          id: true,
          fileName: true,
          fileUrl: true,
          fileType: true,
          fileSizeKb: true,
          status: true,
          createdAt: true,
        },
      },
      approvals: true,
    },
  });
}

// export async function findManagerTask(taskId: string, organizationId: string) {
//   return prisma.task.findFirst({
//     where: { id: taskId, organizationId, status: TaskStatus.MANAGER_APPROVAL },
//     include: { documents: true, approvals: true },
//   });
// }

export async function findManagerTask(
  taskId: string,
  organizationId: string,
  departmentId?: string | null,
) {
  return prisma.task.findFirst({
    where: {
      id: taskId,
      organizationId,
      status: TaskStatus.MANAGER_APPROVAL,
      ...(departmentId ? { departmentId } : {}),
    },
    include: { documents: true, approvals: true },
  });
}

export function decideTaskByManager(
  taskId: string,
  managerId: string,
  status: Extract<ApprovalStatus, "APPROVED" | "REJECTED">,
  remarks?: string,
) {
  return prisma.$transaction(async (tx) => {
    await tx.taskApproval.upsert({
      where: { taskId_stage: { taskId, stage: "MANAGER" } },
      create: {
        taskId,
        stage: "MANAGER",
        status,
        approvedByEmployeeId: managerId,
        remarks,
        actedAt: new Date(),
      },
      update: {
        status,
        approvedByEmployeeId: managerId,
        remarks,
        actedAt: new Date(),
      },
    });

    return tx.task.update({
      where: { id: taskId },
      data: {
        status:
          status === "APPROVED"
            ? TaskStatus.CLIENT_APPROVAL
            : TaskStatus.IN_PROGRESS,
      },
      include: { documents: true, approvals: true },
    });
  });
}

export function findRejectedDocumentsByTask(taskId: string, clientId: string) {
  return prisma.document.findMany({
    where: { taskId, clientId, status: DocumentStatus.REJECTED },
    select: {
      id: true,
      fileName: true,
      fileUrl: true,
      fileType: true,
      aiVerificationNotes: true,
    },
  });
}

export function findManagerRejection(taskId: string) {
  return prisma.taskApproval.findFirst({
    where: { taskId, stage: "MANAGER", status: ApprovalStatus.REJECTED },
    select: {
      remarks: true,
      actedAt: true,
      approvedByEmployeeId: true,
    },
  });
}

export async function findTasksWithRejectedDocs(clientId: string) {
  return prisma.task.findMany({
    where: {
      clientId,
      OR: [
        { documents: { some: { status: DocumentStatus.REJECTED } } },
        {
          approvals: {
            some: {
              stage: "MANAGER",
              status: ApprovalStatus.REJECTED,
            },
          },
        },
      ],
    },
    orderBy: { updatedAt: "desc" },
    include: {
      service: { select: { id: true, name: true, category: true } },
      documents: {
        // where: { status: DocumentStatus.REJECTED },
        select: {
          id: true,
          fileName: true,
          fileUrl: true,
          fileType: true,
          fileSizeKb: true,
          status: true,
          aiVerificationNotes: true,
          serviceDocument: { select: { id: true, name: true } },
        },
      },
      approvals: {
        where: {
          stage: "MANAGER",
          status: ApprovalStatus.REJECTED,
        },
        select: {
          id: true,
          stage: true,
          status: true,
          remarks: true,
          actedAt: true,
          approvedByEmployeeId: true,
        },
      },
    },
  });
}
