package com.xindazhou.vehicle.controller.admin.vehicle;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleGalleryPageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleGalleryRespVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleImagePageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleImageRespVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleImageMoveReqVO;
import com.xindazhou.vehicle.service.gallery.VehicleGalleryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 车辆图库 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 车辆图库")
@RestController
@RequestMapping("/admin/vehicle/gallery")
@Validated
public class VehicleGalleryController {

    @Resource
    private VehicleGalleryService vehicleGalleryService;

    @GetMapping("/page")
    @Operation(summary = "获得车辆图库分页")
    @PreAuthorize("@ss.hasPermission('vehicle:gallery:query')")
    public CommonResult<PageResult<VehicleGalleryRespVO>> getVehicleGalleryPage(@Valid VehicleGalleryPageReqVO pageReqVO) {
        PageResult<VehicleGalleryRespVO> pageResult = vehicleGalleryService.getVehicleGalleryPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/detail")
    @Operation(summary = "获得车辆图库详情")
    @Parameter(name = "categoryId", description = "分类ID", example = "1")
    @Parameter(name = "modelId", description = "型号ID", example = "1")
    @PreAuthorize("@ss.hasPermission('vehicle:gallery:query')")
    public CommonResult<VehicleGalleryRespVO> getVehicleGalleryDetail(@RequestParam(value = "categoryId", required = false) String categoryId,
                                                                        @RequestParam(value = "modelId", required = false) String modelId) {
        VehicleGalleryRespVO gallery = vehicleGalleryService.getVehicleGalleryDetail(categoryId, modelId);
        return success(gallery);
    }

    @GetMapping("/image/page")
    @Operation(summary = "获得车辆图片分页")
    @PreAuthorize("@ss.hasPermission('vehicle:gallery:query')")
    public CommonResult<PageResult<VehicleImageRespVO>> getVehicleImagePage(@Valid VehicleImagePageReqVO pageReqVO) {
        PageResult<VehicleImageRespVO> pageResult = vehicleGalleryService.getVehicleImagePage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/image/upload")
    @Operation(summary = "上传车辆图片")
    @PreAuthorize("@ss.hasPermission('vehicle:gallery:create')")
    public CommonResult<Boolean> uploadVehicleImage(@RequestParam("file") MultipartFile file,
                                                     @RequestParam("galleryId") Long galleryId,
                                                     @RequestParam("category") String category,
                                                     @RequestParam(value = "name", required = false) String name) {
        // TODO: 调用文件服务上传文件，获取URL
        String url = "https://example.com/uploaded/" + file.getOriginalFilename();
        vehicleGalleryService.uploadVehicleImage(galleryId, category, url, name != null ? name : file.getOriginalFilename());
        return success(true);
    }

    @DeleteMapping("/image/delete")
    @Operation(summary = "删除车辆图片")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('vehicle:gallery:delete')")
    public CommonResult<Boolean> deleteVehicleImage(@RequestParam("id") Long id) {
        vehicleGalleryService.deleteVehicleImage(id);
        return success(true);
    }

    @DeleteMapping("/image/batchDelete")
    @Operation(summary = "批量删除车辆图片")
    @PreAuthorize("@ss.hasPermission('vehicle:gallery:delete')")
    public CommonResult<Boolean> batchDeleteVehicleImage(@RequestParam("ids") Collection<Long> ids) {
        vehicleGalleryService.batchDeleteVehicleImage(ids);
        return success(true);
    }

    @PostMapping("/image/move")
    @Operation(summary = "移动车辆图片")
    @PreAuthorize("@ss.hasPermission('vehicle:gallery:update')")
    public CommonResult<Boolean> moveVehicleImage(@Valid @RequestBody VehicleImageMoveReqVO reqVO) {
        vehicleGalleryService.moveVehicleImage(reqVO);
        return success(true);
    }

}

