package com.qqchen.deploy.backend.system.api;

import com.qqchen.deploy.backend.system.model.query.UserQuery;
import com.qqchen.deploy.backend.system.model.request.DepartmentAssignRequest;
import com.qqchen.deploy.backend.system.model.request.ResetPasswordRequest;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.system.entity.User;
import com.qqchen.deploy.backend.system.model.UserDTO;
import com.qqchen.deploy.backend.system.model.request.LoginRequest;
import com.qqchen.deploy.backend.system.model.response.LoginResponse;
import com.qqchen.deploy.backend.system.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/v1/user")
public class UserApiController extends BaseController<User, UserDTO, Long, UserQuery> {

    @Resource
    private IUserService userService;

    @Override
    public Response<UserDTO> create(@Validated @RequestBody UserDTO dto) {
        return super.create(dto);
    }

    @Override
    public Response<UserDTO> update(@PathVariable Long id, @Validated @RequestBody UserDTO dto) {
        return super.update(id, dto);
    }

    @Override
    public Response<Void> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @Override
    public Response<UserDTO> findById(@PathVariable Long id) {
        return super.findById(id);
    }

    @Override
    public Response<List<UserDTO>> findAll() {
        return super.findAll();
    }

    @Override
    public Response<Page<UserDTO>> page(UserQuery query) {
        return super.page(query);
    }

    @Override
    public Response<List<UserDTO>> findAll(UserQuery query) {
        return super.findAll(query);
    }

    @Override
    public CompletableFuture<Response<Void>> batchProcess(List<UserDTO> dtos) {
        return super.batchProcess(dtos);
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Response<LoginResponse> login(@Validated @RequestBody LoginRequest request, HttpServletRequest httpRequest) {
        return Response.success(userService.login(request, httpRequest));
    }

    @Operation(summary = "获取当前用户信息")
    @GetMapping("/current")
    public Response<LoginResponse> getCurrentUser() {
        return Response.success(userService.getCurrentUserResponse());
    }

    @Operation(summary = "重置用户密码")
    @PostMapping("/{id}/reset-password")
    public Response<Void> resetPassword(@PathVariable Long id, @Validated @RequestBody ResetPasswordRequest request) {
        userService.resetPassword(id, request.getNewPassword());
        return Response.success();
    }

    @PutMapping("/{id}/department")
    @Operation(summary = "分配用户部门")
    public Response<Void> assignDepartment(
        @PathVariable Long id,  // 用户ID
        @RequestBody DepartmentAssignRequest request
    ) {
        userService.assignDepartment(id, request.getDepartmentId());
        return Response.success();
    }

    @PostMapping("/{id}/roles")
    @Operation(summary = "分配用户角色")
    public Response<Void> assignRoles(@PathVariable Long id, @RequestBody List<Long> roleIds) {
        userService.assignRoles(id, roleIds);
        return Response.success();
    }

    @Override
    protected void exportData(HttpServletResponse response, List<UserDTO> data) {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=users.xlsx");

        try (OutputStream out = response.getOutputStream()) {
            // ExcelUtils.export(data, out);
        } catch (IOException e) {
            throw new RuntimeException("Export failed", e);
        }
    }

}