package research.loghunter.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import research.loghunter.service.ExportService;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/export")
@RequiredArgsConstructor
public class ExportController {

    private final ExportService exportService;

    @GetMapping("/html")
    public ResponseEntity<byte[]> exportHtml(
            @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
    ) {
        ExportService.ExportRequest request = new ExportService.ExportRequest(
                serverId, patternId, severity, filePath, startDate, endDate, keyword);
        
        ExportService.ExportResult result = exportService.exportHtml(request);
        
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, 
                        "attachment; filename=\"" + encodeFilename(result.filename()) + "\"")
                .contentType(MediaType.TEXT_HTML)
                .body(result.content());
    }

    @GetMapping("/txt")
    public ResponseEntity<byte[]> exportTxt(
            @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
    ) {
        ExportService.ExportRequest request = new ExportService.ExportRequest(
                serverId, patternId, severity, filePath, startDate, endDate, keyword);
        
        ExportService.ExportResult result = exportService.exportTxt(request);
        
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, 
                        "attachment; filename=\"" + encodeFilename(result.filename()) + "\"")
                .contentType(MediaType.TEXT_PLAIN)
                .body(result.content());
    }

    private String encodeFilename(String filename) {
        return URLEncoder.encode(filename, StandardCharsets.UTF_8)
                .replace("+", "%20");
    }
}
