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.security.CurrentUser;
import com.eactive.httpmockserver.user.entity.StaffUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ApiRequestMgmtController {

    @Autowired
    ApiRequestMgmtService apiRequestMgmtService;

    @Autowired
    ApiRequestMapper apiRequestMapper;

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

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

        ApiCollection collection = apiRequestMapper.map(dto);
        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.updateApiCollection(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,
                                                     @CurrentUser StaffUser user) {
        ApiRequestInfo apiRequestInfo = apiRequestMapper.map(dto);
        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");
    }

}
