package com.carmaintenance.controller;

import com.carmaintenance.dto.Result;
import com.carmaintenance.entity.Customer;
import com.carmaintenance.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户管理控制器
 */
@RestController
@RequestMapping("/customers")
@CrossOrigin
public class CustomerController {

    @Autowired
    private CustomerRepository customerRepository;

    /**
     * 获取所有客户
     */
    @GetMapping
    public Result<List<Customer>> getAllCustomers() {
        List<Customer> customers = customerRepository.findAll();
        return Result.success(customers);
    }

    /**
     * 根据ID获取客户
     */
    @GetMapping("/{id}")
    public Result<Customer> getCustomerById(@PathVariable Integer id) {
        Customer customer = customerRepository.findById(id).orElse(null);
        if (customer == null) {
            return Result.notFound("客户不存在");
        }
        return Result.success(customer);
    }

    /**
     * 根据用户ID获取客户信息
     */
    @GetMapping("/user/{userId}")
    public Result<Customer> getCustomerByUserId(@PathVariable Integer userId) {
        Customer customer = customerRepository.findByUserId(userId).orElse(null);
        if (customer == null) {
            return Result.notFound("客户信息不存在");
        }
        return Result.success(customer);
    }
}
