package com.writeoff.module.expert.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.common.model.ImportResult;
import com.writeoff.module.expert.dto.AddBankCardRequest;
import com.writeoff.module.expert.dto.CreateExpertRequest;
import com.writeoff.module.expert.dto.ExpertAssetUploadSignRequest;
import com.writeoff.module.expert.dto.ImportExpertsRequest;
import com.writeoff.module.expert.dto.MergeExpertRequest;
import com.writeoff.module.expert.model.ExpertBankCardInfo;
import com.writeoff.module.expert.model.ExpertInfo;
import com.writeoff.module.expert.service.PlatformExpertService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.PermissionDomain;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/platform/experts")
public class PlatformExpertController {
    private final PlatformExpertService platformExpertService;

    public PlatformExpertController(PlatformExpertService platformExpertService) {
        this.platformExpertService = platformExpertService;
    }

    @GetMapping
    @RequirePermission(value = "platform.expert.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_LIST")
    public ApiResponse<PageResult<ExpertInfo>> list(@RequestParam(value = "keyword", required = false) String keyword) {
        return ApiResponse.success(platformExpertService.list(keyword));
    }

    @GetMapping("/{id}")
    @RequirePermission(value = "platform.expert.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_DETAIL")
    public ApiResponse<ExpertInfo> get(@PathVariable("id") Long id) {
        return ApiResponse.success(platformExpertService.get(id));
    }

    @PostMapping
    @RequirePermission(value = "platform.expert.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_CREATE")
    public ApiResponse<ExpertInfo> create(@RequestBody @Valid CreateExpertRequest request) {
        return ApiResponse.success(platformExpertService.create(request));
    }

    @PostMapping("/upload-sign")
    @RequirePermission(value = "platform.expert.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_UPLOAD_SIGN")
    public ApiResponse<Map<String, Object>> uploadSign(@RequestBody @Valid ExpertAssetUploadSignRequest request) {
        return ApiResponse.success(platformExpertService.presignAssetUpload(request.getFileName(), request.getContentType()));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "platform.expert.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_UPDATE")
    public ApiResponse<ExpertInfo> update(@PathVariable("id") Long id, @RequestBody @Valid CreateExpertRequest request) {
        return ApiResponse.success(platformExpertService.update(id, request));
    }

    @PostMapping("/import")
    @RequirePermission(value = "platform.expert.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_IMPORT")
    public ApiResponse<ImportResult> importExperts(@RequestBody @Valid ImportExpertsRequest request) {
        return ApiResponse.success(platformExpertService.importExperts(request.getExperts()));
    }

    @GetMapping("/export")
    @RequirePermission(value = "platform.expert.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_EXPORT")
    public ApiResponse<Map<String, Object>> exportExperts() {
        return ApiResponse.success(platformExpertService.export());
    }

    @PostMapping("/{id}/merge")
    @RequirePermission(value = "platform.expert.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_MERGE")
    public ApiResponse<Map<String, Object>> merge(@PathVariable("id") Long id, @RequestBody @Valid MergeExpertRequest request) {
        return ApiResponse.success(platformExpertService.merge(id, request));
    }

    @GetMapping("/{id}/bank-cards")
    @RequirePermission(value = "platform.expert.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_CARD_LIST")
    public ApiResponse<List<ExpertBankCardInfo>> cards(@PathVariable("id") Long id) {
        return ApiResponse.success(platformExpertService.listCards(id));
    }

    @GetMapping("/{id}/bank-cards/{cardId}")
    @RequirePermission(value = "platform.expert.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_CARD_DETAIL")
    public ApiResponse<ExpertBankCardInfo> getCard(@PathVariable("id") Long id, @PathVariable("cardId") Long cardId) {
        return ApiResponse.success(platformExpertService.getCard(id, cardId));
    }

    @PostMapping("/{id}/bank-cards")
    @RequirePermission(value = "platform.expert.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_CARD_CREATE")
    public ApiResponse<ExpertBankCardInfo> addCard(@PathVariable("id") Long id, @RequestBody @Valid AddBankCardRequest request) {
        return ApiResponse.success(platformExpertService.addCard(id, request));
    }

    @PutMapping("/{id}/bank-cards/{cardId}")
    @RequirePermission(value = "platform.expert.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_EXPERT_CARD_UPDATE")
    public ApiResponse<ExpertBankCardInfo> updateCard(@PathVariable("id") Long id, @PathVariable("cardId") Long cardId, @RequestBody @Valid AddBankCardRequest request) {
        return ApiResponse.success(platformExpertService.updateCard(id, cardId, request));
    }
}
