package com.yimeng.okr.service;

import com.yimeng.okr.exception.BusinessException;
import org.junit.jupiter.api.Test;

import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.*;

class DatabaseBackupServiceTest {

    @Test
    void createBackupCopiesSqliteFileToBackupDirectory() throws Exception {
        Path temp = Files.createTempDirectory("okr-backup-test");
        Path db = temp.resolve("okr-performance.db");
        Files.writeString(db, "sqlite-content");
        Path backups = temp.resolve("backups");

        DatabaseBackupService service = new DatabaseBackupService("jdbc:sqlite:" + db, backups.toString());
        Path backup = service.createBackup();

        assertTrue(Files.exists(backup));
        assertEquals("sqlite-content", Files.readString(backup));
        assertTrue(backup.getFileName().toString().startsWith("okr-performance-"));
    }

    @Test
    void createBackupReturnsNullForMissingDatabaseFile() throws Exception {
        Path temp = Files.createTempDirectory("okr-backup-test");
        DatabaseBackupService service = new DatabaseBackupService(
                "jdbc:sqlite:" + temp.resolve("missing.db"), temp.resolve("backups").toString());

        Path backup = service.createBackup();
        assertNull(backup, "Should return null for missing file instead of throwing");
    }

    @Test
    void createBackupReturnsNullForInMemoryDatabase() {
        DatabaseBackupService service = new DatabaseBackupService(
                "jdbc:sqlite:file:test?mode=memory&cache=shared", "./data/backups");

        Path backup = service.createBackup();
        assertNull(backup, "Should return null for in-memory database");
    }
}
