package org.hzhub.erp.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.hzhub.erp.common.core.BaseController;
import org.hzhub.erp.common.domain.R;
import org.hzhub.erp.common.page.TableDataInfo;
import org.hzhub.erp.domain.vo.CustomerVO;
import org.hzhub.erp.service.ICustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户档案 API（已废弃）
 *
 * @deprecated 已迁移到动态API系统，请使用：
 *   - /erp/dynamic/v1/customer/list 替代 /erp/customer/list
 *   - /erp/dynamic/v1/customer/detail 替代 /erp/customer/{customerCode}
 *   - /erp/dynamic/v1/customer/sales-areas 替代 /erp/customer/sales-areas
 *   - /erp/dynamic/v1/customer/brands 替代 /erp/customer/brands
 *
 * @author HZHub Team
 * @since 2026-04-21
 * @deprecated since 2026-04-30, will be removed in 2026-07-30
 */
@Deprecated(since = "2026-04-30", forRemoval = true)
@SaIgnore
@RestController
@RequestMapping("/erp/customer")
@RequiredArgsConstructor
public class CustomerController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CustomerController.class);
    private final ICustomerService customerService;

    /**
     * 分页查询客户列表
     * @deprecated 请使用动态API: /erp/dynamic/v1/customer/list
     */
    @GetMapping("/list")
    public TableDataInfo<CustomerVO> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String companyCode,
            @RequestParam(required = false) String salesAreaCode,
            @RequestParam(required = false) String brand) {
        log.warn("⚠️  已废弃API被调用: /erp/customer/list，请迁移到动态API: /erp/dynamic/v1/customer/list");
        return customerService.queryCustomerList(pageNum, pageSize, keyword, companyCode, salesAreaCode, brand);
    }

    /**
     * 获取客户详情
     */
    @GetMapping("/{customerCode}")
    public R<CustomerVO> detail(@PathVariable String customerCode) {
        CustomerVO vo = customerService.getCustomerDetail(customerCode);
        if (vo == null) {
            return R.fail("客户不存在");
        }
        return R.ok(vo);
    }

    /**
     * 获取所有销区列表
     */
    @GetMapping("/sales-areas")
    public R<List<CustomerVO>> salesAreas() {
        return R.ok(customerService.getSalesAreas());
    }

    /**
     * 获取所有品牌列表
     */
    @GetMapping("/brands")
    public R<List<CustomerVO>> brands() {
        return R.ok(customerService.getBrands());
    }

    /**
     * 客户选择列表（用于CRM选择器）
     * 支持按公司过滤（companyIds逗号分隔）
     */
    @GetMapping("/select")
    public TableDataInfo<CustomerVO> select(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String companyIds) {
        return customerService.queryCustomerSelectList(pageNum, pageSize, keyword, companyIds);
    }
}
