package com.erp.customer.controller;

import com.erp.customer.dto.CustomerFollowUpRequest;
import com.erp.customer.entity.CustomerFollowUp;
import com.erp.customer.service.CustomerFollowUpService;
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-followups")
@RequiredArgsConstructor
public class CustomerFollowUpController {
    
    private final CustomerFollowUpService followUpService;
    
    /**
     * 获取跟进记录列表
     */
    @GetMapping
    public ResponseEntity<List<CustomerFollowUp>> listFollowUps(
            @RequestParam(required = false) Long tenantId,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) String followType,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        log.info("查询跟进记录: tenantId={}, customerId={}, followType={}, status={}", 
                tenantId, customerId, followType, status);
        List<CustomerFollowUp> followUps = followUpService.listFollowUps(
                tenantId, customerId, followType, userId, status, page, size);
        return ResponseEntity.ok(followUps);
    }
    
    /**
     * 获取跟进记录详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<CustomerFollowUp> getFollowUpById(@PathVariable Long id) {
        log.info("查询跟进记录详情: id={}", id);
        CustomerFollowUp followUp = followUpService.getFollowUpById(id);
        return ResponseEntity.ok(followUp);
    }
    
    /**
     * 创建跟进记录
     */
    @PostMapping
    public ResponseEntity<CustomerFollowUp> createFollowUp(@RequestBody CustomerFollowUpRequest request) {
        log.info("创建跟进记录: customerId={}, followType={}", request.getCustomerId(), request.getFollowType());
        CustomerFollowUp followUp = followUpService.createFollowUp(request);
        return ResponseEntity.ok(followUp);
    }
    
    /**
     * 更新跟进记录
     */
    @PutMapping("/{id}")
    public ResponseEntity<CustomerFollowUp> updateFollowUp(
            @PathVariable Long id, 
            @RequestBody CustomerFollowUpRequest request) {
        log.info("更新跟进记录: id={}", id);
        CustomerFollowUp followUp = followUpService.updateFollowUp(id, request);
        return ResponseEntity.ok(followUp);
    }
    
    /**
     * 删除跟进记录
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFollowUp(@PathVariable Long id) {
        log.info("删除跟进记录: id={}", id);
        followUpService.deleteFollowUp(id);
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 完成跟进记录
     */
    @PostMapping("/{id}/complete")
    public ResponseEntity<CustomerFollowUp> completeFollowUp(@PathVariable Long id) {
        log.info("完成跟进记录: id={}", id);
        CustomerFollowUp followUp = followUpService.completeFollowUp(id);
        return ResponseEntity.ok(followUp);
    }
    
    /**
     * 取消跟进记录
     */
    @PostMapping("/{id}/cancel")
    public ResponseEntity<CustomerFollowUp> cancelFollowUp(@PathVariable Long id) {
        log.info("取消跟进记录: id={}", id);
        CustomerFollowUp followUp = followUpService.cancelFollowUp(id);
        return ResponseEntity.ok(followUp);
    }
    
    /**
     * 获取客户的跟进记录
     */
    @GetMapping("/customer/{customerId}")
    public ResponseEntity<List<CustomerFollowUp>> getFollowUpsByCustomerId(
            @PathVariable Long customerId,
            @RequestParam(defaultValue = "20") Integer limit) {
        log.info("获取客户跟进记录: customerId={}, limit={}", customerId, limit);
        List<CustomerFollowUp> followUps = followUpService.getFollowUpsByCustomerId(customerId, limit);
        return ResponseEntity.ok(followUps);
    }
    
    /**
     * 获取最近跟进记录
     */
    @GetMapping("/customer/{customerId}/latest")
    public ResponseEntity<CustomerFollowUp> getLatestFollowUp(@PathVariable Long customerId) {
        log.info("获取最近跟进记录: customerId={}", customerId);
        CustomerFollowUp followUp = followUpService.getLatestFollowUp(customerId);
        return ResponseEntity.ok(followUp);
    }
    
    /**
     * 获取待跟进列表
     */
    @GetMapping("/pending")
    public ResponseEntity<List<CustomerFollowUp>> getPendingFollowUps(@RequestParam Long tenantId) {
        log.info("获取待跟进列表: tenantId={}", tenantId);
        List<CustomerFollowUp> followUps = followUpService.getPendingFollowUps(tenantId);
        return ResponseEntity.ok(followUps);
    }
    
    /**
     * 获取今日待跟进
     */
    @GetMapping("/today")
    public ResponseEntity<List<CustomerFollowUp>> getTodayFollowUps(@RequestParam Long tenantId) {
        log.info("获取今日待跟进: tenantId={}", tenantId);
        List<CustomerFollowUp> followUps = followUpService.getTodayFollowUps(tenantId);
        return ResponseEntity.ok(followUps);
    }
    
    /**
     * 统计客户跟进次数
     */
    @GetMapping("/customer/{customerId}/count")
    public ResponseEntity<Long> countFollowUps(@PathVariable Long customerId) {
        log.info("统计跟进次数: customerId={}", customerId);
        Long count = followUpService.countFollowUpsByCustomerId(customerId);
        return ResponseEntity.ok(count);
    }
}
