package com.caliverse.admin.history.service;

import com.caliverse.admin.domain.adminlog.FieldChange;
import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.global.component.transaction.TransactionIdManager;
import com.caliverse.admin.history.ChangeDetector;
import com.caliverse.admin.history.domain.MysqlHistoryLogInfo;
import com.caliverse.admin.history.entity.EDBOperationType;
import com.caliverse.admin.history.repository.MysqlHistoryLogRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class MysqlHistoryLogService {

    private final TransactionIdManager transactionIdManager;
    private final MysqlHistoryLogRepository mysqlHistoryLogRepository;

    public <T> void insertHistoryLog(HISTORYTYPE historyType,
                                                   String tableName,
                                                   String message,
                                                   T data,
                                                   String userId,
                                                   String userIP
    ){

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

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

        mysqlHistoryLogRepository.save(historyLog);
    }

    public <T> void updateHistoryLog(HISTORYTYPE historyType,
                                                   String tableName,
                                                   String message,
                                                   T beforeData,
                                                   T afterData,
                                                   String userId,
                                                   String userIP
    ){
        List<FieldChange> changes = ChangeDetector.detectChanges(
                beforeData,
                afterData
        );

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

        mysqlHistoryLogRepository.save(historyLog);
    }

    public <T> void deleteHistoryLog(HISTORYTYPE historyType,
                                                   String tableName,
                                                   String message,
                                                   T data,
                                                   String userId,
                                                   String userIP
    ){

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

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

        mysqlHistoryLogRepository.save(historyLog);
    }

    public List<MysqlHistoryLogInfo> getAllHistoryLogs() {
        return mysqlHistoryLogRepository.findAll();
    }

    public Optional<MysqlHistoryLogInfo> getHistoryLogById(String id) {
        return mysqlHistoryLogRepository.findById(id);
    }
}
