package com.erp.customer.controller;

import com.erp.customer.entity.CustomerAddress;
import com.erp.customer.service.CustomerAddressService;
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-addresses")
@RequiredArgsConstructor
public class CustomerAddressController {
    
    private final CustomerAddressService addressService;
    
    /**
     * 获取客户的所有地址
     */
    @GetMapping("/customer/{customerId}")
    public ResponseEntity<List<CustomerAddress>> getAddressesByCustomerId(@PathVariable Long customerId) {
        log.info("获取客户地址: customerId={}", customerId);
        List<CustomerAddress> addresses = addressService.getAddressesByCustomerId(customerId);
        return ResponseEntity.ok(addresses);
    }
    
    /**
     * 获取客户默认地址
     */
    @GetMapping("/customer/{customerId}/default")
    public ResponseEntity<CustomerAddress> getDefaultAddress(@PathVariable Long customerId) {
        log.info("获取默认地址: customerId={}", customerId);
        CustomerAddress address = addressService.getDefaultAddress(customerId);
        return ResponseEntity.ok(address);
    }
    
    /**
     * 获取地址详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<CustomerAddress> getAddressById(@PathVariable Long id) {
        log.info("查询地址详情: id={}", id);
        CustomerAddress address = addressService.getAddressById(id);
        return ResponseEntity.ok(address);
    }
    
    /**
     * 创建地址
     */
    @PostMapping
    public ResponseEntity<CustomerAddress> createAddress(
            @RequestParam Long customerId,
            @RequestBody CustomerAddress address) {
        log.info("创建地址: customerId={}, fullAddress={}", customerId, address.getFullAddress());
        CustomerAddress created = addressService.createAddress(customerId, address);
        return ResponseEntity.ok(created);
    }
    
    /**
     * 更新地址
     */
    @PutMapping("/{id}")
    public ResponseEntity<CustomerAddress> updateAddress(
            @PathVariable Long id, 
            @RequestBody CustomerAddress address) {
        log.info("更新地址: id={}", id);
        CustomerAddress updated = addressService.updateAddress(id, address);
        return ResponseEntity.ok(updated);
    }
    
    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAddress(@PathVariable Long id) {
        log.info("删除地址: id={}", id);
        addressService.deleteAddress(id);
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 设置为默认地址
     */
    @PostMapping("/{id}/set-default")
    public ResponseEntity<CustomerAddress> setDefaultAddress(@PathVariable Long id) {
        log.info("设置默认地址: id={}", id);
        CustomerAddress address = addressService.setDefaultAddress(id);
        return ResponseEntity.ok(address);
    }
    
    /**
     * 获取发货地址
     */
    @GetMapping("/customer/{customerId}/delivery")
    public ResponseEntity<List<CustomerAddress>> getDeliveryAddresses(@PathVariable Long customerId) {
        log.info("获取发货地址: customerId={}", customerId);
        List<CustomerAddress> addresses = addressService.getDeliveryAddresses(customerId);
        return ResponseEntity.ok(addresses);
    }
    
    /**
     * 根据类型获取地址
     */
    @GetMapping("/customer/{customerId}/type/{addressType}")
    public ResponseEntity<List<CustomerAddress>> getAddressesByType(
            @PathVariable Long customerId,
            @PathVariable String addressType) {
        log.info("根据类型获取地址: customerId={}, addressType={}", customerId, addressType);
        List<CustomerAddress> addresses = addressService.getAddressesByType(customerId, addressType);
        return ResponseEntity.ok(addresses);
    }
    
    /**
     * 统计客户地址数量
     */
    @GetMapping("/customer/{customerId}/count")
    public ResponseEntity<Long> countAddresses(@PathVariable Long customerId) {
        log.info("统计地址数量: customerId={}", customerId);
        Long count = addressService.countAddressesByCustomerId(customerId);
        return ResponseEntity.ok(count);
    }
}
