package com.agileboot.admin.controller.app;

import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
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.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("/app/collaboration/record")
@Validated
@RequiredArgsConstructor
public class AppCollaborationRecordController {

    private final CollaborationRecordApplicationService recordApplicationService;

    @Operation(summary = "小程序合作记录列表")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<CollaborationRecordDTO>> list(CollaborationRecordQuery query) {
        return ResponseDTO.ok(recordApplicationService.getRecordList(query));
    }

    @Operation(summary = "小程序合作记录详情")
    @GetMapping("/{recordId}")
    public ResponseDTO<CollaborationRecordDetailDTO> getInfo(@PathVariable @Positive Long recordId) {
        return ResponseDTO.ok(recordApplicationService.getRecordInfo(recordId));
    }

    @Operation(summary = "小程序合作记录选项")
    @GetMapping("/options")
    public ResponseDTO<List<CollaborationOptionDTO>> options() {
        return ResponseDTO.ok(recordApplicationService.getOptions());
    }

    @Operation(summary = "小程序合作记录月度统计")
    @GetMapping("/monthly-statistics")
    public ResponseDTO<List<CollaborationMonthlyStatisticsDTO>> monthlyStatistics(@RequestParam Integer year) {
        return ResponseDTO.ok(recordApplicationService.getMonthlyStatistics(year));
    }

    @Operation(summary = "小程序新增合作记录")
    @PostMapping
    public ResponseDTO<Void> add(@Valid @RequestBody AddCollaborationRecordCommand command) {
        recordApplicationService.addRecord(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "小程序修改合作记录")
    @PutMapping
    public ResponseDTO<Void> edit(@Valid @RequestBody UpdateCollaborationRecordCommand command) {
        recordApplicationService.updateRecord(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "小程序删除合作记录")
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        recordApplicationService.deleteRecord(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

}
