package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.dto.ApiResponse;
import com.dora.dto.AdminEmployeeDto;
import com.dora.service.AdminEmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

/**
 * 管理员员工管理控制器
 */
@RestController
@RequestMapping("/admin/employees")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "管理员员工管理", description = "管理员员工管理相关接口")
@RequireAdminOrStaff
public class AdminEmployeeController {

    private final AdminEmployeeService adminEmployeeService;

    @GetMapping("/list")
    @Operation(summary = "获取员工列表", description = "分页获取系统员工列表，支持多种筛选条件")
    public ApiResponse<AdminEmployeeDto.EmployeeListResponse> getEmployeeList(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer pageSize,
            @Parameter(description = "搜索关键词（用户名、姓名）") @RequestParam(required = false) String keyword,
            @Parameter(description = "角色筛选（0普通员工/1管理员）") @RequestParam(required = false) Integer role,
            @Parameter(description = "状态筛选（0禁用/1启用）") @RequestParam(required = false) Integer status,
            @Parameter(description = "排序字段", example = "create_time") @RequestParam(defaultValue = "create_time") String sortField,
            @Parameter(description = "排序方向", example = "desc") @RequestParam(defaultValue = "desc") String sortOrder) {
        
        log.info("收到获取员工列表请求，页码：{}，大小：{}，角色：{}", pageNum, pageSize, role);
        
        AdminEmployeeDto.EmployeeListRequest request = new AdminEmployeeDto.EmployeeListRequest();
        request.setPageNum(pageNum);
        request.setPageSize(pageSize);
        request.setKeyword(keyword);
        request.setRole(role);
        request.setStatus(status);
        request.setSortField(sortField);
        request.setSortOrder(sortOrder);
        
        return adminEmployeeService.getEmployeeList(request);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取员工详情", description = "获取指定员工的详细信息")
    public ApiResponse<AdminEmployeeDto.EmployeeDetailResponse> getEmployeeDetail(
            @Parameter(description = "员工ID") @PathVariable Long id) {
        
        log.info("收到获取员工详情请求，员工ID：{}", id);
        return adminEmployeeService.getEmployeeDetail(id);
    }

    @PostMapping
    @Operation(summary = "创建员工", description = "创建新员工账号")
    public ApiResponse<String> createEmployee(
            @Valid @RequestBody AdminEmployeeDto.EmployeeCreateRequest request) {
        
        log.info("收到创建员工请求，用户名：{}，角色：{}", request.getUsername(), request.getRole());
        return adminEmployeeService.createEmployee(request);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新员工信息", description = "更新员工基本信息")
    public ApiResponse<String> updateEmployee(
            @Parameter(description = "员工ID") @PathVariable Long id,
            @Valid @RequestBody AdminEmployeeDto.EmployeeUpdateRequest request) {
        
        log.info("收到更新员工信息请求，员工ID：{}", id);
        request.setId(id);
        return adminEmployeeService.updateEmployee(request);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除员工", description = "删除员工账号（软删除）")
    public ApiResponse<String> deleteEmployee(
            @Parameter(description = "员工ID") @PathVariable Long id) {
        
        log.info("收到删除员工请求，员工ID：{}", id);
        return adminEmployeeService.deleteEmployee(id);
    }

    @PostMapping("/{id}/reset-password")
    @Operation(summary = "重置员工密码", description = "管理员重置员工密码")
    public ApiResponse<String> resetEmployeePassword(
            @Parameter(description = "员工ID") @PathVariable Long id,
            @Parameter(description = "新密码") @RequestParam @NotBlank String password) {
        
        log.info("收到重置员工密码请求，员工ID：{}", id);
        
        AdminEmployeeDto.EmployeePasswordResetRequest request = new AdminEmployeeDto.EmployeePasswordResetRequest();
        request.setEmployeeId(id);
        request.setNewPassword(password);
        
        return adminEmployeeService.resetEmployeePassword(request);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新员工状态", description = "更新员工账号状态（启用/禁用）")
    public ApiResponse<String> updateEmployeeStatus(
            @Parameter(description = "员工ID") @PathVariable Long id,
            @Parameter(description = "状态值（0禁用/1启用）") @RequestParam @NotNull Integer status) {
        
        log.info("收到更新员工状态请求，员工ID：{}，新状态：{}", id, status);
        
        AdminEmployeeDto.EmployeeStatusUpdateRequest request = new AdminEmployeeDto.EmployeeStatusUpdateRequest();
        request.setEmployeeId(id);
        request.setStatus(status);
        
        return adminEmployeeService.updateEmployeeStatus(request);
    }
}
