package com.example.pdfservice.controller;

import com.example.pdfservice.dto.FileResponse;
import com.example.pdfservice.entity.FileDocument;
import com.example.pdfservice.service.FileStorageService;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/api/files")
public class FileController {

    private final FileStorageService fileStorageService;

    public FileController(FileStorageService fileStorageService) {
        this.fileStorageService = fileStorageService;
    }

    /** 6. List all files */
    @GetMapping
    public ResponseEntity<List<FileResponse>> listFiles() {
        return ResponseEntity.ok(fileStorageService.listAll());
    }

    /** 7. Upload a new PDF file (multipart/form-data, field name "file") */
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<FileResponse> uploadFile(@RequestParam("file") MultipartFile file) {
        FileResponse response = fileStorageService.store(file);
        return ResponseEntity.ok(response);
    }

    /** 7. Download a file as an attachment */
    @GetMapping("/{id}/download")
    public ResponseEntity<Resource> downloadFile(@PathVariable Long id) {
        FileDocument metadata = fileStorageService.getMetadata(id);
        Resource resource = fileStorageService.loadAsResource(id);

        String encodedName = URLEncoder.encode(metadata.getOriginalFileName(), StandardCharsets.UTF_8);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_PDF)
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment; filename=\"" + metadata.getOriginalFileName() + "\"; filename*=UTF-8''" + encodedName)
                .body(resource);
    }

    /** 8. Preview a file inline in the browser (e.g. inside an <iframe> or <embed>) */
    @GetMapping("/{id}/preview")
    public ResponseEntity<Resource> previewFile(@PathVariable Long id) {
        FileDocument metadata = fileStorageService.getMetadata(id);
        Resource resource = fileStorageService.loadAsResource(id);

        String encodedName = URLEncoder.encode(metadata.getOriginalFileName(), StandardCharsets.UTF_8);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_PDF)
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "inline; filename=\"" + metadata.getOriginalFileName() + "\"; filename*=UTF-8''" + encodedName)
                .body(resource);
    }

    /** Optional: delete a file */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFile(@PathVariable Long id) {
        fileStorageService.delete(id);
        return ResponseEntity.noContent().build();
    }
}
