package com.purui.prims.headless.server.rest;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.headless.api.pojo.request.DomainReq;
import com.purui.prims.headless.api.pojo.request.DomainUpdateReq;
import com.purui.prims.headless.api.pojo.response.DomainResp;
import com.purui.prims.headless.server.service.DomainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 主题域API
 *
 * @author charlie
 * @date 2024/7/4
 */
@RestController
@RequestMapping("/api/semantic/domain")
@Api(tags = "主题域管理")
public class DomainController {

    private DomainService domainService;

    public DomainController(DomainService domainService) {
        this.domainService = domainService;
    }

    @ApiOperation(value = "新建主题域")
    @PostMapping("/create")
    public DomainResp createDomain(@RequestBody DomainReq domainReq,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return domainService.createDomain(domainReq, user);
    }

    @ApiOperation(value = "修改主题域")
    @PutMapping("/update")
    public DomainResp updateDomain(@RequestBody DomainUpdateReq domainUpdateReq,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return domainService.updateDomain(domainUpdateReq, user);
    }

    @ApiOperation(value = "删除主题域")
    @DeleteMapping("/remove/{domainId}")
    public Boolean deleteDomain(@PathVariable("domainId") Long domainId) {
        domainService.deleteDomain(domainId);
        return true;
    }

    @ApiOperation(value = "主题域列表")
    @GetMapping("/list")
    public List<DomainResp> getDomainList(HttpServletRequest request,
                                          HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return domainService.getDomainListWithAdminAuth(user);
    }

    @ApiOperation(value = "获取主题域详情")
    @GetMapping("/getDomain/{id}")
    public DomainResp getDomain(@PathVariable("id") Long id) {
        return domainService.getDomain(id);
    }

    @ApiOperation(value = "根据id列表获取主题域详情")
    @GetMapping("/getDomainListByIds/{domainIds}")
    public List<DomainResp> getDomainListByIds(@PathVariable("domainIds") String domainIds) {
        return domainService.getDomainList(Arrays.stream(domainIds.split(",")).map(Long::parseLong)
                                                 .collect(Collectors.toList()));
    }

}
