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.ExpertService;
import com.writeoff.security.DataScopeType;
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/experts")
public class ExpertController {
    private final ExpertService expertService;

    public ExpertController(ExpertService expertService) {
        this.expertService = expertService;
    }

    @GetMapping
    @RequirePermission(value = "expert.read", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_LIST")
    public ApiResponse<PageResult<ExpertInfo>> list(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        return ApiResponse.success(expertService.list(keyword, pageNo, pageSize));
    }

    @GetMapping("/{id}")
    @RequirePermission(value = "expert.read", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_DETAIL")
    public ApiResponse<ExpertInfo> get(@PathVariable("id") Long id) {
        return ApiResponse.success(expertService.get(id));
    }

    @PostMapping
    @RequirePermission(value = "expert.create", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_CREATE")
    public ApiResponse<ExpertInfo> create(@RequestBody @Valid CreateExpertRequest request) {
        return ApiResponse.success(expertService.create(request));
    }

    @PostMapping("/upload-sign")
    @RequirePermission(value = "expert.create", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_UPLOAD_SIGN")
    public ApiResponse<Map<String, Object>> uploadSign(@RequestBody @Valid ExpertAssetUploadSignRequest request) {
        return ApiResponse.success(expertService.presignAssetUpload(request.getFileName(), request.getContentType()));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "expert.create", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_UPDATE")
    public ApiResponse<ExpertInfo> update(@PathVariable("id") Long id, @RequestBody @Valid CreateExpertRequest request) {
        return ApiResponse.success(expertService.update(id, request));
    }

    @PostMapping("/import")
    @RequirePermission(value = "expert.import", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_IMPORT")
    public ApiResponse<ImportResult> importExperts(@RequestBody @Valid ImportExpertsRequest request) {
        return ApiResponse.success(expertService.importExperts(request.getExperts()));
    }

    @GetMapping("/export")
    @RequirePermission(value = "expert.export", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_EXPORT")
    public ApiResponse<Map<String, Object>> exportExperts() {
        return ApiResponse.success(expertService.export());
    }

    @PostMapping("/{id}/merge")
    @RequirePermission(value = "expert.merge", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_MERGE")
    public ApiResponse<Map<String, Object>> merge(@PathVariable("id") Long id, @RequestBody @Valid MergeExpertRequest request) {
        return ApiResponse.success(expertService.merge(id, request));
    }

    @GetMapping("/{id}/bank-cards")
    @RequirePermission(value = "expert.read", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_CARD_LIST")
    public ApiResponse<List<ExpertBankCardInfo>> cards(@PathVariable("id") Long id) {
        return ApiResponse.success(expertService.listCards(id));
    }

    @GetMapping("/{id}/bank-cards/{cardId}")
    @RequirePermission(value = "expert.read", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_CARD_DETAIL")
    public ApiResponse<ExpertBankCardInfo> getCard(@PathVariable("id") Long id, @PathVariable("cardId") Long cardId) {
        return ApiResponse.success(expertService.getCard(id, cardId));
    }

    @PostMapping("/{id}/bank-cards")
    @RequirePermission(value = "expert.card.manage", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_CARD_CREATE")
    public ApiResponse<ExpertBankCardInfo> addCard(@PathVariable("id") Long id, @RequestBody @Valid AddBankCardRequest request) {
        return ApiResponse.success(expertService.addCard(id, request));
    }

    @PutMapping("/{id}/bank-cards/{cardId}")
    @RequirePermission(value = "expert.card.manage", dataScope = DataScopeType.TENANT, auditAction = "EXPERT_CARD_UPDATE")
    public ApiResponse<ExpertBankCardInfo> updateCard(@PathVariable("id") Long id, @PathVariable("cardId") Long cardId, @RequestBody @Valid AddBankCardRequest request) {
        return ApiResponse.success(expertService.updateCard(id, cardId, request));
    }
}
