package com.dora.controller;

import com.dora.common.Result;
import com.dora.dto.PageResultDto;
import com.dora.dto.UserContentManageDto;
import com.dora.entity.Course;
import com.dora.entity.Video;
import com.dora.entity.Workflow;
import com.dora.service.UserContentManageService;
import com.dora.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user/content")
@RequiredArgsConstructor
@Tag(name = "用户作品管理", description = "用户侧作品(视频/工作流/课程)的列表、更新与删除接口")
public class UserContentManageController {

	private final UserContentManageService userContentManageService;

	@GetMapping("/videos")
	@Operation(summary = "我的视频列表", description = "分页查询我的视频，支持按审核状态与关键词过滤")
	public Result<PageResultDto<Video>> listMyVideos(@Valid UserContentManageDto.PageQuery query) {
		Long userId = SecurityUtil.getCurrentUserId();
		PageResultDto<Video> page = userContentManageService.listMyVideos(userId, query);
		return Result.success(page);
	}

	@GetMapping("/workflows")
	@Operation(summary = "我的工作流列表", description = "分页查询我的工作流，支持按审核状态与关键词过滤")
	public Result<PageResultDto<Workflow>> listMyWorkflows(@Valid UserContentManageDto.PageQuery query) {
		Long userId = SecurityUtil.getCurrentUserId();
		PageResultDto<Workflow> page = userContentManageService.listMyWorkflows(userId, query);
		return Result.success(page);
	}

	@GetMapping("/courses")
	@Operation(summary = "我的课程列表", description = "分页查询我的课程，支持按审核状态与关键词过滤")
	public Result<PageResultDto<Course>> listMyCourses(@Valid UserContentManageDto.PageQuery query) {
		Long userId = SecurityUtil.getCurrentUserId();
		PageResultDto<Course> page = userContentManageService.listMyCourses(userId, query);
		return Result.success(page);
	}

	@PutMapping("/videos/{id}")
	@Operation(summary = "更新我的视频元信息", description = "允许修改标题/描述/封面/分类/公开状态")
	public Result<String> updateMyVideo(
			@Parameter(description = "视频数据库ID") @PathVariable Long id,
			@Valid @RequestBody UserContentManageDto.VideoUpdateRequest req) {
		Long userId = SecurityUtil.getCurrentUserId();
		boolean ok = userContentManageService.updateMyVideo(userId, id, req);
		return ok ? Result.success("更新成功") : Result.error(400, "更新失败");
	}

	@DeleteMapping("/videos/{id}")
	@Operation(summary = "删除我的视频", description = "软删除视频记录")
	public Result<String> deleteMyVideo(@Parameter(description = "视频数据库ID") @PathVariable Long id) {
		Long userId = SecurityUtil.getCurrentUserId();
		boolean ok = userContentManageService.deleteMyVideo(userId, id);
		return ok ? Result.success("删除成功") : Result.error(400, "删除失败");
	}

	@PutMapping("/workflows/{id}")
	@Operation(summary = "更新我的工作流", description = "允许修改名称/描述/封面/分类/公开与权限/价格/免费标识/数据包/演示视频")
	public Result<String> updateMyWorkflow(
			@Parameter(description = "工作流数据库ID") @PathVariable Long id,
			@Valid @RequestBody UserContentManageDto.WorkflowUpdateRequest req) {
		Long userId = SecurityUtil.getCurrentUserId();
		boolean ok = userContentManageService.updateMyWorkflow(userId, id, req);
		return ok ? Result.success("更新成功") : Result.error(400, "更新失败");
	}

	@DeleteMapping("/workflows/{id}")
	@Operation(summary = "删除我的工作流", description = "软删除工作流记录")
	public Result<String> deleteMyWorkflow(@Parameter(description = "工作流数据库ID") @PathVariable Long id) {
		Long userId = SecurityUtil.getCurrentUserId();
		boolean ok = userContentManageService.deleteMyWorkflow(userId, id);
		return ok ? Result.success("删除成功") : Result.error(400, "删除失败");
	}

	@PutMapping("/courses")
	@Operation(summary = "更新我的课程元信息", description = "允许修改标题/描述/封面/分类/是否免费/访问级别")
	public Result<String> updateMyCourse(@Valid @RequestBody UserContentManageDto.CourseUpdateRequest req) {
		Long userId = SecurityUtil.getCurrentUserId();
		boolean ok = userContentManageService.updateMyCourse(userId, req);
		return ok ? Result.success("更新成功") : Result.error(400, "更新失败");
	}

	@DeleteMapping("/courses/{id}")
	@Operation(summary = "删除我的课程", description = "软删除课程记录")
	public Result<String> deleteMyCourse(@Parameter(description = "课程数据库ID") @PathVariable Long id) {
		Long userId = SecurityUtil.getCurrentUserId();
		boolean ok = userContentManageService.deleteMyCourse(userId, id);
		return ok ? Result.success("删除成功") : Result.error(400, "删除失败");
	}

	@GetMapping("/stats")
	@Operation(summary = "我的作品统计", description = "返回我名下视频/工作流/课程的总数与各审核状态数量")
	public Result<UserContentManageDto.MyStatsResponse> getMyStats() {
		Long userId = SecurityUtil.getCurrentUserId();
		return Result.success(userContentManageService.getMyStats(userId));
	}
}

