package com.nbport.zgwl.manifest.controller;

import com.nbport.zgwl.common.PageResult;
import com.nbport.zgwl.common.R;
import com.nbport.zgwl.manifest.dto.ContractTemplateDto;
import com.nbport.zgwl.manifest.dto.ContractTemplateQueryDto;
import com.nbport.zgwl.manifest.dto.TemplateOrgOptionDto;
import com.nbport.zgwl.manifest.service.ContractTemplateService;
import com.nbport.zgwl.manifest.utils.ManifestManageConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 模板管理 Controller
 *
 * 路由尽量保持简单：
 * - /list          分页
 * - /published     已发布列表
 * - /org/options   假组织选项
 * - /{id}          详情
 * - POST           新增
 * - PUT            编辑
 * - PUT /{id}/status/{status} 状态切换
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(ManifestManageConstants.REQUEST_PREFIX)
public class ContractTemplateController {

    private final ContractTemplateService contractTemplateService;

    @PostMapping("/list")
    public R<PageResult<ContractTemplateDto>> queryList(@RequestBody ContractTemplateQueryDto queryDto) {
        return R.success(contractTemplateService.queryTemplateList(queryDto));
    }

    @GetMapping("/published")
    public R<List<ContractTemplateDto>> queryPublishedList() {
        return R.success(contractTemplateService.queryPublishedTemplates());
    }

    @GetMapping("/org/options")
    public R<List<TemplateOrgOptionDto>> queryOrgOptions() {
        return R.success(contractTemplateService.queryOrgOptions());
    }

    @GetMapping("/{id}")
    public R<ContractTemplateDto> getDetail(@PathVariable("id") Long id) {
        return R.success(contractTemplateService.getTemplateDetail(id));
    }

    @PostMapping
    public R<ContractTemplateDto> add(@RequestBody ContractTemplateDto template) {
        return R.success(contractTemplateService.createTemplate(template));
    }

    @PutMapping
    public R<ContractTemplateDto> edit(@RequestBody ContractTemplateDto template) {
        return R.success(contractTemplateService.updateTemplate(template));
    }

    @PutMapping("/{id}/status/{status}")
    public R<ContractTemplateDto> changeStatus(@PathVariable("id") Long id, @PathVariable("status") String status) {
        return R.success(contractTemplateService.changeTemplateStatus(id, status));
    }
}
