import java.sql.Connection;
import java.sql.Statement;

public class Migration {
    public static void run(Connection conn) {
        try (Statement stmt = conn.createStatement()) {
            System.out.println("Running migrations...");
            stmt.execute("""
                CREATE TABLE IF NOT EXISTS users (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL,
                    email TEXT NOT NULL UNIQUE,
                    password_hash TEXT NOT NULL
                )
            """);

            stmt.execute("""
                CREATE TABLE IF NOT EXISTS accounts (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    owner_id INTEGER NOT NULL,
                    type TEXT CHECK(type IN ('CHECKING','SAVINGS','CREDIT')) NOT NULL,
                    account_number TEXT NOT NULL UNIQUE,
                    bank_code TEXT NOT NULL,
                    balance REAL NOT NULL DEFAULT 0,
                    FOREIGN KEY(owner_id) REFERENCES users(id)
                )
            """);

            stmt.execute("""
                CREATE TABLE IF NOT EXISTS giro_accounts (
                    id INTEGER PRIMARY KEY,
                    overdraft_limit REAL DEFAULT 0,
                    FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE
                )
            """);

            stmt.execute("""
                CREATE TABLE IF NOT EXISTS spar_accounts (
                    id INTEGER PRIMARY KEY,
                    interest_rate REAL DEFAULT 0,
                    FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE
                )
            """);

            stmt.execute("""
                CREATE TABLE IF NOT EXISTS kredit_accounts (
                    id INTEGER PRIMARY KEY,
                    credit_limit REAL DEFAULT 0,
                    repayment_plan TEXT,
                    FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE
                )
            """);

            stmt.execute("""
                CREATE TABLE IF NOT EXISTS storage (
                    key TEXT PRIMARY KEY,
                    type TEXT,
                    data TEXT NOT NULL
                )
            """);
            System.out.println("Migrations completed.");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
