package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.system.dto.CreateEnterpriseRequest;
import com.writeoff.module.system.dto.EnterpriseLogoUploadSignRequest;
import com.writeoff.module.system.dto.UpdateEnterpriseRequest;
import com.writeoff.module.system.model.EnterpriseInfo;
import com.writeoff.module.system.service.EnterpriseService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
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;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/enterprises")
public class EnterpriseController {
    private final EnterpriseService enterpriseService;

    public EnterpriseController(EnterpriseService enterpriseService) {
        this.enterpriseService = enterpriseService;
    }

    @GetMapping
    @RequirePermission(value = "enterprise.read", dataScope = DataScopeType.TENANT, auditAction = "ENTERPRISE_LIST")
    public ApiResponse<PageResult<EnterpriseInfo>> list(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        return ApiResponse.success(enterpriseService.list(pageNo, pageSize));
    }

    @PostMapping
    @RequirePermission(value = "enterprise.manage", dataScope = DataScopeType.TENANT, auditAction = "ENTERPRISE_CREATE")
    public ApiResponse<EnterpriseInfo> create(@RequestBody @Valid CreateEnterpriseRequest request) {
        return ApiResponse.success(enterpriseService.create(request));
    }

    @PostMapping("/logo-upload-sign")
    @RequirePermission(value = "enterprise.manage", dataScope = DataScopeType.TENANT, auditAction = "ENTERPRISE_LOGO_UPLOAD_SIGN")
    public ApiResponse<java.util.Map<String, Object>> logoUploadSign(@RequestBody @Valid EnterpriseLogoUploadSignRequest request) {
        return ApiResponse.success(enterpriseService.presignLogoUpload(request.getFileName(), request.getContentType()));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "enterprise.manage", dataScope = DataScopeType.TENANT, auditAction = "ENTERPRISE_UPDATE")
    public ApiResponse<EnterpriseInfo> update(@PathVariable("id") Long id, @RequestBody @Valid UpdateEnterpriseRequest request) {
        return ApiResponse.success(enterpriseService.update(id, request));
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "enterprise.manage", dataScope = DataScopeType.TENANT, auditAction = "ENTERPRISE_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        enterpriseService.enable(id);
        return ApiResponse.success("ok");
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "enterprise.manage", dataScope = DataScopeType.TENANT, auditAction = "ENTERPRISE_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        enterpriseService.disable(id);
        return ApiResponse.success("ok");
    }

    @PostMapping("/{id}/delete")
    @RequirePermission(value = "enterprise.delete", dataScope = DataScopeType.TENANT, auditAction = "ENTERPRISE_DELETE")
    public ApiResponse<String> softDelete(@PathVariable("id") Long id) {
        enterpriseService.softDelete(id);
        return ApiResponse.success("ok");
    }
}
