package dev.lions.unionflow.server.service;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.doReturn;

import dev.lions.unionflow.server.api.dto.backup.request.RestoreBackupRequest;
import dev.lions.unionflow.server.api.dto.backup.response.BackupResponse;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectSpy;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
 * Tests de couverture pour BackupService.restoreBackup().
 * Couvre la branche où le statut de la sauvegarde n'est PAS "COMPLETED".
 */
@QuarkusTest
class BackupServiceNotCompletedTest {

    @InjectSpy
    BackupService backupService;

    @InjectMock
    SecurityIdentity securityIdentity;

    private static final UUID FIXED_ID = UUID.fromString("bbbbbbbb-cccc-dddd-eeee-ffffffffffff");

    @BeforeEach
    void setUp() {
        java.security.Principal principal = () -> "test-user";
        org.mockito.Mockito.when(securityIdentity.getPrincipal()).thenReturn(principal);
    }

    @Test
    @DisplayName("restoreBackup avec sauvegarde IN_PROGRESS lance RuntimeException (statut non COMPLETED)")
    void restoreBackup_inProgressStatus_throwsRuntimeException() {
        BackupResponse inProgressBackup = BackupResponse.builder()
                .id(FIXED_ID)
                .name("Sauvegarde en cours")
                .description("Test statut IN_PROGRESS")
                .type("AUTO")
                .sizeBytes(500_000L)
                .sizeFormatted("500 KB")
                .status("IN_PROGRESS")
                .createdAt(LocalDateTime.now().minusMinutes(10))
                .createdBy("test-user")
                .includesDatabase(true)
                .includesFiles(true)
                .includesConfiguration(true)
                .filePath("/backups/in-progress.zip")
                .build();

        // getAllBackups() retourne la sauvegarde IN_PROGRESS avec ID fixe
        doReturn(List.of(inProgressBackup)).when(backupService).getAllBackups();

        RestoreBackupRequest request = RestoreBackupRequest.builder()
                .backupId(FIXED_ID)
                .createRestorePoint(false)
                .restoreDatabase(true)
                .restoreFiles(false)
                .restoreConfiguration(false)
                .build();

        // La branche "!COMPLETED" doit lancer RuntimeException
        assertThatThrownBy(() -> backupService.restoreBackup(request))
                .isInstanceOf(RuntimeException.class)
                .hasMessageContaining("complétée pour être restaurée");
    }

    @Test
    @DisplayName("restoreBackup avec sauvegarde FAILED lance RuntimeException (statut non COMPLETED)")
    void restoreBackup_failedStatus_throwsRuntimeException() {
        BackupResponse failedBackup = BackupResponse.builder()
                .id(FIXED_ID)
                .name("Sauvegarde échouée")
                .description("Test statut FAILED")
                .type("AUTO")
                .sizeBytes(0L)
                .sizeFormatted("0 B")
                .status("FAILED")
                .createdAt(LocalDateTime.now().minusHours(1))
                .createdBy("system")
                .includesDatabase(true)
                .includesFiles(false)
                .includesConfiguration(false)
                .filePath("/backups/failed.zip")
                .build();

        doReturn(List.of(failedBackup)).when(backupService).getAllBackups();

        RestoreBackupRequest request = RestoreBackupRequest.builder()
                .backupId(FIXED_ID)
                .createRestorePoint(false)
                .restoreDatabase(false)
                .restoreFiles(false)
                .restoreConfiguration(false)
                .build();

        assertThatThrownBy(() -> backupService.restoreBackup(request))
                .isInstanceOf(RuntimeException.class)
                .hasMessageContaining("complétée pour être restaurée");
    }
}
