package com.eactive.testmaster.client.controller;

import com.eactive.testmaster.client.dto.ApiCollectionDTO;
import com.eactive.testmaster.client.dto.ApiRequestDTO;
import com.eactive.testmaster.client.entity.ApiCollection;
import com.eactive.testmaster.client.entity.ApiLayout;
import com.eactive.testmaster.client.entity.ApiRequestInfo;
import com.eactive.testmaster.client.mapper.ApiLayoutMapper;
import com.eactive.testmaster.client.mapper.ApiRequestMapper;
import com.eactive.testmaster.client.service.ApiLayoutMgmtService;
import com.eactive.testmaster.client.service.ApiRequestMgmtService;
import com.eactive.testmaster.common.util.SecurityUtil;
import com.eactive.testmaster.user.entity.StaffUser;
import com.eactive.testmaster.user.service.UserService;
import java.util.Collections;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.http.ResponseEntity;
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.RestController;

@RestController
public class ApiRequestMgmtController {

    private static final String SUCCESS = "{\"success\" : true }";
    private final ApiRequestMgmtService apiRequestMgmtService;

    private final ApiLayoutMgmtService apiLayoutMgmtService;

    private final ApiRequestMapper apiRequestMapper;

    private final ApiLayoutMapper apiLayoutMapper;

    private final UserService userService;

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

    @GetMapping("/mgmt/collections/list.do")
    public ResponseEntity<List<ApiCollectionDTO>> listApiRequests() {
        List<ApiCollection> collections = apiRequestMgmtService.findAll();
        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().body(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().body(SUCCESS);
    }

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

    @PostMapping("/mgmt/collections/{id}/apis/save.do")
    @Transactional
    public ResponseEntity<?> 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);

        if (dto.getLayout() != null) {
            dto.getLayout().setApiRequestId(apiId);
            dto.getLayout().setId(apiId);
            apiLayoutMgmtService.saveLayout(apiLayoutMapper.map(dto.getLayout()));
        }
        return ResponseEntity.ok(Collections.singletonMap("apiId", apiId));

    }

    @PostMapping("/mgmt/collections/{id}/apis/delete.do")
    @Transactional
    public ResponseEntity<String> removeApiFromCollection(@PathVariable(name = "id") String id, @RequestBody ApiRequestDTO dto) {
        ApiLayout layout = apiLayoutMgmtService.findLayoutByApiRequest(dto.getId());
        if (layout != null) {
            apiLayoutMgmtService.deleteLayout(layout.getId());
        }
        apiRequestMgmtService.removeApiFromCollection(id, dto.getId());
        return ResponseEntity.ok().body(SUCCESS);
    }

}
