package com.dne.ems.controller;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dne.ems.dto.OperationLogDTO;
import com.dne.ems.model.enums.OperationType;
import com.dne.ems.security.CustomUserDetails;
import com.dne.ems.service.OperationLogService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 操作日志控制器，提供查询用户操作日志的API。
 *
 * @version 1.0
 * @since 2025-06-20
 */
@RestController
@RequestMapping("/api/logs")
@Tag(name = "操作日志管理", description = "提供操作日志查询功能")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    /**
     * 获取当前用户的操作日志
     *
     * @param userDetails 当前用户详情
     * @return 操作日志列表
     */
    @GetMapping("/my-logs")
    @Operation(summary = "获取当前用户的操作日志", description = "返回当前登录用户的所有操作记录")
    public ResponseEntity<List<OperationLogDTO>> getMyOperationLogs(
            @AuthenticationPrincipal CustomUserDetails userDetails) {
        
        Long userId = userDetails.getUserAccount().getId();
        List<OperationLogDTO> logs = operationLogService.getUserOperationLogs(userId);
        return ResponseEntity.ok(logs);
    }

    /**
     * 获取所有用户的操作日志（仅限管理员）
     *
     * @param operationType 操作类型（可选）
     * @param userId 用户ID（可选）
     * @param startTime 开始时间（可选）
     * @param endTime 结束时间（可选）
     * @return 操作日志列表
     */
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    @Operation(summary = "获取所有操作日志", description = "管理员可以查询所有用户的操作日志，支持多种过滤条件")
    public ResponseEntity<List<OperationLogDTO>> getAllOperationLogs(
            @RequestParam(required = false) 
            @Parameter(description = "操作类型") 
            OperationType operationType,
            
            @RequestParam(required = false) 
            @Parameter(description = "用户ID") 
            Long userId,
            
            @RequestParam(required = false) 
            @Parameter(description = "开始时间，格式：yyyy-MM-dd'T'HH:mm:ss") 
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 
            LocalDateTime startTime,
            
            @RequestParam(required = false) 
            @Parameter(description = "结束时间，格式：yyyy-MM-dd'T'HH:mm:ss") 
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 
            LocalDateTime endTime) {
        
        List<OperationLogDTO> logs = operationLogService.queryOperationLogs(userId, operationType, startTime, endTime);
        return ResponseEntity.ok(logs);
    }
} 