package com.reading.platform.util;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class InitDatabase {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://8.148.151.56:3306/reading_platform?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
        String user = "root";
        String password = "reading_platform_pwd";

        // Read SQL file
        String sqlFile = "init-data.sql";
        String sql = new String(Files.readAllBytes(Paths.get(sqlFile)));

        // Split by semicolon and execute each statement
        String[] statements = sql.split(";");

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            System.out.println("Connected to database successfully");

            for (String stmt : statements) {
                String trimmed = stmt.trim();
                if (!trimmed.isEmpty() && !trimmed.startsWith("--")) {
                    try (Statement statement = conn.createStatement()) {
                        System.out.println("Executing: " + trimmed.substring(0, Math.min(50, trimmed.length())) + "...");
                        statement.execute(trimmed);
                    } catch (Exception e) {
                        System.out.println("Error executing statement: " + e.getMessage());
                    }
                }
            }

            System.out.println("\n=== Initialization Complete ===");

            // Verify data
            try (Statement statement = conn.createStatement()) {
                System.out.println("\nVerifying admin user:");
                var rs = statement.executeQuery("SELECT id, username, name, status FROM t_admin_user WHERE username = 'admin'");
                while (rs.next()) {
                    System.out.println("  ID: " + rs.getString("id") + ", Username: " + rs.getString("username") +
                                     ", Name: " + rs.getString("name") + ", Status: " + rs.getString("status"));
                }

                System.out.println("\nVerifying teacher users:");
                rs = statement.executeQuery("SELECT id, username, name, status FROM t_teacher WHERE username IN ('teacher1', 'school')");
                while (rs.next()) {
                    System.out.println("  ID: " + rs.getString("id") + ", Username: " + rs.getString("username") +
                                     ", Name: " + rs.getString("name") + ", Status: " + rs.getString("status"));
                }

                System.out.println("\nVerifying parent users:");
                rs = statement.executeQuery("SELECT id, username, name, status FROM t_parent WHERE username = 'parent1'");
                while (rs.next()) {
                    System.out.println("  ID: " + rs.getString("id") + ", Username: " + rs.getString("username") +
                                     ", Name: " + rs.getString("name") + ", Status: " + rs.getString("status"));
                }
            }
        }
    }
}
