package com.zsElectric.boot.business.controller.applet;

import com.zsElectric.boot.business.converter.UserVehicleConverter;
import com.zsElectric.boot.business.model.form.UserVehicleForm;
import com.zsElectric.boot.business.model.vo.UserVehicleVO;
import com.zsElectric.boot.business.service.UserVehicleService;
import com.zsElectric.boot.common.annotation.Log;
import com.zsElectric.boot.common.enums.LogModuleEnum;
import com.zsElectric.boot.core.web.Result;
import com.zsElectric.boot.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序用户车辆管理控制层
 *
 * @author zsElectric
 * @since 2026-03-03
 */
@Tag(name = "我的车辆接口")
@RestController
@RequestMapping("/applet/v1/vehicle")
@RequiredArgsConstructor
public class AppletUserVehicleController {

    private final UserVehicleService userVehicleService;
    private final UserVehicleConverter userVehicleConverter;

    @Operation(summary = "我的车辆列表")
    @GetMapping("/list")
    public Result<List<UserVehicleVO>> getMyVehicleList() {
        Long userId = SecurityUtils.getUserId();
        List<UserVehicleVO> list = userVehicleService.getUserVehicleList(userId);
        return Result.success(list);
    }

    @Operation(summary = "获取车辆详情")
    @GetMapping("/{id}")
    public Result<UserVehicleVO> getVehicleDetail(
            @Parameter(description = "车辆ID") @PathVariable Long id
    ) {
        UserVehicleForm formData = userVehicleService.getUserVehicleFormData(id);
        UserVehicleVO vo = userVehicleConverter.formToVO(formData);
        return Result.success(vo);
    }

    @Operation(summary = "获取默认车辆")
    @GetMapping("/default")
    public Result<UserVehicleVO> getDefaultVehicle() {
        Long userId = SecurityUtils.getUserId();
        UserVehicleVO vehicle = userVehicleService.getDefaultVehicle(userId);
        return Result.success(vehicle);
    }

    @Operation(summary = "新增车辆")
    @PostMapping
    @Log(value = "新增车辆", module = LogModuleEnum.APP_OTHER)
    public Result<Void> addVehicle(@RequestBody @Valid UserVehicleForm formData) {
        // 设置当前用户ID
        formData.setUserId(SecurityUtils.getUserId());
        boolean result = userVehicleService.saveUserVehicle(formData);
        return Result.judge(result);
    }

    @Operation(summary = "修改车辆")
    @PutMapping("/{id}")
    @Log(value = "修改车辆", module = LogModuleEnum.APP_OTHER)
    public Result<Void> updateVehicle(
            @Parameter(description = "车辆ID") @PathVariable Long id,
            @RequestBody @Valid UserVehicleForm formData
    ) {
        // 设置当前用户ID，确保只能修改自己的车辆
        formData.setUserId(SecurityUtils.getUserId());
        boolean result = userVehicleService.updateUserVehicle(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除车辆")
    @DeleteMapping("/{id}")
    @Log(value = "删除车辆", module = LogModuleEnum.APP_OTHER)
    public Result<Void> deleteVehicle(
            @Parameter(description = "车辆ID") @PathVariable Long id
    ) {
        boolean result = userVehicleService.deleteUserVehicles(String.valueOf(id));
        return Result.judge(result);
    }

    @Operation(summary = "设置默认车辆")
    @PutMapping("/default/{id}")
    @Log(value = "设置默认车辆", module = LogModuleEnum.APP_OTHER)
    public Result<Void> setDefaultVehicle(
            @Parameter(description = "车辆ID") @PathVariable Long id
    ) {
        Long userId = SecurityUtils.getUserId();
        boolean result = userVehicleService.setDefaultVehicle(userId, id);
        return Result.judge(result);
    }
}
