package com.agileboot.admin.controller.collaboration;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.domain.collaboration.record.CollaborationRecordApplicationService;
import com.agileboot.domain.collaboration.record.command.AddCollaborationRecordCommand;
import com.agileboot.domain.collaboration.record.command.UpdateCollaborationRecordCommand;
import com.agileboot.domain.collaboration.record.dto.CollaborationMonthlyStatisticsDTO;
import com.agileboot.domain.collaboration.record.dto.CollaborationOptionDTO;
import com.agileboot.domain.collaboration.record.dto.CollaborationRecordDTO;
import com.agileboot.domain.collaboration.record.dto.CollaborationRecordDetailDTO;
import com.agileboot.domain.collaboration.record.query.CollaborationRecordQuery;
import com.agileboot.domain.common.command.BulkOperationCommand;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author codex
 */
@Tag(name = "合作记录API", description = "合作记录相关的增删查改和统计")
@RestController
@RequestMapping("/collaboration/record")
@Validated
@RequiredArgsConstructor
public class CollaborationRecordController extends BaseController {

    private final CollaborationRecordApplicationService recordApplicationService;

    @Operation(summary = "合作记录列表")
    @PreAuthorize("@permission.has('collaboration:record:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<CollaborationRecordDTO>> list(CollaborationRecordQuery query) {
        return ResponseDTO.ok(recordApplicationService.getRecordList(query));
    }

    @Operation(summary = "合作记录详情")
    @PreAuthorize("@permission.has('collaboration:record:query')")
    @GetMapping("/{recordId}")
    public ResponseDTO<CollaborationRecordDetailDTO> getInfo(@PathVariable @Positive Long recordId) {
        return ResponseDTO.ok(recordApplicationService.getRecordInfo(recordId));
    }

    @Operation(summary = "合作记录选项")
    @PreAuthorize("@permission.has('collaboration:record:list')")
    @GetMapping("/options")
    public ResponseDTO<List<CollaborationOptionDTO>> options() {
        return ResponseDTO.ok(recordApplicationService.getOptions());
    }

    @Operation(summary = "合作记录月度统计")
    @PreAuthorize("@permission.has('collaboration:record:statistics')")
    @GetMapping("/monthly-statistics")
    public ResponseDTO<List<CollaborationMonthlyStatisticsDTO>> monthlyStatistics(@RequestParam Integer year) {
        return ResponseDTO.ok(recordApplicationService.getMonthlyStatistics(year));
    }

    @Operation(summary = "新增合作记录")
    @PreAuthorize("@permission.has('collaboration:record:add')")
    @AccessLog(title = "合作记录", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@Valid @RequestBody AddCollaborationRecordCommand command) {
        recordApplicationService.addRecord(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "修改合作记录")
    @PreAuthorize("@permission.has('collaboration:record:edit')")
    @AccessLog(title = "合作记录", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@Valid @RequestBody UpdateCollaborationRecordCommand command) {
        recordApplicationService.updateRecord(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "删除合作记录")
    @PreAuthorize("@permission.has('collaboration:record:remove')")
    @AccessLog(title = "合作记录", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        recordApplicationService.deleteRecord(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

}
