package com.zsElectric.boot.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.business.model.form.UserFeedbackForm;
import com.zsElectric.boot.business.model.query.UserFeedbackQuery;
import com.zsElectric.boot.business.model.vo.UserFeedbackVO;
import com.zsElectric.boot.business.service.UserFeedbackService;
import com.zsElectric.boot.core.web.PageResult;
import com.zsElectric.boot.core.web.Result;
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 org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "v2.0 用户反馈")
@RestController
@RequestMapping("/api/v1/userFeedback")
@RequiredArgsConstructor
public class UserFeedbackController {

    private final UserFeedbackService userFeedbackService;

    @Operation(summary = "用户反馈分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:userFeedback:query')")
    public PageResult<UserFeedbackVO> getUserFeedbackPage(UserFeedbackQuery queryParams ) {
        IPage<UserFeedbackVO> result = userFeedbackService.getUserFeedbackPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增用户反馈")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:userFeedback:add')")
    public Result<Void> saveUserFeedback(@RequestBody @Valid UserFeedbackForm formData ) {
        boolean result = userFeedbackService.saveUserFeedback(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户反馈表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:userFeedback:edit')")
    public Result<UserFeedbackForm> getUserFeedbackForm(
            @Parameter(description = "用户反馈ID") @PathVariable Long id
    ) {
        UserFeedbackForm formData = userFeedbackService.getUserFeedbackFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户反馈")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:userFeedback:edit')")
    public Result<Void> updateUserFeedback(
            @Parameter(description = "用户反馈ID") @PathVariable Long id,
            @RequestBody @Validated UserFeedbackForm formData
    ) {
        boolean result = userFeedbackService.updateUserFeedback(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户反馈")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:userFeedback:delete')")
    public Result<Void> deleteUserFeedbacks(
            @Parameter(description = "用户反馈ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userFeedbackService.deleteUserFeedbacks(ids);
        return Result.judge(result);
    }

    @Operation(summary = "用户反馈回复")
    @PutMapping(value = "/{id}/{reply}")
    @PreAuthorize("@ss.hasPerm('business:userFeedback:reply')")
    public Result<Void> reply(
            @Parameter(description = "用户反馈ID") @PathVariable Long id,
            @Parameter(description = "回复") @PathVariable String reply
    ) {
        UserFeedbackForm formData = new UserFeedbackForm();
        formData.setId(id);
        formData.setReply(reply);
        formData.setReplyStatus(1);
        boolean result = userFeedbackService.updateUserFeedback(id, formData);
        return Result.judge(result);
    }
}

