package com.erp.customer.controller;

import com.erp.customer.entity.CustomerContact;
import com.erp.customer.service.CustomerContactService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户联系人控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/customer-contacts")
@RequiredArgsConstructor
public class CustomerContactController {
    
    private final CustomerContactService contactService;
    
    /**
     * 获取客户的所有联系人
     */
    @GetMapping("/customer/{customerId}")
    public ResponseEntity<List<CustomerContact>> getContactsByCustomerId(@PathVariable Long customerId) {
        log.info("获取客户联系人: customerId={}", customerId);
        List<CustomerContact> contacts = contactService.getContactsByCustomerId(customerId);
        return ResponseEntity.ok(contacts);
    }
    
    /**
     * 获取客户的主要联系人
     */
    @GetMapping("/customer/{customerId}/primary")
    public ResponseEntity<CustomerContact> getPrimaryContact(@PathVariable Long customerId) {
        log.info("获取主要联系人: customerId={}", customerId);
        CustomerContact contact = contactService.getPrimaryContact(customerId);
        return ResponseEntity.ok(contact);
    }
    
    /**
     * 获取联系人详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<CustomerContact> getContactById(@PathVariable Long id) {
        log.info("查询联系人详情: id={}", id);
        CustomerContact contact = contactService.getContactById(id);
        return ResponseEntity.ok(contact);
    }
    
    /**
     * 创建联系人
     */
    @PostMapping
    public ResponseEntity<CustomerContact> createContact(
            @RequestParam Long customerId,
            @RequestBody CustomerContact contact) {
        log.info("创建联系人: customerId={}, contactName={}", customerId, contact.getContactName());
        CustomerContact created = contactService.createContact(customerId, contact);
        return ResponseEntity.ok(created);
    }
    
    /**
     * 更新联系人
     */
    @PutMapping("/{id}")
    public ResponseEntity<CustomerContact> updateContact(
            @PathVariable Long id, 
            @RequestBody CustomerContact contact) {
        log.info("更新联系人: id={}", id);
        CustomerContact updated = contactService.updateContact(id, contact);
        return ResponseEntity.ok(updated);
    }
    
    /**
     * 删除联系人
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteContact(@PathVariable Long id) {
        log.info("删除联系人: id={}", id);
        contactService.deleteContact(id);
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 设置为主要联系人
     */
    @PostMapping("/{id}/set-primary")
    public ResponseEntity<CustomerContact> setPrimaryContact(@PathVariable Long id) {
        log.info("设置主要联系人: id={}", id);
        CustomerContact contact = contactService.setPrimaryContact(id);
        return ResponseEntity.ok(contact);
    }
    
    /**
     * 根据手机号查询
     */
    @GetMapping("/mobile/{mobile}")
    public ResponseEntity<CustomerContact> getContactByMobile(@PathVariable String mobile) {
        log.info("根据手机号查询: mobile={}", mobile);
        CustomerContact contact = contactService.getContactByMobile(mobile);
        return ResponseEntity.ok(contact);
    }
    
    /**
     * 根据邮箱查询
     */
    @GetMapping("/email/{email}")
    public ResponseEntity<CustomerContact> getContactByEmail(@PathVariable String email) {
        log.info("根据邮箱查询: email={}", email);
        CustomerContact contact = contactService.getContactByEmail(email);
        return ResponseEntity.ok(contact);
    }
    
    /**
     * 统计客户联系人数量
     */
    @GetMapping("/customer/{customerId}/count")
    public ResponseEntity<Long> countContacts(@PathVariable Long customerId) {
        log.info("统计联系人数量: customerId={}", customerId);
        Long count = contactService.countContactsByCustomerId(customerId);
        return ResponseEntity.ok(count);
    }
    
    /**
     * 获取高影响力联系人
     */
    @GetMapping("/customer/{customerId}/high-influence")
    public ResponseEntity<List<CustomerContact>> getHighInfluenceContacts(@PathVariable Long customerId) {
        log.info("获取高影响力联系人: customerId={}", customerId);
        List<CustomerContact> contacts = contactService.getHighInfluenceContacts(customerId);
        return ResponseEntity.ok(contacts);
    }
}
