package com.yingpaipay.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yingpaipay.business.domain.Document;
import com.yingpaipay.business.domain.bo.DocumentSearchBo;
import com.yingpaipay.business.domain.excel.SearchChineseExcel;
import com.yingpaipay.business.domain.excel.SearchEnglishExcel;
import com.yingpaipay.business.domain.excel.SearchExcel;
import com.yingpaipay.business.domain.vo.DocumentVo;
import com.yingpaipay.business.service.IDocumentService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
public class SearchController {

    private final IDocumentService documentService;

    @SaCheckPermission("search:index:list")
    @GetMapping("/list")
    public TableDataInfo<DocumentVo> list(DocumentSearchBo bo, PageQuery pageQuery) {
        return documentService.listPageOnSearch(bo, pageQuery);
    }

    @RepeatSubmit()
    @SaCheckPermission("search:index:export")
    @Log(title = "文档检索", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@RequestBody DocumentSearchBo bo, HttpServletResponse response) {
        List<Document> vos = documentService.listOnSearch(bo);
        List<SearchExcel> excel = vos.stream().map(e -> {
            SearchExcel element = new SearchExcel();
            element.setDocumentName(Arrays.stream(e.getName().split("-")).filter(str -> !str.equals("NA")).collect(Collectors.joining("-")));
            element.setLanguage(e.getLanguage());
            element.setVersion(e.getVersion());
            element.setVersionDate(
                Optional.ofNullable(e.getVersionDate())
                    .map(DateUtils::formatDate)
                    .orElse("-")
            );
            return element;
        }).toList();
        if (LocaleContextHolder.getLocale().getLanguage().equals("zh")) {
            List<SearchChineseExcel> converedExcel = MapstructUtils.convert(excel, SearchChineseExcel.class);
            ExcelUtil.exportExcel(converedExcel, "文件追踪表", SearchChineseExcel.class, response);
        } else {
            List<SearchEnglishExcel> converedExcel = MapstructUtils.convert(excel, SearchEnglishExcel.class);
            ExcelUtil.exportExcel(converedExcel, "Document Tracker", SearchEnglishExcel.class, response);
        }
    }

}
