package org.springblade.modules.martial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.martial.pojo.entity.MartialContact;
import org.springblade.modules.martial.service.IMartialContactService;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/martial/contact")
@Tag(name = "联系人管理", description = "联系人接口")
public class MartialContactController extends BladeController {

	private final IMartialContactService contactService;

	@GetMapping("/list")
	@Operation(summary = "分页列表", description = "获取当前用户的联系人列表")
	public R<IPage<MartialContact>> list(Query query) {
		Long userId = AuthUtil.getUserId();
		IPage<MartialContact> pages = contactService.getContactList(userId, query.getCurrent(), query.getSize());
		return R.data(pages);
	}

	@GetMapping("/detail")
	@Operation(summary = "详情", description = "获取联系人详情")
	public R<MartialContact> detail(@RequestParam Long id) {
		return R.data(contactService.getContactDetail(id));
	}

	@PostMapping("/submit")
	@Operation(summary = "保存", description = "新增或修改联系人")
	public R<Boolean> submit(@RequestBody MartialContact contact) {
		Long userId = AuthUtil.getUserId();
		log.info("Contact submit - id: {}, name: {}, userId: {}, isDefault: {}", 
			contact.getId(), contact.getName(), userId, contact.getIsDefault());
		
		return R.data(contactService.saveContact(contact, userId));
	}

	@PostMapping("/remove")
	@Operation(summary = "删除", description = "删除联系人")
	public R<Boolean> remove(@RequestParam String ids) {
		return R.data(contactService.removeByIds(Func.toLongList(ids)));
	}

}
