package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.common.exception.BusinessException;
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.AuthContext;
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;

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

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

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

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

    @PostMapping("/logo-upload-sign")
    @RequirePermission(value = "tenant.manage", dataScope = DataScopeType.TENANT, auditAction = "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()));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "tenant.manage", dataScope = DataScopeType.TENANT, auditAction = "TENANT_UPDATE")
    public ApiResponse<TenantInfo> update(@PathVariable("id") Long id,
                                          @RequestBody @Valid UpdateTenantRequest request) {
        Long currentTenantId = AuthContext.requireTenantId();
        if (!currentTenantId.equals(id)) {
            throw new BusinessException(10003, "租户不存在");
        }
        return ApiResponse.success(tenantService.updateTenant(id, request.getTenantName(), request.getLogoUrl()));
    }

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