package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.common.Result;
import com.dora.dto.PromotionPosterCreateDto;
import com.dora.dto.PromotionPosterDto;
import com.dora.dto.PromotionPosterListDto;
import com.dora.dto.PromotionPosterUpdateDto;
import com.dora.dto.PromotionPosterSortDto;
import com.dora.dto.PromotionPosterStatusDto;
import com.dora.dto.PageResultDto;
import com.dora.service.PromotionPosterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 管理员推广海报管理控制器
 */
@RestController
@RequestMapping("/admin/promotion-posters")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "管理员推广海报管理", description = "管理员推广海报管理相关接口")
@RequireAdminOrStaff
public class AdminPromotionPosterController {

    private final PromotionPosterService promotionPosterService;

    @GetMapping("/list")
    @Operation(summary = "分页获取推广海报列表", description = "管理员分页查询推广海报列表")
    public Result<PageResultDto<PromotionPosterListDto>> getPromotionPostersByPage(
            @Parameter(description = "页码，从1开始") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "20") int size) {
        
        log.info("管理员查询推广海报列表: page={}, size={}", page, size);
        PageResultDto<PromotionPosterListDto> result = promotionPosterService.getPromotionPostersByPage(page, size);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取推广海报详情", description = "根据ID获取推广海报详细信息")
    public Result<PromotionPosterDto> getPromotionPosterById(
            @Parameter(description = "推广海报ID") @PathVariable Long id) {
        
        log.info("管理员查询推广海报详情: id={}", id);
        PromotionPosterDto result = promotionPosterService.getPromotionPosterDtoById(id);
        return Result.success(result);
    }

    @PostMapping("/create")
    @Operation(summary = "创建推广海报", description = "管理员创建新推广海报")
    public Result<PromotionPosterDto> createPromotionPoster(@Valid @RequestBody PromotionPosterCreateDto createDto) {
        
        log.info("管理员创建推广海报: {}", createDto);
        PromotionPosterDto result = promotionPosterService.createPromotionPoster(createDto);
        return Result.success(result);
    }

    @PutMapping("/update")
    @Operation(summary = "更新推广海报", description = "管理员更新推广海报信息")
    public Result<PromotionPosterDto> updatePromotionPoster(@Valid @RequestBody PromotionPosterUpdateDto updateDto) {
        
        log.info("管理员更新推广海报: {}", updateDto);
        PromotionPosterDto result = promotionPosterService.updatePromotionPoster(updateDto);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除推广海报", description = "管理员删除推广海报")
    public Result<Void> deletePromotionPoster(
            @Parameter(description = "推广海报ID") @PathVariable Long id) {
        
        log.info("管理员删除推广海报: id={}", id);
        promotionPosterService.deletePromotionPoster(id);
        return Result.success(null);
    }

    @PutMapping("/batch-sort")
    @Operation(summary = "批量更新排序", description = "批量更新推广海报排序")
    public Result<Void> batchUpdateSortOrder(@Valid @RequestBody List<PromotionPosterSortDto> sortDtos) {
        
        log.info("管理员批量更新推广海报排序: size={}", sortDtos.size());
        promotionPosterService.batchUpdateSortOrder(sortDtos);
        return Result.success(null);
    }

    @PutMapping("/status")
    @Operation(summary = "切换推广海报状态", description = "启用或禁用推广海报")
    public Result<Void> updatePromotionPosterStatus(@Valid @RequestBody PromotionPosterStatusDto statusDto) {
        
        log.info("管理员更新推广海报状态: id={}, enabled={}", statusDto.getId(), statusDto.getIsEnabled());
        promotionPosterService.updatePromotionPosterStatus(statusDto.getId(), statusDto.getIsEnabled());
        return Result.success(null);
    }
}
