package com.cf.imes.module.system.controller.admin.funds.advertisement;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.validation.InEnum;
import com.cf.imes.module.system.controller.admin.funds.advertisement.vo.AdvertisementPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.advertisement.vo.AdvertisementRePublishReqVO;
import com.cf.imes.module.system.controller.admin.funds.advertisement.vo.AdvertisementRespVO;
import com.cf.imes.module.system.controller.admin.funds.advertisement.vo.AdvertisementSaveReqVO;
import com.cf.imes.module.system.enums.advertisement.AdvertisementPositionEnum;
import com.cf.imes.module.system.service.funds.advertisement.AdvertisementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 广告管理")
@RestController
@RequestMapping("/system/ad")
@Validated
@Slf4j
public class AdvertisementController {

    @Resource
    private AdvertisementService advertisementService;


    @PostMapping("")
    @Operation(summary = "新建广告")
    public CommonResult<Long> createAdvertisement(@Valid @RequestBody AdvertisementSaveReqVO createReqVO) {
        return success(advertisementService.create(createReqVO));
    }


    @PutMapping("")
    @Operation(summary = "更新广告")
    public CommonResult<Boolean> updateAdvertisement(@Valid @RequestBody AdvertisementSaveReqVO reqVO) {
        advertisementService.update(reqVO);
        return success(true);
    }

    @PostMapping("/image")
    @Operation(summary = "上传广告图片")
    @Parameter(description = "广告图片", required = true)
    public CommonResult<String> uploadAdImage(@RequestPart("file") MultipartFile file) {
        return success(advertisementService.uploadAdImage(file));
    }


    @DeleteMapping("/{id}")
    @Operation(summary = "删除广告")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteAdvertisement(@PathVariable("id") Long id) {
        advertisementService.delete(id);
        return success(true);
    }


    @GetMapping("/{id}")
    @Operation(summary = "查询广告")
    @Parameter(name = "id", description = "广告ID", required = true, example = "1024")
    public CommonResult<AdvertisementRespVO> getAdvertisement(@PathVariable("id") Long id) {
        return success(advertisementService.get(id));
    }


    @GetMapping("/page")
    @Operation(summary = "查询广告分页")
    public CommonResult<PageResult<AdvertisementRespVO>> getAdvertisementPage(@Valid AdvertisementPageReqVO pageReqVO) {
        return success(advertisementService.getPage(pageReqVO));
    }


    @PostMapping("/publish/{id}")
    @Operation(summary = "立即发布广告")
    @Parameter(name = "id", description = "广告ID", required = true, example = "1024")
    public CommonResult<Boolean> publish(@PathVariable Long id) {
        advertisementService.publish(id);
        return success(true);
    }

    @PostMapping("/endPublish/{id}")
    @Operation(summary = "结束发布广告")
    @Parameter(name = "id", description = "广告ID", required = true, example = "1024")
    public CommonResult<Boolean> endPublish(@PathVariable Long id) {
        advertisementService.endPublish(id);
        return success(true);
    }

    @PostMapping("/republish")
    @Operation(summary = "重新发布广告")
    public CommonResult<Boolean> rePublish(@RequestBody AdvertisementRePublishReqVO reqVO) {
        advertisementService.republish(reqVO);
        return success(true);
    }


    @GetMapping("/position/page")
    @Operation(summary = "根据广告位置获取广告信息")
    public CommonResult<List<AdvertisementRespVO>> getAdvertisementByPosition(@RequestParam("adPosition") @InEnum(AdvertisementPositionEnum.class) Integer adPosition) {
        return success(advertisementService.getAdvertisementByPosition(adPosition));
    }
}
