package cn.cordys.crm.customer.controller;

import cn.cordys.common.util.Translator;
import cn.cordys.crm.base.BaseTest;
import cn.cordys.crm.customer.domain.CustomerCapacity;
import cn.cordys.crm.system.dto.request.CapacityAddRequest;
import cn.cordys.crm.system.dto.request.CapacityUpdateRequest;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MvcResult;

import java.util.List;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CustomerCapacityControllerTests extends BaseTest {

    private static String CAPACITY_ID;

    @Test
    @Order(1)
    void add() throws Exception {
        CapacityAddRequest request = new CapacityAddRequest();
        request.setScopeIds(List.of("admin"));
        request.setCapacity(10);
        this.requestPostWithOk("/account-capacity/add", request);
        MvcResult mvcResult = this.requestPost("/account-capacity/add", request).andExpect(status().is5xxServerError()).andReturn();
        assert mvcResult.getResponse().getContentAsString().contains(Translator.get("capacity.scope.duplicate"));
    }

    @Test
    @Order(2)
    void page() throws Exception {
        MvcResult mvcResult = this.requestGetWithOkAndReturn("/account-capacity/get");
        List<CustomerCapacity> result = getResultDataArray(mvcResult, CustomerCapacity.class);
        assert result.size() == 1;
        CAPACITY_ID = result.getFirst().getId();
    }

    @Test
    @Order(3)
    void update() throws Exception {
        CapacityUpdateRequest request = new CapacityUpdateRequest();
        request.setId("not-exist");
        request.setScopeIds(List.of("admin"));
        request.setCapacity(100);
        MvcResult mvcResult = this.requestPost("/account-capacity/update", request).andExpect(status().is5xxServerError()).andReturn();
        assert mvcResult.getResponse().getContentAsString().contains(Translator.get("capacity.not.exist"));
        request.setId(CAPACITY_ID);
        this.requestPost("/account-capacity/update", request);
    }

    @Test
    @Order(4)
    void delete() throws Exception {
        this.requestGetWithOk("/account-capacity/delete/" + CAPACITY_ID);
    }
}
