package com.eactive.httpmockserver.client.controller;

import com.eactive.httpmockserver.client.dto.ApiCollectionDTO;
import com.eactive.httpmockserver.client.dto.ApiRequestDTO;
import com.eactive.httpmockserver.client.entity.ApiCollection;
import com.eactive.httpmockserver.client.entity.ApiRequestInfo;
import com.eactive.httpmockserver.client.mapper.ApiRequestMapper;
import com.eactive.httpmockserver.client.service.ApiRequestMgmtService;
import com.eactive.httpmockserver.common.util.SecurityUtil;
import com.eactive.httpmockserver.user.entity.StaffUser;
import com.eactive.httpmockserver.user.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ApiRequestMgmtController {

    private static final String SUCCESS = "success";
    private final ApiRequestMgmtService apiRequestMgmtService;

    private final ApiRequestMapper apiRequestMapper;

    private final UserService userService;

    public ApiRequestMgmtController(ApiRequestMgmtService apiRequestMgmtService, ApiRequestMapper apiRequestMapper, UserService userService) {
        this.apiRequestMgmtService = apiRequestMgmtService;
        this.apiRequestMapper = apiRequestMapper;
        this.userService = userService;
    }

    @GetMapping("/mgmt/collections/list.do")
    public ResponseEntity<List<ApiCollectionDTO>> listApiRequests() {
        StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
        List<ApiCollection> collections = apiRequestMgmtService.getMyApis(user);
        return ResponseEntity.ok(apiRequestMapper.mapCollections(collections));
    }

    @PostMapping("/mgmt/collections/create.do")
    public ResponseEntity<String> createApiCollection(
            @RequestBody ApiCollectionDTO dto) {

        ApiCollection collection = apiRequestMapper.map(dto);
        StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
        collection.setOwner(user);

        apiRequestMgmtService.createNewApiCollection(collection);
        return ResponseEntity.ok(SUCCESS);
    }


    @PostMapping("/mgmt/collections/update.do")
    public ResponseEntity<String> updateApiCollection(@RequestBody ApiCollectionDTO dto) {
        ApiCollection updated = apiRequestMapper.map(dto);
        apiRequestMgmtService.updateApiCollectionName(updated.getId(), updated);
        return ResponseEntity.ok(SUCCESS);
    }

    @PostMapping("/mgmt/collections/delete.do")
    public ResponseEntity<String> deleteApiCollection(@RequestBody ApiCollectionDTO dto) {
        apiRequestMgmtService.deleteApiCollection(dto.getId());
        return ResponseEntity.ok(SUCCESS);
    }

    @PostMapping("/mgmt/collections/{id}/apis/save.do")
    public ResponseEntity<String> addApiToCollection(@PathVariable(name = "id") String id,
                                                     @RequestBody ApiRequestDTO dto) {
        ApiRequestInfo apiRequestInfo = apiRequestMapper.map(dto);
        StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
        apiRequestInfo.setOwner(user);
        String apiId = apiRequestMgmtService.saveApiToCollection(id, apiRequestInfo);
        return ResponseEntity.ok(apiId);
    }

    @PostMapping("/mgmt/collections/{id}/apis/delete.do")
    public ResponseEntity<String> removeApiFromCollection(@PathVariable(name = "id") String id,
                                                          @RequestBody ApiRequestDTO dto) {
        apiRequestMgmtService.removeApiFromCollection(id, dto.getId());
        return ResponseEntity.ok(SUCCESS);
    }

}
