package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.dto.ApiResponse;
import com.dora.dto.AdminContentAuditDto;
import com.dora.service.AdminContentAuditService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 管理员内容审核控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin/content/audit")
@RequiredArgsConstructor
@Tag(name = "管理员内容审核", description = "管理员内容审核相关接口")
@RequireAdminOrStaff
public class AdminContentAuditController {

    private final AdminContentAuditService adminContentAuditService;

    @GetMapping("/list")
    @Operation(summary = "获取内容审核列表", description = "获取视频、工作流、课程等内容的审核列表")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功", 
            content = @Content(schema = @Schema(implementation = AdminContentAuditDto.ContentAuditListResponse.class))),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<AdminContentAuditDto.ContentAuditListResponse> getContentAuditList(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "内容类型筛选", example = "video") @RequestParam(required = false) String contentType,
            @Parameter(description = "审核状态筛选", example = "0") @RequestParam(required = false) Integer auditStatus,
            @Parameter(description = "创建者ID筛选", example = "123") @RequestParam(required = false) Long ownerId,
            @Parameter(description = "关键词搜索", example = "测试") @RequestParam(required = false) String keyword,
            @Parameter(description = "创建时间开始") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createTimeStart,
            @Parameter(description = "创建时间结束") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createTimeEnd,
            @Parameter(description = "排序字段", example = "create_time") @RequestParam(defaultValue = "create_time") String sortField,
            @Parameter(description = "排序方向", example = "desc") @RequestParam(defaultValue = "desc") String sortOrder) {
        
        log.info("收到获取内容审核列表请求，页码：{}，大小：{}，类型：{}，状态：{}", page, size, contentType, auditStatus);
        
        AdminContentAuditDto.ContentAuditListRequest request = new AdminContentAuditDto.ContentAuditListRequest();
        request.setPage(page);
        request.setSize(size);
        request.setContentType(contentType);
        request.setAuditStatus(auditStatus);
        request.setOwnerId(ownerId);
        request.setKeyword(keyword);
        request.setCreateTimeStart(createTimeStart);
        request.setCreateTimeEnd(createTimeEnd);
        request.setSortField(sortField);
        request.setSortOrder(sortOrder);
        
        return adminContentAuditService.getContentAuditList(request);
    }

    @PostMapping("/{contentType}/{contentId}")
    @Operation(summary = "审核内容", description = "审核指定的视频、工作流或课程内容")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "审核成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<String> auditContent(
            @Parameter(description = "内容类型", example = "video") @PathVariable String contentType,
            @Parameter(description = "内容ID", example = "123") @PathVariable Long contentId,
            @Parameter(description = "审核状态", example = "1") @RequestParam Integer auditStatus,
            @Parameter(description = "拒绝原因") @RequestParam(required = false) String rejectReason) {
        
        log.info("收到审核内容请求，类型：{}，ID：{}，状态：{}", contentType, contentId, auditStatus);
        
        AdminContentAuditDto.ContentAuditRequest request = new AdminContentAuditDto.ContentAuditRequest();
        request.setContentId(contentId);
        request.setContentType(contentType);
        request.setAuditStatus(auditStatus);
        request.setRejectReason(rejectReason);
        
        return adminContentAuditService.auditContent(request);
    }

    @PostMapping("/audit")
    @Operation(summary = "审核内容（JSON）", description = "通过JSON请求体审核内容")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "审核成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<String> auditContentJson(@Valid @RequestBody AdminContentAuditDto.ContentAuditRequest request) {
        log.info("收到JSON审核内容请求，类型：{}，ID：{}，状态：{}", 
            request.getContentType(), request.getContentId(), request.getAuditStatus());
        
        return adminContentAuditService.auditContent(request);
    }

    @PostMapping("/batch")
    @Operation(summary = "批量审核内容", description = "批量审核多个内容")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "批量审核完成"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<String> batchAuditContent(@Valid @RequestBody AdminContentAuditDto.BatchAuditRequest request) {
        log.info("收到批量审核内容请求，项目数量：{}", request.getAuditItems().size());
        
        return adminContentAuditService.batchAuditContent(request);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取审核统计信息", description = "获取各种状态的内容审核统计数据")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功", 
            content = @Content(schema = @Schema(implementation = AdminContentAuditDto.AuditStatistics.class))),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<AdminContentAuditDto.AuditStatistics> getAuditStatistics() {
        log.info("收到获取审核统计信息请求");
        
        return adminContentAuditService.getAuditStatistics();
    }

    @PostMapping("/update-course-properties")
    @Operation(summary = "更新课程属性", description = "更新课程的免费状态和访问级别")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<String> updateCourseProperties(@Valid @RequestBody AdminContentAuditDto.CoursePropertiesUpdateRequest request) {
        log.info("收到更新课程属性请求，课程ID：{}", request.getCourseId());
        
        return adminContentAuditService.updateCourseProperties(request);
    }

    @PostMapping("/update-workflow-properties")
    @Operation(summary = "更新工作流属性", description = "更新工作流的免费状态和访问权限")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<String> updateWorkflowProperties(@Valid @RequestBody AdminContentAuditDto.WorkflowPropertiesUpdateRequest request) {
        log.info("收到更新工作流属性请求，工作流ID：{}", request.getWorkflowId());
        
        return adminContentAuditService.updateWorkflowProperties(request);
    }

    @PostMapping("/video-play-auth")
    @Operation(summary = "管理员获取视频播放凭证", description = "管理员审核时获取视频播放凭证，无需用户权限验证")
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<Object> getVideoPlayAuth(@RequestBody AdminContentAuditDto.VideoPlayAuthRequest request) {
        return adminContentAuditService.getVideoPlayAuth(request);
    }
}
