package com.zsElectric.boot.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.business.model.form.UserVehicleForm;
import com.zsElectric.boot.business.model.query.UserVehicleQuery;
import com.zsElectric.boot.business.model.vo.UserVehicleVO;
import com.zsElectric.boot.business.service.UserVehicleService;
import com.zsElectric.boot.core.web.PageResult;
import com.zsElectric.boot.core.web.Result;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户车辆管理前端控制层
 *
 * @author zsElectric
 * @since 2026-03-03
 */
@Tag(name = "用户车辆管理接口")
@RestController
@RequestMapping("/api/v1/user-vehicle")
@RequiredArgsConstructor
public class UserVehicleController {

    private final UserVehicleService userVehicleService;

    @Operation(summary = "用户车辆分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:query')")
    public PageResult<UserVehicleVO> getUserVehiclePage(UserVehicleQuery queryParams) {
        IPage<UserVehicleVO> result = userVehicleService.getUserVehiclePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "获取用户车辆列表")
    @GetMapping("/list/{userId}")
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:query')")
    public Result<List<UserVehicleVO>> getUserVehicleList(
            @Parameter(description = "用户ID") @PathVariable Long userId
    ) {
        List<UserVehicleVO> list = userVehicleService.getUserVehicleList(userId);
        return Result.success(list);
    }

    @Operation(summary = "获取用户默认车辆")
    @GetMapping("/default/{userId}")
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:query')")
    public Result<UserVehicleVO> getDefaultVehicle(
            @Parameter(description = "用户ID") @PathVariable Long userId
    ) {
        UserVehicleVO vehicle = userVehicleService.getDefaultVehicle(userId);
        return Result.success(vehicle);
    }

    @Operation(summary = "新增用户车辆")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:add')")
    public Result<Void> saveUserVehicle(@RequestBody @Valid UserVehicleForm formData) {
        boolean result = userVehicleService.saveUserVehicle(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户车辆表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:edit')")
    public Result<UserVehicleForm> getUserVehicleForm(
            @Parameter(description = "车辆ID") @PathVariable Long id
    ) {
        UserVehicleForm formData = userVehicleService.getUserVehicleFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户车辆")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:edit')")
    public Result<Void> updateUserVehicle(
            @Parameter(description = "车辆ID") @PathVariable Long id,
            @RequestBody @Validated UserVehicleForm formData
    ) {
        boolean result = userVehicleService.updateUserVehicle(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户车辆")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:delete')")
    public Result<Void> deleteUserVehicles(
            @Parameter(description = "车辆ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userVehicleService.deleteUserVehicles(ids);
        return Result.judge(result);
    }

    @Operation(summary = "设置默认车辆")
    @PutMapping("/default/{userId}/{vehicleId}")
    @PreAuthorize("@ss.hasPerm('business:user-vehicle:edit')")
    public Result<Void> setDefaultVehicle(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId
    ) {
        boolean result = userVehicleService.setDefaultVehicle(userId, vehicleId);
        return Result.judge(result);
    }
}
