package research.loghunter.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import research.loghunter.dto.ErrorLogDto;
import research.loghunter.dto.FileTreeDto;
import research.loghunter.service.ErrorLogService;

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

@RestController
@RequestMapping("/api/error-logs")
@RequiredArgsConstructor
public class ErrorLogController {

    private final ErrorLogService errorLogService;

    @GetMapping
    public ResponseEntity<Page<ErrorLogDto>> search(
            @RequestParam(required = false) Long serverId,
            @RequestParam(required = false) Long patternId,
            @RequestParam(required = false) String severity,
            @RequestParam(required = false) String filePath,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return ResponseEntity.ok(errorLogService.search(
                serverId, patternId, severity, filePath, startDate, endDate, keyword, page, size));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ErrorLogDto> findById(@PathVariable Long id) {
        return ResponseEntity.ok(errorLogService.findById(id));
    }

    @GetMapping("/server/{serverId}")
    public ResponseEntity<Page<ErrorLogDto>> findByServerId(
            @PathVariable Long serverId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return ResponseEntity.ok(errorLogService.findByServerId(serverId, page, size));
    }

    /**
     * 트리 구조 데이터 조회 (서버 > 로그경로 > 파일)
     */
    @GetMapping("/tree")
    public ResponseEntity<List<FileTreeDto.ServerNode>> getFileTree() {
        return ResponseEntity.ok(errorLogService.getFileTree());
    }

    /**
     * 서버별 파일 목록 조회
     */
    @GetMapping("/files")
    public ResponseEntity<List<String>> getFilesByServer(
            @RequestParam(required = false) Long serverId
    ) {
        return ResponseEntity.ok(errorLogService.getFilesByServer(serverId));
    }

    /**
     * 선택한 에러 삭제
     */
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> deleteByIds(@RequestBody List<Long> ids) {
        int deleted = errorLogService.deleteByIds(ids);
        return ResponseEntity.ok(Map.of(
                "success", true,
                "deleted", deleted
        ));
    }

    /**
     * 파일별 에러 및 스캔기록 삭제
     */
    @DeleteMapping("/by-file")
    public ResponseEntity<Map<String, Object>> deleteByFile(
            @RequestParam Long serverId,
            @RequestParam String filePath
    ) {
        Map<String, Object> result = errorLogService.deleteFileAndErrors(serverId, filePath);
        return ResponseEntity.ok(result);
    }
}
