package com.izouma.nineth.web;

import com.izouma.nineth.domain.BalanceRecord;
import com.izouma.nineth.domain.ExportWithdraw;
import com.izouma.nineth.domain.UserBalance;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.AutoWithdrawRecordRepo;
import com.izouma.nineth.repo.BalanceRecordRepo;
import com.izouma.nineth.repo.ExportWithdrawRepo;
import com.izouma.nineth.repo.UserBalanceRepo;
import com.izouma.nineth.service.UserBalanceService;
import com.izouma.nineth.utils.DateTimeUtils;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/userBalance")
@AllArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class UserBalanceController extends BaseController {

    private final UserBalanceRepo               userBalanceRepo;
    private final BalanceRecordRepo             balanceRecordRepo;
    private final UserBalanceService            userBalanceService;
    private final ExportWithdrawRepo            exportWithdrawRepo;
    private final RedisTemplate<String, Object> redisTemplate;
    private final AutoWithdrawRecordRepo        autoWithdrawRecordRepo;

    @PostMapping("/all")
    public Page<UserBalance> all(@RequestBody PageQuery pageQuery) {
        return userBalanceRepo.findAll(JpaUtils.toSpecification(pageQuery, UserBalance.class), JpaUtils.toPageRequest(pageQuery));
    }

    @PostMapping("/records")
    public Page<BalanceRecord> records(@RequestBody PageQuery pageQuery) {
        return balanceRecordRepo.findAll(JpaUtils.toSpecification(pageQuery, BalanceRecord.class), JpaUtils.toPageRequest(pageQuery));
    }

    @PostMapping("/settle")
    public void settle(@RequestParam LocalDate start, @RequestParam LocalDate end) {
        userBalanceService.settle(start, end);
    }

    @PostMapping(value = "/exportWithdraw")
    public void exportWithdraw(String remark) throws IOException, InvalidFormatException {
        userBalanceService.exportWithdrawAsync(remark);
    }

    @PostMapping(value = "/exportHistory")
    public Page<ExportWithdraw> exportHistory(Pageable pageable) {
        return exportWithdrawRepo.findAll(pageable);
    }

    @PostMapping("/importFail")
    public void importFail(@RequestPart("withdrawList") MultipartFile withdrawFile,
                           @RequestPart("failList") MultipartFile failFile) throws IOException {
        userBalanceService.importFail(withdrawFile, failFile);
    }

    @PostMapping("/autoWithdraw")
    public void autoWithdraw() throws ExecutionException, InterruptedException {

        LocalDate date = LocalDate.now();
        autoWithdrawRecordRepo.findByDate(date).ifPresent(a -> {
            throw new BusinessException("今日已经提现过");
        });

        userBalanceService.autoWithdraw(date);
    }
}
