package com.caliverse.admin.history.service;

import com.caliverse.admin.domain.adminlog.FieldChange;
import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.dynamodb.domain.doc.DynamoDBDocBase;
import com.caliverse.admin.global.component.transaction.TransactionIdManager;
import com.caliverse.admin.history.ChangeDetector;
import com.caliverse.admin.history.domain.DynamodbHistoryLogInfo;
import com.caliverse.admin.history.entity.EDBOperationType;
import com.caliverse.admin.history.repository.DynamodbHistoryLogRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class DynamodbHistoryLogService {
    @Value("${amazon.dynamodb.metaTable}")
    private String tableName;

    private final TransactionIdManager transactionIdManager;
    private final DynamodbHistoryLogRepository dynamodbHistoryLogRepository;

    public DynamodbHistoryLogInfo insertHistoryLog(HISTORYTYPE historyType,
                                                   String message,
                                                   DynamoDBDocBase metadata,
                                                   String userId,
                                                   String userIP
    ){

        List<FieldChange> changes = ChangeDetector.detectInsertChanges(metadata);

        DynamodbHistoryLogInfo historyLog = new DynamodbHistoryLogInfo(
                EDBOperationType.INSERT,
                historyType,
                tableName,
                message,
                transactionIdManager.getCurrentTransactionId(),
                changes,
                userId,
                userIP,
                metadata
        );

        return dynamodbHistoryLogRepository.save(historyLog);
    }

    public DynamodbHistoryLogInfo updateHistoryLog(HISTORYTYPE historyType,
                                                   String message,
                                                   DynamoDBDocBase beforeMetadata,
                                                   DynamoDBDocBase afterMetadata,
                                                   String userId,
                                                   String userIP
    ){
        List<FieldChange> changes = ChangeDetector.detectChanges(
                beforeMetadata,
                afterMetadata
        );

        DynamodbHistoryLogInfo historyLog = new DynamodbHistoryLogInfo(
                EDBOperationType.UPDATE,
                historyType,
                tableName,
                message,
                transactionIdManager.getCurrentTransactionId(),
                changes,
                userId,
                userIP,
                afterMetadata
        );

        return dynamodbHistoryLogRepository.save(historyLog);
    }

    public DynamodbHistoryLogInfo deleteHistoryLog(HISTORYTYPE historyType,
                                                   String message,
                                                   DynamoDBDocBase metadata,
                                                   String userId,
                                                   String userIP
    ){

        List<FieldChange> changes = ChangeDetector.detectDeleteChanges(metadata);

        DynamodbHistoryLogInfo historyLog = new DynamodbHistoryLogInfo(
                EDBOperationType.DELETE,
                historyType,
                tableName,
                message,
                transactionIdManager.getCurrentTransactionId(),
                changes,
                userId,
                userIP,
                metadata
        );

        return dynamodbHistoryLogRepository.save(historyLog);
    }

    public List<DynamodbHistoryLogInfo> getAllHistoryLogs() {
        return dynamodbHistoryLogRepository.findAll();
    }

    public Optional<DynamodbHistoryLogInfo> getHistoryLogById(String id) {
        return dynamodbHistoryLogRepository.findById(id);
    }
}
