package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.common.Result;
import com.dora.dto.BannerCreateDto;
import com.dora.dto.BannerDto;
import com.dora.dto.BannerListDto;
import com.dora.dto.BannerUpdateDto;
import com.dora.dto.BannerSortDto;
import com.dora.dto.BannerStatusDto;
import com.dora.dto.PageResultDto;
import com.dora.service.BannerService;
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;

/**
 * 管理员Banner管理控制器
 */
@RestController
@RequestMapping("/admin/banners")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "管理员Banner管理", description = "管理员Banner管理相关接口")
@RequireAdminOrStaff
public class AdminBannerController {

    private final BannerService bannerService;

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

    @GetMapping("/{id}")
    @Operation(summary = "获取Banner详情", description = "根据ID获取Banner详细信息")
    public Result<BannerDto> getBannerById(
            @Parameter(description = "Banner ID") @PathVariable Long id) {
        
        log.info("管理员查询Banner详情: id={}", id);
        BannerDto result = bannerService.getBannerDtoById(id);
        return Result.success(result);
    }

    @PostMapping("/create")
    @Operation(summary = "创建Banner", description = "管理员创建新Banner")
    public Result<BannerDto> createBanner(@Valid @RequestBody BannerCreateDto createDto) {
        
        log.info("管理员创建Banner: {}", createDto);
        BannerDto result = bannerService.createBanner(createDto);
        return Result.success(result);
    }

    @PutMapping("/update")
    @Operation(summary = "更新Banner", description = "管理员更新Banner信息")
    public Result<BannerDto> updateBanner(@Valid @RequestBody BannerUpdateDto updateDto) {
        
        log.info("管理员更新Banner: {}", updateDto);
        BannerDto result = bannerService.updateBanner(updateDto);
        return Result.success(result);
    }

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

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

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