package com.adityachandel.booklore.task.tasks;

import com.adityachandel.booklore.model.dto.request.TaskCreateRequest;
import com.adityachandel.booklore.model.dto.response.TaskCreateResponse;
import com.adityachandel.booklore.model.enums.TaskType;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.task.TaskStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;

@Component
@RequiredArgsConstructor
@Slf4j
public class DeletedBooksCleanupTask implements Task {

    private final BookRepository bookRepository;

    @Override
    public TaskCreateResponse execute(TaskCreateRequest request) {
        TaskCreateResponse.TaskCreateResponseBuilder builder = TaskCreateResponse.builder()
                .taskId(UUID.randomUUID().toString())
                .taskType(getTaskType());

        long startTime = System.currentTimeMillis();
        log.info("{}: Task started", getTaskType());

        try {
            int deletedCount;
            if (request.isTriggeredByCron()) {
                Instant cutoff = Instant.now().minus(7, ChronoUnit.DAYS);
                deletedCount = bookRepository.deleteSoftDeletedBefore(cutoff);
                log.info("{}: Removed {} deleted books older than {}", getTaskType(), deletedCount, cutoff);
            } else {
                deletedCount = bookRepository.deleteAllSoftDeleted();
                log.info("{}: Removed all {} deleted books (on-demand execution)", getTaskType(), deletedCount);
            }
            builder.status(TaskStatus.COMPLETED);
        } catch (Exception e) {
            log.error("{}: Error cleaning up deleted books", getTaskType(), e);
            builder.status(TaskStatus.FAILED);
        }

        long endTime = System.currentTimeMillis();
        log.info("{}: Task completed. Duration: {} ms", getTaskType(), endTime - startTime);

        return builder.build();
    }

    @Override
    public TaskType getTaskType() {
        return TaskType.CLEANUP_DELETED_BOOKS;
    }

    @Override
    public String getMetadata() {
        long deleted = bookRepository.countAllSoftDeleted();
        return "Book" + (deleted != 1 ? "s" : "") + " pending cleanup: " + deleted;
    }
}