package com.eshore.gringotts.web.domain.controller;

import com.eshore.gringotts.web.configuration.amis.AmisResponse;
import com.eshore.gringotts.web.domain.base.controller.ListController;
import com.eshore.gringotts.web.domain.base.controller.query.Query;
import com.eshore.gringotts.web.domain.entity.CheckOrder;
import com.eshore.gringotts.web.domain.service.CheckOrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDateTime;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 监管
 *
 * @author lanyuanxiaoyao
 * @date 2024-11-28
 */
@Slf4j
@RestController
@RequestMapping("check_order")
public class CheckOrderController implements ListController<CheckOrderController.ListItem> {
    private final CheckOrderService checkOrderService;
    private final ObjectMapper mapper;

    public CheckOrderController(CheckOrderService checkOrderService, Jackson2ObjectMapperBuilder builder) {
        this.checkOrderService = checkOrderService;
        this.mapper = builder.build();
    }

    @GetMapping("/list")
    @Override
    public AmisResponse<ImmutableList<CheckOrderController.ListItem>> list() throws Exception {
        return AmisResponse.responseSuccess(checkOrderService.list().collect(this::toListItem));
    }

    @PostMapping("/list")
    @Override
    public AmisResponse<ImmutableList<ListItem>> list(@RequestBody Query query) throws Exception {
        return AmisResponse.responseSuccess(checkOrderService.list().collect(this::toListItem));
    }

    @SneakyThrows
    private ListItem toListItem(CheckOrder order) {
        ListItem item = new ListItem();
        item.setCheckOrderId(order.getId());
        item.setDescription(order.getDescription());
        item.setType(order.getType());
        item.setParameters(mapper.readValue(order.getParameters(), new TypeReference<>() {}));
        item.setState(order.getState());
        item.setCreatedUsername(order.getCreatedUser().getUsername());
        item.setCreatedTime(order.getCreatedTime());
        item.setModifiedUsername(order.getModifiedUser().getUsername());
        item.setModifiedTime(order.getModifiedTime());
        return item;
    }

    @GetMapping("/operation/{id}/{operation}")
    public AmisResponse<Object> operation(@PathVariable Long id, @PathVariable CheckOrder.Operation operation) throws JsonProcessingException {
        log.info("id:{}, operation:{}", id, operation);
        checkOrderService.operation(id, operation);
        return AmisResponse.responseSuccess();
    }

    @Data
    public static final class ListItem {
        private Long checkOrderId;
        private String description;
        private CheckOrder.Type type;
        private ImmutableMap<String, Object> parameters;
        private CheckOrder.State state;
        private LocalDateTime createdTime;
        private String createdUsername;
        private LocalDateTime modifiedTime;
        private String modifiedUsername;
    }
}
