package com.xyw.console.asset.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.xyw.console.asset.entity.PhoneAssetEntity;
import com.xyw.console.asset.mapper.PhoneAssetMapper;
import com.xyw.console.asset.service.PhoneAssetService;
import com.xyw.console.auth.PagePermissionService;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

class PhoneAssetControllerTest {
  /**
   * 代码作用（白话）：证明浏览器可以通过新增接口创建手机号资产。
   * 关联文件：PhoneAssetController.java、PhoneAssetService.java、PhoneAssetSaveRequest.java。
   * 关联逻辑（调用链/数据流）：POST /api/phone-assets -> Controller -> Service -> Mapper -> as_phone_asset。
   */
  @Test
  void createsPhoneAssetThroughPostEndpoint() throws Exception {
    PhoneAssetMapper mapper = mock(PhoneAssetMapper.class);
    when(mapper.selectCount(any())).thenReturn(0L);
    when(mapper.insert(any(PhoneAssetEntity.class))).thenReturn(1);
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new PhoneAssetController(new PhoneAssetService(mapper), mock(PagePermissionService.class))).build();

    mockMvc.perform(post("/api/phone-assets").contentType(MediaType.APPLICATION_JSON)
            .content("{\"phoneNumber\":\"13812345678\",\"realNameOwner\":\"张三\"}"))
        .andExpect(status().isOk());
  }

  /**
   * 代码作用（白话）：证明列表中的编辑操作可以调用更新接口。
   * 关联文件：PhoneAssetController.java、PhoneAssetService.java、PhoneAssetView.js。
   * 关联逻辑（调用链/数据流）：PUT /api/phone-assets/1 -> Service.update -> Mapper.updateById -> 列表刷新。
   */
  @Test
  void updatesPhoneAssetThroughPutEndpoint() throws Exception {
    PhoneAssetMapper mapper = mock(PhoneAssetMapper.class);
    PhoneAssetEntity entity = activeEntity();
    when(mapper.selectOne(any())).thenReturn(entity);
    when(mapper.selectCount(any())).thenReturn(0L);
    when(mapper.updateById(any(PhoneAssetEntity.class))).thenReturn(1);
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new PhoneAssetController(new PhoneAssetService(mapper), mock(PagePermissionService.class))).build();

    mockMvc.perform(put("/api/phone-assets/1").contentType(MediaType.APPLICATION_JSON)
            .content("{\"phoneNumber\":\"13912345678\",\"realNameOwner\":\"李四\"}"))
        .andExpect(status().isOk());
  }

  /**
   * 代码作用（白话）：证明用户确认后可通过删除接口软删除无关联资产。
   * 关联文件：PhoneAssetController.java、PhoneAssetService.java、PhoneAssetView.js。
   * 关联逻辑（调用链/数据流）：DELETE /api/phone-assets/1 -> Service.softDelete -> Mapper.updateById -> 列表刷新。
   */
  @Test
  void softDeletesPhoneAssetThroughDeleteEndpoint() throws Exception {
    PhoneAssetMapper mapper = mock(PhoneAssetMapper.class);
    when(mapper.selectOne(any())).thenReturn(activeEntity());
    when(mapper.updateById(any(PhoneAssetEntity.class))).thenReturn(1);
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new PhoneAssetController(new PhoneAssetService(mapper), mock(PagePermissionService.class))).build();

    mockMvc.perform(delete("/api/phone-assets/1"))
        .andExpect(status().isOk());
  }

  /**
   * 代码作用（白话）：构造一条仍有效的资产，供编辑和删除接口测试使用。
   * 关联文件：PhoneAssetService.java、PhoneAssetEntity.java。
   * 关联逻辑（调用链/数据流）：测试输入 -> Service 查询有效资产 -> 更新或软删除。
   */
  private PhoneAssetEntity activeEntity() {
    PhoneAssetEntity entity = new PhoneAssetEntity();
    entity.setId(1L);
    entity.setPhoneNumber("13812345678");
    entity.setDeleteTime(0L);
    return entity;
  }
}
