package com.microservices.zone.common.controller;

import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.domain.GenericsAjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.zone.detail.domain.vo.ZoneFrontRoleVo;
import com.microservices.zone.detail.service.IZoneDetailService;
import com.microservices.zone.resource.domain.ZoneResource;
import com.microservices.zone.resource.domain.vo.*;
import com.microservices.zone.resource.service.IAiSkillParseService;
import com.microservices.zone.resource.service.IZoneResourceService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author otto
 */
@RestController
@RequestMapping("/zoneFront")
@Api(tags = "特色专区前台接口")
public class FrontController extends BaseController {

    @Autowired
    private IZoneResourceService zoneResourceService;

    @Autowired
    private IAiSkillParseService aiSkillParseService;

    @Autowired
    private IZoneDetailService zoneDetailService;

    /**
     * 查询我的资源列表
     */
    @RequiresPermissions("zone:front:resource:myList")
    @GetMapping("/myResourceList")
    @ApiOperation(value = "查询我的资源列表")
    public GenericsTableDataInfo<ZoneResourceDataVo> myResourceList(ZoneResourceSearchVo zoneResourceSearchVo) {
        ZoneResource zoneResourceSearch = zoneResourceSearchVo.toZoneResource();
        zoneResourceSearch.setCreateBy(SecurityUtils.getUsername());
        startPage();
        return zoneResourceService.selectMyZoneResourceList(zoneResourceSearch);
    }

    /**
     * 查询我的资源详情
     */
    @RequiresPermissions("zone:front:resource:query")
    @GetMapping("/myResource/{id}")
    @ApiOperation(value = "查询我的资源详情")
    public GenericsAjaxResult<ZoneResourceDataVo> myResource(@PathVariable("id") Long id) {
        return genericsSuccess(zoneResourceService.selectZoneResourceDataById(id));
    }

    /**
     * 新增资源
     */
    @RequiresPermissions("zone:front:resource:add")
    @Log(title = "专区前台资源操作", businessType = BusinessType.INSERT)
    @PostMapping("/addResource")
    @ApiOperation(value = "新增资源")
    public AjaxResult addResource(@RequestBody ZoneResourceInputVo zoneResourceInputVo) {
        return success(zoneResourceService.insertZoneResource(zoneResourceInputVo));
    }

    /**
     * 编辑资源
     */
    @RequiresPermissions("zone:front:resource:edit")
    @Log(title = "专区前台资源操作", businessType = BusinessType.UPDATE)
    @PutMapping("/editResource")
    @ApiOperation(value = "编辑资源")
    public AjaxResult editResource(@RequestBody ZoneResourceUpdateVo zoneResourceUpdateVo) {
        return success(zoneResourceService.updateZoneResource(zoneResourceUpdateVo));
    }

    /**
     * 删除资源
     */
    @RequiresPermissions("zone:front:resource:remove")
    @Log(title = "专区前台资源操作", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeResource/{ids}")
    @ApiOperation(value = "删除资源")
    public AjaxResult removeResource(@PathVariable Long[] ids) {
        return toAjax(zoneResourceService.deleteMyZoneResourceByIds(ids));
    }

    /**
     * 检查当前登录用户在当前专区下角色
     */
    @GetMapping("/{zoneId}/checkCurrentRole")
    @ApiOperation(value = "检查当前登录用户在当前专区下权限",
            notes = "1、当权限为Manager时，后台返回资源管理访问地址和资讯管理访问地址，前台可提供（资源管理/资讯管理）按钮跳转到对应的后台地址\n" +
                    "2、当权限为Member时，用户可在前台操作资源和资讯\n" +
                    "3、当权限为None时，不展示前台操作按钮\n" +
                    "4、检测到用户登录后才能调用该接口，否则不展示前台操作按钮")
    public GenericsAjaxResult<ZoneFrontRoleVo> checkCurrentRole(@PathVariable Long zoneId) {
        return genericsSuccess(zoneDetailService.checkCurrentRoleInZone(zoneId));
    }

    // ==================== Skill资源广场 - 我的资源管理 ====================

    /**
     * 上传ZIP并解析
     */
//    @RequiresPermissions("zone:front:skill:add")
    @PostMapping("/skill/parse")
    @ApiOperation("上传ZIP并解析Skill包")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件实体", paramType = "form", dataType = "_file"),
    })
    public GenericsAjaxResult<AiSkillParseResultVo> parseSkillZip(@RequestPart("file") MultipartFile file) {
        return genericsSuccess(aiSkillParseService.parseSkillZip(file));
    }

    /**
     * 创建Skill资源
     */
//    @RequiresPermissions("zone:front:skill:add")
    @Log(title = "创建Skill资源", businessType = BusinessType.INSERT)
    @PostMapping("/skill")
    @ApiOperation("创建Skill资源")
    public AjaxResult addSkill(@RequestBody @Validated AiSkillInputVo inputVo) {
        return success(zoneResourceService.insertAiSkillResource(inputVo));
    }


    /**
     * 重新提交被驳回的Skill资源
     */
//    @RequiresPermissions("zone:front:skill:add")
    @Log(title = "重新提交Skill资源", businessType = BusinessType.UPDATE)
    @PutMapping("/skillResource/{id}/resubmit")
    @ApiOperation(value = "重新提交被驳回的资源")
    public AjaxResult resubmitAiResource(
            @ApiParam(value = "资源ID", required = true) @PathVariable("id") Long id) {
        return toAjax(zoneResourceService.resubmitAiResource(id));
    }

    /**
     * 撤回审核中的Skill资源
     */
//    @RequiresPermissions("zone:front:skill:edit")
    @Log(title = "撤回Skill资源", businessType = BusinessType.UPDATE)
    @PutMapping("/skillResource/{id}/withdraw")
    @ApiOperation(value = "撤回审核中的资源")
    public AjaxResult withdrawAiResource(
            @ApiParam(value = "资源ID", required = true) @PathVariable("id") Long id) {
        return toAjax(zoneResourceService.withdrawAiResource(id));
    }

    /**
     * 下架资源
     */
//    @RequiresPermissions("ai:resource:audit")
    @Log(title = "下架资源", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/takeDown")
    @ApiOperation("下架资源")
    public AjaxResult takeDownResource(
            @ApiParam(value = "资源ID", required = true) @PathVariable("id") Long id,
            @RequestParam(value = "reason", required = false) String reason) {
        return toAjax(zoneResourceService.takeDownAiResource(id, reason));
    }
    /**
     * 点赞/取消点赞
     */
//    @RequiresPermissions("ai:resource:skill:query")
    @PostMapping("/{id}/like")
    @ApiOperation("点赞/取消点赞AI资源")
    public AjaxResult toggleLike(
            @ApiParam(value = "资源ID", required = true) @PathVariable("id") Long id) {
        boolean liked = zoneResourceService.toggleAiResourceLike(id);
        return success(liked ? "已点赞" : "已取消点赞");
    }
}
