package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.dto.AdminGiftCodeDto;
import com.dora.dto.ApiResponse;
import com.dora.service.AdminGiftCodeService;
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;

/**
 * 管理端礼品码管理控制器
 */
@RestController
@RequestMapping("/admin/gift-codes")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "管理端礼品码管理", description = "管理端礼品码相关接口")
@RequireAdminOrStaff
public class AdminGiftCodeController {

    private final AdminGiftCodeService adminGiftCodeService;

    @GetMapping("/list")
    @Operation(summary = "获取礼品码列表", description = "分页获取礼品码列表，支持多种筛选条件")
    public ApiResponse<AdminGiftCodeDto.GiftCodeListResponse> getGiftCodeList(
            @Valid AdminGiftCodeDto.GiftCodeListRequest request) {
        
        log.info("收到礼品码列表查询请求: {}", request);
        return adminGiftCodeService.getGiftCodeList(request);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取礼品码详情", description = "根据礼品码ID获取详细信息")
    public ApiResponse<AdminGiftCodeDto.GiftCodeDetailResponse> getGiftCodeDetail(
            @Parameter(description = "礼品码ID") @PathVariable Long id) {
        
        log.info("收到礼品码详情查询请求，ID: {}", id);
        return adminGiftCodeService.getGiftCodeDetail(id);
    }

    @PostMapping("/create")
    @Operation(summary = "创建礼品码", description = "创建新的礼品码")
    public ApiResponse<String> createGiftCode(
            @Valid @RequestBody AdminGiftCodeDto.CreateGiftCodeRequest request) {
        
        log.info("收到创建礼品码请求: {}", request);
        return adminGiftCodeService.createGiftCode(request);
    }

    @PutMapping("/update")
    @Operation(summary = "更新礼品码", description = "更新礼品码信息")
    public ApiResponse<String> updateGiftCode(
            @Valid @RequestBody AdminGiftCodeDto.UpdateGiftCodeRequest request) {
        
        log.info("收到更新礼品码请求: {}", request);
        return adminGiftCodeService.updateGiftCode(request);
    }

    @PutMapping("/status")
    @Operation(summary = "更新礼品码状态", description = "启用或禁用礼品码")
    public ApiResponse<String> updateGiftCodeStatus(
            @Valid @RequestBody AdminGiftCodeDto.UpdateGiftCodeStatusRequest request) {
        
        log.info("收到更新礼品码状态请求: {}", request);
        return adminGiftCodeService.updateGiftCodeStatus(request);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除礼品码", description = "根据ID删除礼品码")
    public ApiResponse<String> deleteGiftCode(
            @Parameter(description = "礼品码ID") @PathVariable Long id) {
        
        log.info("收到删除礼品码请求，ID: {}", id);
        return adminGiftCodeService.deleteGiftCode(id);
    }

    @PostMapping("/batch")
    @Operation(summary = "批量操作礼品码", description = "批量启用、禁用或删除礼品码")
    public ApiResponse<String> batchOperateGiftCodes(
            @Valid @RequestBody AdminGiftCodeDto.BatchOperationRequest request) {
        
        log.info("收到批量操作礼品码请求: {}", request);
        return adminGiftCodeService.batchOperateGiftCodes(request);
    }

    @GetMapping("/usage/list")
    @Operation(summary = "获取使用记录列表", description = "分页获取礼品码使用记录列表")
    public ApiResponse<AdminGiftCodeDto.UsageListResponse> getUsageList(
            @Valid AdminGiftCodeDto.UsageListRequest request) {
        
        log.info("收到使用记录列表查询请求: {}", request);
        return adminGiftCodeService.getUsageList(request);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取礼品码统计信息", description = "获取礼品码相关的统计数据")
    public ApiResponse<AdminGiftCodeDto.GiftCodeStatisticsResponse> getGiftCodeStatistics() {
        
        log.info("收到礼品码统计信息查询请求");
        return adminGiftCodeService.getGiftCodeStatistics();
    }

    // ==================== 便捷接口 ====================

    @GetMapping("/search")
    @Operation(summary = "搜索礼品码", description = "根据关键词快速搜索礼品码")
    public ApiResponse<AdminGiftCodeDto.GiftCodeListResponse> searchGiftCodes(
            @Parameter(description = "搜索关键词（礼品码、名称）") @RequestParam String keyword,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        
        log.info("收到礼品码搜索请求，关键词: {}", keyword);
        
        AdminGiftCodeDto.GiftCodeListRequest request = new AdminGiftCodeDto.GiftCodeListRequest();
        request.setKeyword(keyword);
        request.setPage(page);
        request.setSize(size);
        
        return adminGiftCodeService.getGiftCodeList(request);
    }

    @GetMapping("/type/{type}")
    @Operation(summary = "按类型获取礼品码", description = "根据礼品码类型获取礼品码列表")
    public ApiResponse<AdminGiftCodeDto.GiftCodeListResponse> getGiftCodesByType(
            @Parameter(description = "礼品码类型（1-积分卡/2-会员卡）") @PathVariable Integer type,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        
        log.info("收到按类型查询礼品码请求，类型: {}", type);
        
        AdminGiftCodeDto.GiftCodeListRequest request = new AdminGiftCodeDto.GiftCodeListRequest();
        request.setType(type);
        request.setPage(page);
        request.setSize(size);
        
        return adminGiftCodeService.getGiftCodeList(request);
    }

    @GetMapping("/status/{isActive}")
    @Operation(summary = "按状态获取礼品码", description = "根据启用状态获取礼品码列表")
    public ApiResponse<AdminGiftCodeDto.GiftCodeListResponse> getGiftCodesByStatus(
            @Parameter(description = "启用状态（0-禁用/1-启用）") @PathVariable Integer isActive,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        
        log.info("收到按状态查询礼品码请求，状态: {}", isActive);
        
        AdminGiftCodeDto.GiftCodeListRequest request = new AdminGiftCodeDto.GiftCodeListRequest();
        request.setIsActive(isActive);
        request.setPage(page);
        request.setSize(size);
        
        return adminGiftCodeService.getGiftCodeList(request);
    }

    @GetMapping("/{id}/usage")
    @Operation(summary = "获取指定礼品码的使用记录", description = "获取指定礼品码的使用记录列表")
    public ApiResponse<AdminGiftCodeDto.UsageListResponse> getGiftCodeUsage(
            @Parameter(description = "礼品码ID") @PathVariable Long id,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        
        log.info("收到指定礼品码使用记录查询请求，礼品码ID: {}", id);
        
        AdminGiftCodeDto.UsageListRequest request = new AdminGiftCodeDto.UsageListRequest();
        request.setGiftCodeId(id);
        request.setPage(page);
        request.setSize(size);
        
        return adminGiftCodeService.getUsageList(request);
    }

    @GetMapping("/user/{userId}/usage")
    @Operation(summary = "获取指定用户的礼品码使用记录", description = "获取指定用户的礼品码使用记录列表")
    public ApiResponse<AdminGiftCodeDto.UsageListResponse> getUserGiftCodeUsage(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        
        log.info("收到指定用户礼品码使用记录查询请求，用户ID: {}", userId);
        
        AdminGiftCodeDto.UsageListRequest request = new AdminGiftCodeDto.UsageListRequest();
        request.setUserId(userId);
        request.setPage(page);
        request.setSize(size);
        
        return adminGiftCodeService.getUsageList(request);
    }

    @PostMapping("/{id}/enable")
    @Operation(summary = "启用礼品码", description = "启用指定的礼品码")
    public ApiResponse<String> enableGiftCode(
            @Parameter(description = "礼品码ID") @PathVariable Long id) {
        
        log.info("收到启用礼品码请求，ID: {}", id);
        
        AdminGiftCodeDto.UpdateGiftCodeStatusRequest request = new AdminGiftCodeDto.UpdateGiftCodeStatusRequest();
        request.setId(id);
        request.setIsActive(1);
        
        return adminGiftCodeService.updateGiftCodeStatus(request);
    }

    @PostMapping("/{id}/disable")
    @Operation(summary = "禁用礼品码", description = "禁用指定的礼品码")
    public ApiResponse<String> disableGiftCode(
            @Parameter(description = "礼品码ID") @PathVariable Long id) {
        
        log.info("收到禁用礼品码请求，ID: {}", id);
        
        AdminGiftCodeDto.UpdateGiftCodeStatusRequest request = new AdminGiftCodeDto.UpdateGiftCodeStatusRequest();
        request.setId(id);
        request.setIsActive(0);
        
        return adminGiftCodeService.updateGiftCodeStatus(request);
    }

    @GetMapping("/expired")
    @Operation(summary = "获取已过期的礼品码", description = "获取已过期的礼品码列表")
    public ApiResponse<AdminGiftCodeDto.GiftCodeListResponse> getExpiredGiftCodes(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        
        log.info("收到已过期礼品码查询请求");
        
        return adminGiftCodeService.getExpiredGiftCodes(page, size);
    }

    @PostMapping("/fix-usage-count")
    @Operation(summary = "修复使用次数数据一致性", description = "修复礼品码表和使用记录表之间的数据不一致问题")
    public ApiResponse<String> fixUsageCountConsistency() {
        
        log.info("收到修复礼品码使用次数数据一致性请求");
        
        return adminGiftCodeService.fixUsageCountConsistency();
    }
}
