package cn.novalon.gym.manage.db.config;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

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

class FlywayMigrationScriptTest {

    @Test
    void testMigrationScriptsExist() throws IOException {
        Path migrationDir = Paths.get("src/main/resources/db/migration");
        
        assertTrue(Files.exists(migrationDir), "Migration directory should exist");
        
        List<Path> sqlFiles = Files.list(migrationDir)
                .filter(p -> p.toString().endsWith(".sql"))
                .sorted()
                .collect(Collectors.toList());
        
        assertFalse(sqlFiles.isEmpty(), "Should have migration scripts");
        
        System.out.println("Found migration scripts:");
        sqlFiles.forEach(p -> System.out.println("  - " + p.getFileName()));
    }

    @Test
    void testMigrationScriptNaming() throws IOException {
        Path migrationDir = Paths.get("src/main/resources/db/migration");
        
        List<Path> sqlFiles = Files.list(migrationDir)
                .filter(p -> p.toString().endsWith(".sql"))
                .collect(Collectors.toList());
        
        for (Path file : sqlFiles) {
            String filename = file.getFileName().toString();
            assertTrue(filename.matches("V\\d+__.*\\.sql"), 
                    "Migration script should follow Flyway naming convention: " + filename);
        }
    }

    @Test
    void testMigrationScriptContent() throws IOException {
        Path migrationDir = Paths.get("src/main/resources/db/migration");
        
        List<Path> sqlFiles = Files.list(migrationDir)
                .filter(p -> p.toString().endsWith(".sql"))
                .sorted()
                .collect(Collectors.toList());
        
        for (Path file : sqlFiles) {
            String content = Files.readString(file);
            assertNotNull(content, "Migration script should have content: " + file.getFileName());
            assertFalse(content.trim().isEmpty(), "Migration script should not be empty: " + file.getFileName());
            
            if (content.contains("CREATE TABLE")) {
                assertTrue(content.contains("IF NOT EXISTS"), 
                        "CREATE TABLE statements should use IF NOT EXISTS: " + file.getFileName());
            }
        }
    }

    @Test
    void testMigrationScriptVersionOrder() throws IOException {
        Path migrationDir = Paths.get("src/main/resources/db/migration");
        
        List<Path> sqlFiles = Files.list(migrationDir)
                .filter(p -> p.toString().endsWith(".sql"))
                .collect(Collectors.toList());
        
        List<Integer> versions = sqlFiles.stream()
                .map(p -> {
                    String filename = p.getFileName().toString();
                    String versionStr = filename.substring(1, filename.indexOf("__"));
                    return Integer.parseInt(versionStr);
                })
                .sorted()
                .collect(Collectors.toList());
        
        for (int i = 1; i < versions.size(); i++) {
            assertTrue(versions.get(i) > versions.get(i - 1), 
                    "Migration versions should be in ascending order");
        }
    }
}