package com.red.circle.other.app.inner.endpoint.team;

import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.app.inner.service.team.TeamPolicyManagerClientService;
import com.red.circle.other.infra.database.mongo.dto.team.TeamBillSettleResult;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamBillCycle;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamPolicyManager;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamBillCycleService;
import com.red.circle.other.inner.endpoint.team.target.TeamPolicyManagerClient;
import com.red.circle.other.inner.model.cmd.team.TeamBillCycleBackQryCmd;
import com.red.circle.other.inner.model.cmd.team.TeamPolicyManagerCmd;
import com.red.circle.other.inner.model.dto.agency.agency.TeamPolicyManagerDTO;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 团队策略.
 *
 * @author pengliang on 2023/8/19
 */
@Validated
@RestController
@RequestMapping(value = TeamPolicyManagerClient.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class TeamPolicyManagerClientEndpoint implements TeamPolicyManagerClient {

  private final TeamPolicyManagerClientService teamPolicyManagerClientService;
  private final TeamBillCycleService teamBillCycleService;

  @Override
  public ResultResponse<TeamPolicyManagerDTO> getReleaseByRegionAndType(String sysOrigin,
      String region) {
    return ResultResponse.success(
        teamPolicyManagerClientService.getReleaseByRegionAndType(sysOrigin, region));
  }

  @Override
  public ResultResponse<TeamPolicyManagerDTO> getReleaseByRegionAndType(String sysOrigin, String region, String policyType) {
    return ResultResponse.success(
            teamPolicyManagerClientService.getReleaseByRegionAndTypePolicyType(sysOrigin, region, policyType));
  }

  @Override
  public ResultResponse<Map<String, TeamPolicyManagerDTO>> mapRegionRelease(Set<String> regions) {
    return ResultResponse.success(teamPolicyManagerClientService.mapRegionRelease(regions));
  }

  @Override
  public ResultResponse<List<TeamPolicyManagerDTO>> listSysOriginRegion(String sysOrigin,
      String region, String policyType) {
    return ResultResponse.success(
        teamPolicyManagerClientService.listTeamPolicyAndTypePolicy(sysOrigin, region, policyType));
  }

  @Override
  public ResultResponse<List<TeamPolicyManagerDTO>> listTeamPolicy(String sysOrigin,
      String region) {
    return ResultResponse.success(teamPolicyManagerClientService.listTeamPolicy(sysOrigin, region));
  }


  @Override
  public ResultResponse<Void> add(TeamPolicyManagerCmd cmd) {
    teamPolicyManagerClientService.add(cmd);

    /*List<TeamBillCycle> teamBillCycles = teamBillCycleService.list(new TeamBillCycleBackQryCmd()
            .setRegion(cmd.getRegion())
            .setSysOrigin(cmd.getSysOrigin()));
    TeamPolicyManagerDTO regionPolicyManager = teamPolicyManagerClientService.getReleaseByRegionAndType(
            cmd.getSysOrigin(), cmd.getRegion()
    );

    teamPolicyManagerClientService.getReleaseByRegionAndType(cmd.getSysOrigin(), cmd.getRegion());
    for (TeamBillCycle item : teamBillCycles) {
      teamBillCycleService.updateSelective(new TeamBillCycle()
              .setId(item.getId())
              .setSettleResult(new TeamBillSettleResult()
                      .setPolicy(regionPolicyManager)
                      .setWorkMembers(countWorkMembers(teamMemberTargets))
                      .setFinishMembers(teamBillMemberTargets.size())
                      .setMemberSalary(sumMemberSalary(teamBillMemberTargets))
                      .setOwnSalary(ownSalary(teamBillMemberTargets))
                      .setTotalSalary(ownTotalSalary(teamBillMemberTargets))
              )
      );
    }*/

    return ResultResponse.success();
  }


  @Override
  public ResultResponse<Void> deleteById(Long id) {
    teamPolicyManagerClientService.deleteById(id);
    return ResultResponse.success();
  }
}
