package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.system.dto.CreateTenantAdminRequest;
import com.writeoff.module.system.dto.CreateTenantRequest;
import com.writeoff.module.system.dto.EnterpriseLogoUploadSignRequest;
import com.writeoff.module.system.dto.UpdateTenantRequest;
import com.writeoff.module.system.model.TenantInfo;
import com.writeoff.module.system.service.TenantService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.PermissionDomain;
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.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/platform/tenants")
public class PlatformTenantController {
    private final TenantService tenantService;

    public PlatformTenantController(TenantService tenantService) {
        this.tenantService = tenantService;
    }

    @GetMapping
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_LIST")
    public ApiResponse<PageResult<TenantInfo>> list() {
        return ApiResponse.success(tenantService.list());
    }

    @GetMapping("/{id}/admin")
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_GET_ADMIN")
    public ApiResponse<java.util.Map<String, Object>> getAdmin(@PathVariable("id") Long id) {
        return ApiResponse.success(tenantService.getTenantAdmin(id, "TENANT_ADMIN"));
    }

    @PostMapping
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_CREATE")
    public ApiResponse<TenantInfo> create(@RequestBody @Valid CreateTenantRequest request) {
        return ApiResponse.success(tenantService.create(request));
    }

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

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        tenantService.enable(id);
        return ApiResponse.success("ok");
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        tenantService.disable(id);
        return ApiResponse.success("ok");
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_UPDATE")
    public ApiResponse<TenantInfo> update(@PathVariable("id") Long id,
                                          @RequestBody @Valid UpdateTenantRequest request) {
        return ApiResponse.success(tenantService.updateTenant(id, request.getTenantName(), request.getLogoUrl()));
    }

    @PostMapping("/{id}/delete")
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_DELETE")
    public ApiResponse<String> softDelete(@PathVariable("id") Long id) {
        tenantService.softDelete(id);
        return ApiResponse.success("ok");
    }

    @PostMapping("/{id}/admin")
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_SET_ADMIN")
    public ApiResponse<java.util.Map<String, Object>> setAdmin(@PathVariable("id") Long id,
                                                               @RequestBody @Valid CreateTenantAdminRequest request) {
        return ApiResponse.success(tenantService.createTenantAdmin(id, request));
    }

    @PostMapping("/{id}/init-baseline")
    @RequirePermission(value = "platform.tenant.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.TENANT, auditAction = "PLATFORM_TENANT_INIT_BASELINE")
    public ApiResponse<java.util.Map<String, Object>> initBaseline(@PathVariable("id") Long id) {
        return ApiResponse.success(tenantService.initTenantBaseline(id));
    }
}
