package com.storycove.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ImageProcessingProgressService {

    private static final Logger logger = LoggerFactory.getLogger(ImageProcessingProgressService.class);

    private final Map<UUID, ImageProcessingProgress> progressMap = new ConcurrentHashMap<>();

    public static class ImageProcessingProgress {
        private final UUID storyId;
        private final int totalImages;
        private volatile int processedImages;
        private volatile String currentImageUrl;
        private volatile String status;
        private volatile boolean completed;
        private volatile String errorMessage;

        public ImageProcessingProgress(UUID storyId, int totalImages) {
            this.storyId = storyId;
            this.totalImages = totalImages;
            this.processedImages = 0;
            this.status = "Starting";
            this.completed = false;
        }

        // Getters
        public UUID getStoryId() { return storyId; }
        public int getTotalImages() { return totalImages; }
        public int getProcessedImages() { return processedImages; }
        public String getCurrentImageUrl() { return currentImageUrl; }
        public String getStatus() { return status; }
        public boolean isCompleted() { return completed; }
        public String getErrorMessage() { return errorMessage; }
        public double getProgressPercentage() {
            return totalImages > 0 ? (double) processedImages / totalImages * 100 : 100;
        }

        // Setters
        public void setProcessedImages(int processedImages) { this.processedImages = processedImages; }
        public void setCurrentImageUrl(String currentImageUrl) { this.currentImageUrl = currentImageUrl; }
        public void setStatus(String status) { this.status = status; }
        public void setCompleted(boolean completed) { this.completed = completed; }
        public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }

        public void incrementProcessed() {
            this.processedImages++;
        }
    }

    public ImageProcessingProgress startProgress(UUID storyId, int totalImages) {
        ImageProcessingProgress progress = new ImageProcessingProgress(storyId, totalImages);
        progressMap.put(storyId, progress);
        logger.info("Started image processing progress tracking for story {} with {} images", storyId, totalImages);
        return progress;
    }

    public ImageProcessingProgress getProgress(UUID storyId) {
        return progressMap.get(storyId);
    }

    public void updateProgress(UUID storyId, int processedImages, String currentImageUrl, String status) {
        ImageProcessingProgress progress = progressMap.get(storyId);
        if (progress != null) {
            progress.setProcessedImages(processedImages);
            progress.setCurrentImageUrl(currentImageUrl);
            progress.setStatus(status);
            logger.debug("Updated progress for story {}: {}/{} - {}", storyId, processedImages, progress.getTotalImages(), status);
        }
    }

    public void completeProgress(UUID storyId, String finalStatus) {
        ImageProcessingProgress progress = progressMap.get(storyId);
        if (progress != null) {
            progress.setCompleted(true);
            progress.setStatus(finalStatus);
            logger.info("Completed image processing for story {}: {}", storyId, finalStatus);
        }
    }

    public void setError(UUID storyId, String errorMessage) {
        ImageProcessingProgress progress = progressMap.get(storyId);
        if (progress != null) {
            progress.setErrorMessage(errorMessage);
            progress.setStatus("Error: " + errorMessage);
            progress.setCompleted(true);
            logger.error("Image processing error for story {}: {}", storyId, errorMessage);
        }
    }

    public void removeProgress(UUID storyId) {
        progressMap.remove(storyId);
        logger.debug("Removed progress tracking for story {}", storyId);
    }

    public boolean isProcessing(UUID storyId) {
        ImageProcessingProgress progress = progressMap.get(storyId);
        return progress != null && !progress.isCompleted();
    }
}