package com.aiteacher.enrichment; import com.aiteacher.book.Book; import com.aiteacher.book.BookRepository; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.NoSuchElementException; import java.util.UUID; @RestController @RequestMapping("/api/v1/admin/books/{id}/enrich") public class EnrichmentController { private final BookRepository bookRepository; private final EnrichmentBackfillService backfillService; public EnrichmentController(BookRepository bookRepository, EnrichmentBackfillService backfillService) { this.bookRepository = bookRepository; this.backfillService = backfillService; } @PostMapping public ResponseEntity start(@PathVariable UUID id) { Book book = bookRepository.findById(id) .orElseThrow(() -> new NoSuchElementException("Book not found.")); backfillService.backfillBook(id, book.getTitle()); int total = backfillService.countTotalTextChunks(id); int enriched = backfillService.countEnrichedChunks(id).orElse(0); return ResponseEntity.status(HttpStatus.ACCEPTED) .body(new EnrichmentBackfillService.BackfillProgress("RUNNING", total, enriched, null)); } @GetMapping public ResponseEntity status(@PathVariable UUID id) { bookRepository.findById(id) .orElseThrow(() -> new NoSuchElementException("Book not found.")); EnrichmentBackfillService.BackfillProgress progress = backfillService.getProgress(id); if ("IDLE".equals(progress.status())) { int total = backfillService.countTotalTextChunks(id); int enriched = backfillService.countEnrichedChunks(id).orElse(0); progress = new EnrichmentBackfillService.BackfillProgress( enriched >= total && total > 0 ? "COMPLETED" : "IDLE", total, enriched, null); } return ResponseEntity.ok(progress); } }