package com.competition.modules.sys.controller;

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.sys.dto.CreateUserDto;
import com.competition.modules.sys.dto.UpdateUserDto;
import com.competition.modules.sys.service.ISysUserService;
import com.competition.modules.sys.service.ISysTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService userService;
    private final ISysTenantService tenantService;

    @PostMapping
    @Operation(summary = "创建用户")
    public Result<Map<String, Object>> create(@Valid @RequestBody CreateUserDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(userService.createUser(dto, tenantId));
    }

    @GetMapping("/stats")
    @Operation(summary = "用户统计")
    public Result<Map<String, Object>> getStats() {
        return Result.success(userService.getStats());
    }

    @GetMapping
    @Operation(summary = "查询用户列表")
    public Result<PageResult<Map<String, Object>>> findAll(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String userType,
            @RequestParam(required = false) Long filterTenantId,
            @RequestParam(required = false) String userSource,
            @RequestParam(required = false) String status) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperTenant = SecurityUtil.isSuperAdmin() || tenantService.isSuperTenant(tenantId);
        return Result.success(userService.findAll(page, pageSize, tenantId, keyword, isSuperTenant, userType, filterTenantId, userSource, status));
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询用户详情")
    public Result<Map<String, Object>> findOne(@PathVariable Long id) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperTenant = tenantService.isSuperTenant(tenantId);
        return Result.success(userService.findDetail(id, tenantId, isSuperTenant));
    }

    @PatchMapping("/{id}/status")
    @Operation(summary = "启用/禁用用户")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestBody Map<String, String> body) {
        String status = body.get("status");
        Long operatorId = SecurityUtil.getCurrentUserId();
        userService.updateStatus(id, status, operatorId);
        return Result.success();
    }

    @PatchMapping("/{id}")
    @Operation(summary = "更新用户")
    public Result<Map<String, Object>> update(@PathVariable Long id, @RequestBody UpdateUserDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperTenant = tenantService.isSuperTenant(tenantId);
        return Result.success(userService.updateUser(id, dto, isSuperTenant ? null : tenantId));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户")
    public Result<Void> remove(@PathVariable Long id) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        userService.removeUser(id, tenantId);
        return Result.success();
    }
}
