package com.erp.customer.controller;

import com.erp.customer.dto.CustomerCreateRequest;
import com.erp.customer.dto.CustomerUpdateRequest;
import com.erp.customer.entity.Customer;
import com.erp.customer.service.CustomerService;
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/customers")
@RequiredArgsConstructor
public class CustomerController {
    
    private final CustomerService customerService;
    
    /**
     * 获取客户列表
     */
    @GetMapping
    public ResponseEntity<List<Customer>> listCustomers(
            @RequestParam(required = false) Long tenantId,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long salesId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        log.info("查询客户列表: tenantId={}, level={}, status={}, keyword={}, page={}, size={}", 
                tenantId, level, status, keyword, page, size);
        List<Customer> customers = customerService.listCustomers(tenantId, level, status, keyword, salesId, page, size);
        return ResponseEntity.ok(customers);
    }
    
    /**
     * 获取客户详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Customer> getCustomerById(@PathVariable Long id) {
        log.info("查询客户详情: id={}", id);
        Customer customer = customerService.getCustomerById(id);
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 根据客户编码获取
     */
    @GetMapping("/code/{customerCode}")
    public ResponseEntity<Customer> getCustomerByCode(@PathVariable String customerCode) {
        log.info("根据编码查询客户: customerCode={}", customerCode);
        Customer customer = customerService.getCustomerByCode(customerCode);
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 创建客户
     */
    @PostMapping
    public ResponseEntity<Customer> createCustomer(@RequestBody CustomerCreateRequest request) {
        log.info("创建客户: customerName={}, customerCode={}", request.getCustomerName(), request.getCustomerCode());
        Customer customer = customerService.createCustomer(request);
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 更新客户
     */
    @PutMapping("/{id}")
    public ResponseEntity<Customer> updateCustomer(@PathVariable Long id, @RequestBody CustomerUpdateRequest request) {
        log.info("更新客户: id={}", id);
        Customer customer = customerService.updateCustomer(id, request);
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 删除客户
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCustomer(@PathVariable Long id) {
        log.info("删除客户: id={}", id);
        customerService.deleteCustomer(id);
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 切换客户状态
     */
    @PostMapping("/{id}/toggle-status")
    public ResponseEntity<Customer> toggleCustomerStatus(@PathVariable Long id) {
        log.info("切换客户状态: id={}", id);
        Customer customer = customerService.toggleCustomerStatus(id);
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 更新客户等级
     */
    @PostMapping("/{id}/level")
    public ResponseEntity<Customer> updateCustomerLevel(@PathVariable Long id, @RequestParam String level) {
        log.info("更新客户等级: id={}, level={}", id, level);
        Customer customer = customerService.updateCustomerLevel(id, level);
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 获取客户的所有分店
     */
    @GetMapping("/{id}/branches")
    public ResponseEntity<List<Customer>> getCustomerBranches(@PathVariable Long id) {
        log.info("获取客户分店: customerId={}", id);
        List<Customer> branches = customerService.getCustomerBranches(id);
        return ResponseEntity.ok(branches);
    }
    
    /**
     * 获取客户的上级（总店）
     */
    @GetMapping("/{id}/parent")
    public ResponseEntity<Customer> getCustomerParent(@PathVariable Long id) {
        log.info("获取客户上级: customerId={}", id);
        Customer parent = customerService.getCustomerParent(id);
        return ResponseEntity.ok(parent);
    }
    
    /**
     * 获取超时未联系客户
     */
    @GetMapping("/overdue-followup")
    public ResponseEntity<List<Customer>> getLongTimeNoContactCustomers(
            @RequestParam Long tenantId,
            @RequestParam(defaultValue = "7") Integer days) {
        log.info("获取超时未联系客户: tenantId={}, days={}", tenantId, days);
        List<Customer> customers = customerService.getLongTimeNoContactCustomers(tenantId, days);
        return ResponseEntity.ok(customers);
    }
    
    /**
     * 获取VIP客户列表
     */
    @GetMapping("/vip")
    public ResponseEntity<List<Customer>> getTopVipCustomers(
            @RequestParam Long tenantId,
            @RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取VIP客户: tenantId={}, limit={}", tenantId, limit);
        List<Customer> customers = customerService.getTopVipCustomers(tenantId, limit);
        return ResponseEntity.ok(customers);
    }
    
    /**
     * 更新客户余额
     */
    @PostMapping("/{id}/balance")
    public ResponseEntity<Customer> updateCustomerBalance(
            @PathVariable Long id,
            @RequestParam java.math.BigDecimal amount) {
        log.info("更新客户余额: id={}, amount={}", id, amount);
        Customer customer = customerService.updateCustomerBalance(id, amount);
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public ResponseEntity<String> health() {
        return ResponseEntity.ok("Customer Service is healthy");
    }
}
