package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadDispatchParam;
import com.gxwebsoft.cms.param.LeadFollowParam;
import com.gxwebsoft.cms.service.CustomerLeadService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 客资管理控制器
 *
 * @author 科技小王子
 * @since 2026-04-14
 */
@Slf4j
@RestController
@RequestMapping("/customer/lead")
@Tag(name = "客资管理", description = "客资管理相关接口")
public class CustomerLeadController extends BaseController {

    @Autowired
    private CustomerLeadService customerLeadService;

    @Operation(summary = "分页查询客资列表")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('customer:lead:page') or @pms.hasPermission('customer:lead:view')")
    public ApiResult<Map<String, Object>> getLeadPage(CustomerLeadParam param) {
        return success(customerLeadService.getLeadPage(param));
    }

    @Operation(summary = "获取客资详情")
    @GetMapping("/detail/{leadId}")
    @PreAuthorize("@pms.hasPermission('customer:lead:detail') or @pms.hasPermission('customer:lead:view')")
    public ApiResult<?> getLeadDetail(
            @Parameter(description = "客资ID") @PathVariable Integer leadId) {
        return success(customerLeadService.getLeadDetail(leadId));
    }

    @Operation(summary = "创建客资（管理员录入）")
    @PostMapping("/create")
    @PreAuthorize("@pms.hasPermission('customer:lead:create')")
    public ApiResult<?> createLead(@RequestBody CustomerLeadParam param) {
        return success(customerLeadService.createLead(param));
    }

    @Operation(summary = "更新客资信息")
    @PutMapping("/update")
    @PreAuthorize("@pms.hasPermission('customer:lead:update')")
    public ApiResult<?> updateLead(@RequestBody CustomerLeadParam param) {
        return success(customerLeadService.updateLead(param));
    }

    @Operation(summary = "更新客资状态")
    @PutMapping("/status/{leadId}")
    @PreAuthorize("@pms.hasPermission('customer:lead:status')")
    public ApiResult<?> updateLeadStatus(
            @Parameter(description = "客资ID") @PathVariable Integer leadId,
            @Parameter(description = "状态") @RequestParam Integer status,
            @Parameter(description = "备注") @RequestParam(required = false) String remarks) {
        return success(customerLeadService.updateLeadStatus(leadId, status, remarks));
    }

    @Operation(summary = "派单给业务员")
    @PostMapping("/dispatch")
    @PreAuthorize("@pms.hasPermission('customer:lead:dispatch')")
    public ApiResult<?> dispatchLead(@RequestBody LeadDispatchParam param) {
        return success(customerLeadService.dispatchLead(param));
    }

    @Operation(summary = "批量派单")
    @PostMapping("/dispatch/batch")
    @PreAuthorize("@pms.hasPermission('customer:lead:dispatch')")
    public ApiResult<?> batchDispatchLeads(@RequestBody LeadDispatchParam param) {
        return success(customerLeadService.batchDispatchLeads(param));
    }

    @Operation(summary = "添加跟进记录")
    @PostMapping("/follow")
    @PreAuthorize("@pms.hasPermission('customer:lead:follow')")
    public ApiResult<?> addFollowLog(@RequestBody LeadFollowParam param) {
        return success(customerLeadService.addFollowLog(param));
    }

    @Operation(summary = "获取跟进历史")
    @GetMapping("/follow/history/{leadId}")
    @PreAuthorize("@pms.hasPermission('customer:lead:view')")
    public ApiResult<?> getFollowHistory(
            @Parameter(description = "客资ID") @PathVariable Integer leadId) {
        return success(customerLeadService.getFollowHistory(leadId));
    }

    @Operation(summary = "获取统计数据")
    @GetMapping("/statistics")
    @PreAuthorize("@pms.hasPermission('customer:lead:statistics')")
    public ApiResult<?> getStatistics(CustomerLeadParam param) {
        return success(customerLeadService.getStatistics(param));
    }

    @Operation(summary = "导出客资数据")
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('customer:lead:export')")
    public ApiResult<?> exportLeads(CustomerLeadParam param) {
        List<Map<String, Object>> data = customerLeadService.exportLeads(param);
        return success(data);
    }

    @Operation(summary = "获取未分配客资")
    @GetMapping("/unassigned")
    @PreAuthorize("@pms.hasPermission('customer:lead:view')")
    public ApiResult<?> getUnassignedLeads() {
        return success(customerLeadService.getUnassignedLeads());
    }

    @Operation(summary = "获取业务员客资统计")
    @GetMapping("/salesman/stats/{salesmanId}")
    @PreAuthorize("@pms.hasPermission('customer:lead:view')")
    public ApiResult<?> getSalesmanLeadStats(
            @Parameter(description = "业务员ID") @PathVariable Integer salesmanId) {
        return success(customerLeadService.getSalesmanLeadStats(salesmanId));
    }
}
