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.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务管理控制器。
 * <p>提供业务新增、编辑、删除、提交、审核、驳回、归档及查询筛选接口（需求标识：SRS-F-02-001 ~ SRS-F-02-008）。</p>
 */
@RestController
@RequestMapping("/api/business")
@Tag(name = "业务管理", description = "业务管理模块接口（需求标识：SRS-F-02-001~SRS-F-02-008）")
public class BusinessController {

    private final BusinessService businessService;

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

    /**
     * 新增业务。
     *
     * @requirement(name="业务新增", id="SRS-F-02-001")
     */
    @PostMapping("/add")
    @Operation(summary = "新增业务", description = "创建新的业务记录")
    public ApiResult<BusinessItem> add(@RequestBody BusinessItem item) {
        return ApiResult.success(businessService.addBusiness(item));
    }

    /**
     * 编辑业务。
     *
     * @requirement(name="业务编辑", id="SRS-F-02-002")
     */
    @PutMapping("/edit")
    @Operation(summary = "编辑业务", description = "修改已有业务记录（仅草稿状态可编辑）")
    public ApiResult<BusinessItem> edit(@RequestBody BusinessItem item) {
        return ApiResult.success(businessService.editBusiness(item));
    }

    /**
     * 删除业务。
     *
     * @requirement(name="业务删除", id="SRS-F-02-003")
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除业务", description = "根据ID删除业务（仅草稿状态可删除）")
    public ApiResult<Void> delete(@PathVariable Long id) {
        businessService.deleteBusiness(id);
        return ApiResult.success();
    }

    /**
     * 提交业务。
     *
     * @requirement(name="业务提交", id="SRS-F-02-004")
     */
    @PutMapping("/submit")
    @Operation(summary = "提交业务", description = "用户将业务提交给指定审核人")
    public ApiResult<BusinessItem> submit(@RequestParam Long id,
                                           @RequestParam Long reviewerId,
                                           @RequestParam String reviewerName) {
        return ApiResult.success(businessService.submitBusiness(id, reviewerId, reviewerName));
    }

    /**
     * 审核业务。
     *
     * @requirement(name="业务审核", id="SRS-F-02-005")
     */
    @PutMapping("/review")
    @Operation(summary = "审核业务", description = "审核人通过业务申请")
    public ApiResult<BusinessItem> review(@RequestParam Long id, @RequestParam String comment) {
        return ApiResult.success(businessService.approveBusiness(id, comment));
    }

    /**
     * 驳回业务。
     *
     * @requirement(name="业务驳回", id="SRS-F-02-006")
     */
    @PutMapping("/reject")
    @Operation(summary = "驳回业务", description = "审核人驳回业务申请")
    public ApiResult<BusinessItem> reject(@RequestParam Long id, @RequestParam String comment) {
        return ApiResult.success(businessService.rejectBusiness(id, comment));
    }

    /**
     * 归档业务。
     *
     * @requirement(name="业务归档", id="SRS-F-02-007")
     */
    @PutMapping("/archive/{id}")
    @Operation(summary = "归档业务", description = "将已通过的业务归档锁定")
    public ApiResult<BusinessItem> archive(@PathVariable Long id) {
        return ApiResult.success(businessService.archiveBusiness(id));
    }

    /**
     * 查询业务列表。
     *
     * @requirement(name="业务查询筛选", id="SRS-F-02-008")
     */
    @GetMapping("/list")
    @Operation(summary = "查询业务列表", description = "支持按类型、状态、日期范围多条件筛选分页查询")
    public ApiResult<List<BusinessItem>> list(@RequestParam(required = false) String businessType,
                                               @RequestParam(required = false) String status,
                                               @RequestParam(required = false) String dateFrom,
                                               @RequestParam(required = false) String dateTo,
                                               @RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "10") int size) {
        return ApiResult.success(businessService.listBusinesses(businessType, status, dateFrom, dateTo, page, size));
    }
}
