package de.oaa.xxx.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class SchemaMigration implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(SchemaMigration.class);
    private final JdbcTemplate jdbc;

    public SchemaMigration(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }

    @Override
    public void run(ApplicationArguments args) {
        try {
            String columnType = jdbc.queryForObject(
                "SELECT DATA_TYPE FROM information_schema.COLUMNS " +
                "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'verification' AND COLUMN_NAME = 'image'",
                String.class);
            if ("blob".equalsIgnoreCase(columnType)) {
                log.info("Migrating verification.image from BLOB to MEDIUMBLOB");
                jdbc.execute("ALTER TABLE verification MODIFY COLUMN image MEDIUMBLOB");
                log.info("Migration complete");
            }
        } catch (Exception e) {
            log.warn("Schema migration check failed: {}", e.getMessage());
        }
    }
}
