package com.erp.customer.controller;

import com.erp.customer.dto.CustomerRelationshipRequest;
import com.erp.customer.entity.CustomerRelationship;
import com.erp.customer.service.CustomerRelationshipService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户关系控制器（8级客户关系）
 */
@Slf4j
@RestController
@RequestMapping("/api/customer-relationships")
@RequiredArgsConstructor
public class CustomerRelationshipController {
    
    private final CustomerRelationshipService relationshipService;
    
    /**
     * 获取客户的所有关系
     */
    @GetMapping("/customer/{customerId}")
    public ResponseEntity<List<CustomerRelationship>> getRelationshipsByCustomerId(@PathVariable Long customerId) {
        log.info("获取客户关系: customerId={}", customerId);
        List<CustomerRelationship> relationships = relationshipService.getRelationshipsByCustomerId(customerId);
        return ResponseEntity.ok(relationships);
    }
    
    /**
     * 获取关系详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<CustomerRelationship> getRelationshipById(@PathVariable Long id) {
        log.info("查询关系详情: id={}", id);
        CustomerRelationship relationship = relationshipService.getRelationshipById(id);
        return ResponseEntity.ok(relationship);
    }
    
    /**
     * 创建客户关系
     */
    @PostMapping
    public ResponseEntity<CustomerRelationship> createRelationship(@RequestBody CustomerRelationshipRequest request) {
        log.info("创建客户关系: customerId={}, parentId={}, level={}", 
                request.getCustomerId(), request.getParentCustomerId(), request.getRelationshipLevel());
        CustomerRelationship relationship = relationshipService.createRelationship(request);
        return ResponseEntity.ok(relationship);
    }
    
    /**
     * 更新客户关系
     */
    @PutMapping("/{id}")
    public ResponseEntity<CustomerRelationship> updateRelationship(
            @PathVariable Long id, 
            @RequestBody CustomerRelationshipRequest request) {
        log.info("更新客户关系: id={}", id);
        CustomerRelationship relationship = relationshipService.updateRelationship(id, request);
        return ResponseEntity.ok(relationship);
    }
    
    /**
     * 删除客户关系
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRelationship(@PathVariable Long id) {
        log.info("删除客户关系: id={}", id);
        relationshipService.deleteRelationship(id);
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 解除客户关系
     */
    @PostMapping("/{id}/terminate")
    public ResponseEntity<CustomerRelationship> terminateRelationship(@PathVariable Long id) {
        log.info("解除客户关系: id={}", id);
        CustomerRelationship relationship = relationshipService.terminateRelationship(id);
        return ResponseEntity.ok(relationship);
    }
    
    /**
     * 获取客户的所有分店
     */
    @GetMapping("/customer/{customerId}/branches")
    public ResponseEntity<List<CustomerRelationship>> getBranchesByCustomerId(@PathVariable Long customerId) {
        log.info("获取客户分店: customerId={}", customerId);
        List<CustomerRelationship> branches = relationshipService.getBranchesByCustomerId(customerId);
        return ResponseEntity.ok(branches);
    }
    
    /**
     * 获取总店的所有分店（所有层级）
     */
    @GetMapping("/headquarters/{headquartersId}/all-branches")
    public ResponseEntity<List<CustomerRelationship>> getAllBranchesByHeadquartersId(@PathVariable Long headquartersId) {
        log.info("获取总店所有分店: headquartersId={}", headquartersId);
        List<CustomerRelationship> branches = relationshipService.getAllBranchesByHeadquartersId(headquartersId);
        return ResponseEntity.ok(branches);
    }
    
    /**
     * 获取客户的上级
     */
    @GetMapping("/customer/{customerId}/parent")
    public ResponseEntity<CustomerRelationship> getParentByCustomerId(@PathVariable Long customerId) {
        log.info("获取客户上级: customerId={}", customerId);
        CustomerRelationship parent = relationshipService.getParentByCustomerId(customerId);
        return ResponseEntity.ok(parent);
    }
    
    /**
     * 获取客户直接上级
     */
    @GetMapping("/customer/{customerId}/direct-parent")
    public ResponseEntity<CustomerRelationship> getDirectParentByCustomerId(@PathVariable Long customerId) {
        log.info("获取客户直接上级: customerId={}", customerId);
        CustomerRelationship parent = relationshipService.getDirectParentByCustomerId(customerId);
        return ResponseEntity.ok(parent);
    }
    
    /**
     * 查询所有总店
     */
    @GetMapping("/headquarters")
    public ResponseEntity<List<CustomerRelationship>> getAllHeadquarters(@RequestParam Long tenantId) {
        log.info("查询所有总店: tenantId={}", tenantId);
        List<CustomerRelationship> headquarters = relationshipService.getAllHeadquarters(tenantId);
        return ResponseEntity.ok(headquarters);
    }
    
    /**
     * 根据集团名称查询
     */
    @GetMapping("/group")
    public ResponseEntity<List<CustomerRelationship>> getByGroupName(
            @RequestParam Long tenantId,
            @RequestParam String groupName) {
        log.info("根据集团查询: tenantId={}, groupName={}", tenantId, groupName);
        List<CustomerRelationship> relationships = relationshipService.getByGroupName(tenantId, groupName);
        return ResponseEntity.ok(relationships);
    }
    
    /**
     * 统计分店数量
     */
    @GetMapping("/customer/{customerId}/branch-count")
    public ResponseEntity<Long> countBranches(@PathVariable Long customerId) {
        log.info("统计分店数量: customerId={}", customerId);
        Long count = relationshipService.countBranchesByCustomerId(customerId);
        return ResponseEntity.ok(count);
    }
    
    /**
     * 获取客户的所有关联关系
     */
    @GetMapping("/customer/{customerId}/all-related")
    public ResponseEntity<List<CustomerRelationship>> getAllRelatedRelationships(@PathVariable Long customerId) {
        log.info("获取所有关联关系: customerId={}", customerId);
        List<CustomerRelationship> relationships = relationshipService.getAllRelatedRelationships(customerId);
        return ResponseEntity.ok(relationships);
    }
    
    /**
     * 转移客户到新的父客户
     */
    @PostMapping("/transfer")
    public ResponseEntity<CustomerRelationship> transferCustomer(
            @RequestParam Long customerId,
            @RequestParam Long newParentId,
            @RequestParam Integer newLevel) {
        log.info("转移客户: customerId={}, newParentId={}, newLevel={}", customerId, newParentId, newLevel);
        CustomerRelationship relationship = relationshipService.transferCustomer(customerId, newParentId, newLevel);
        return ResponseEntity.ok(relationship);
    }
}
