package com.kamco.cd.kamcoback.postgres.core;

import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.AllocateInfoDto;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.InferenceDetail;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.LabelerDetail;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.UserList;
import com.kamco.cd.kamcoback.label.dto.WorkerStatsDto.WorkProgressInfo;
import com.kamco.cd.kamcoback.label.dto.WorkerStatsDto.WorkerStatistics;
import com.kamco.cd.kamcoback.postgres.entity.LabelingAssignmentEntity;
import com.kamco.cd.kamcoback.postgres.repository.label.LabelAllocateRepository;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LabelAllocateCoreService {

  private final LabelAllocateRepository labelAllocateRepository;

  public List<AllocateInfoDto> fetchNextIds(
      Long lastId, Long batchSize, Integer compareYyyy, Integer targetYyyy, Integer stage) {
    return labelAllocateRepository.fetchNextIds(lastId, batchSize, compareYyyy, targetYyyy, stage);
  }

  public void assignOwner(
      List<AllocateInfoDto> ids,
      String userId,
      Integer compareYyyy,
      Integer targetYyyy,
      Integer stage) {
    labelAllocateRepository.assignOwner(ids, userId, compareYyyy, targetYyyy, stage);
  }

  public List<LabelAllocateDto.Basic> findAssignedLabelerList(
      Integer compareYyyy, Integer targetYyyy, Integer stage) {
    return labelAllocateRepository.findAssignedLabelerList(compareYyyy, targetYyyy, stage).stream()
        .map(LabelingAssignmentEntity::toDto)
        .toList();
  }

  public Long findLabelUnAssignedCnt(Integer stage, Integer compareYyyy, Integer targetYyyy) {
    return labelAllocateRepository.findLabelUnAssignedCnt(stage, compareYyyy, targetYyyy);
  }

  public void assignInspector(UUID assignmentUid, String inspectorUid) {
    labelAllocateRepository.assignInspector(assignmentUid, inspectorUid);
  }

  public List<UserList> availUserList(String role) {
    return labelAllocateRepository.availUserList(role);
  }

  public List<WorkerStatistics> findWorkerStatistics(
      Long analUid,
      String workerType,
      String searchName,
      String searchEmployeeNo,
      String sortType) {
    return labelAllocateRepository.findWorkerStatistics(
        analUid, workerType, searchName, searchEmployeeNo, sortType);
  }

  public WorkProgressInfo findWorkProgressInfo(Long analUid) {
    return labelAllocateRepository.findWorkProgressInfo(analUid);
  }

  public Long findDailyProcessedCount(
      String workerId, String workerType, LocalDate date, Long analUid) {
    return labelAllocateRepository.findDailyProcessedCount(workerId, workerType, date, analUid);
  }

  public void assignInspectorBulk(List<UUID> assignmentUids, String inspectorUid) {
    labelAllocateRepository.assignInspectorBulk(assignmentUids, inspectorUid);
  }

  public InferenceDetail findInferenceDetail(
      Integer compareYyyy, Integer targetYyyy, Integer stage) {
    return labelAllocateRepository.findInferenceDetail(compareYyyy, targetYyyy, stage);
  }

  public Long findLabelUnCompleteCnt(Long analUid) {
    return labelAllocateRepository.findLabelUnCompleteCnt(analUid);
  }

  public List<Long> fetchNextMoveIds(
      Long lastId, Long batchSize, Integer compareYyyy, Integer targetYyyy, Integer stage) {
    return labelAllocateRepository.fetchNextMoveIds(
        lastId, batchSize, compareYyyy, targetYyyy, stage);
  }

  public void assignOwnerMove(List<Long> sub, String userId) {
    labelAllocateRepository.assignOwnerMove(sub, userId);
  }

  public LabelerDetail findLabelerDetail(
      String userId, Integer compareYyyy, Integer targetYyyy, Integer stage) {
    return labelAllocateRepository.findLabelerDetail(userId, compareYyyy, targetYyyy, stage);
  }
}
