package com.example.demo.controller;

import com.example.demo.model.ApiResult;
import com.example.demo.model.BusinessItem;
import com.example.demo.service.BusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务管理控制器。
 * <p>提供业务的新增、编辑、删除、提交、审核、驳回、归档及查询筛选 RESTful API。</p>
 */
@Tag(name = "业务管理", description = "业务的新增、编辑、删除、提交、审核、驳回、归档及查询筛选")
@RestController
@RequestMapping("/api/business")
public class BusinessController {

    private final BusinessService businessService;

    public BusinessController(BusinessService businessService) {
        this.businessService = businessService;
    }

    /**
     * 新增业务。
     *
     * @requirement(name="业务新增", id="SRS-F-02-001")
     */
    @Operation(summary = "新增业务")
    @PostMapping("/add")
    public ApiResult<BusinessItem> add(@RequestBody BusinessItem item) {
        return ApiResult.success("新增成功", businessService.save(item));
    }

    /**
     * 编辑业务。
     *
     * @requirement(name="业务编辑", id="SRS-F-02-002")
     */
    @Operation(summary = "编辑业务")
    @PutMapping("/edit")
    public ApiResult<BusinessItem> edit(@RequestBody BusinessItem item) {
        BusinessItem updated = businessService.update(item);
        if (updated == null) {
            return ApiResult.error(400, "业务不存在或当前状态不可编辑");
        }
        return ApiResult.success("编辑成功", updated);
    }

    /**
     * 删除业务。
     *
     * @requirement(name="业务删除", id="SRS-F-02-003")
     */
    @Operation(summary = "删除业务")
    @DeleteMapping("/delete/{id}")
    public ApiResult<Void> delete(@PathVariable Long id) {
        if (!businessService.deleteById(id)) {
            return ApiResult.error(400, "业务不存在或当前状态不可删除");
        }
        return ApiResult.success("删除成功", null);
    }

    /**
     * 提交业务（草稿→待审核）。
     *
     * @requirement(name="业务提交", id="SRS-F-02-004")
     */
    @Operation(summary = "提交业务")
    @PutMapping("/submit")
    public ApiResult<BusinessItem> submit(
            @Parameter(description = "业务ID") @RequestParam Long businessId) {
        BusinessItem item = businessService.submit(businessId);
        if (item == null) {
            return ApiResult.error(400, "业务不存在或当前状态不可提交");
        }
        return ApiResult.success("提交成功", item);
    }

    /**
     * 审核通过业务（待审核→通过）。
     *
     * @requirement(name="业务审核", id="SRS-F-02-005")
     */
    @Operation(summary = "审核通过")
    @PutMapping("/review")
    public ApiResult<BusinessItem> review(
            @Parameter(description = "业务ID") @RequestParam Long businessId,
            @Parameter(description = "审核人ID") @RequestParam Long auditorId,
            @Parameter(description = "审核意见") @RequestParam(required = false) String comment) {
        BusinessItem item = businessService.approve(businessId, auditorId, comment);
        if (item == null) {
            return ApiResult.error(400, "业务不存在或当前状态不可审核");
        }
        return ApiResult.success("审核通过", item);
    }

    /**
     * 驳回业务（待审核→驳回）。
     *
     * @requirement(name="业务驳回", id="SRS-F-02-006")
     */
    @Operation(summary = "驳回业务")
    @PutMapping("/reject")
    public ApiResult<BusinessItem> reject(
            @Parameter(description = "业务ID") @RequestParam Long businessId,
            @Parameter(description = "审核人ID") @RequestParam Long auditorId,
            @Parameter(description = "驳回原因") @RequestParam(required = false) String comment) {
        BusinessItem item = businessService.reject(businessId, auditorId, comment);
        if (item == null) {
            return ApiResult.error(400, "业务不存在或当前状态不可驳回");
        }
        return ApiResult.success("驳回成功", item);
    }

    /**
     * 归档业务（通过→已归档）。
     *
     * @requirement(name="业务归档", id="SRS-F-02-007")
     */
    @Operation(summary = "归档业务")
    @PutMapping("/archive/{id}")
    public ApiResult<BusinessItem> archive(@PathVariable Long id) {
        BusinessItem item = businessService.archive(id);
        if (item == null) {
            return ApiResult.error(400, "业务不存在或当前状态不可归档");
        }
        return ApiResult.success("归档成功", item);
    }

    /**
     * 查询业务列表。
     *
     * @requirement(name="业务查询筛选", id="SRS-F-02-008")
     */
    @Operation(summary = "查询业务列表")
    @GetMapping("/list")
    public ApiResult<List<BusinessItem>> list(
            @Parameter(description = "业务类型") @RequestParam(required = false) String businessType,
            @Parameter(description = "状态") @RequestParam(required = false) String status,
            @Parameter(description = "关键字（编号/内容）") @RequestParam(required = false) String keyword) {
        return ApiResult.success(businessService.list(businessType, status, keyword));
    }
}
