package at.procon.ted.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Utility to inspect database objects that depend on ENUM types.
 */
@Slf4j
public class InspectDatabase {

    private static final String DB_URL = "jdbc:postgresql://94.130.218.54:5432/Sales";
    private static final String DB_USER = "postgres";
    private static final String DB_PASSWORD = "PDmXRx0Rbk9OFOn9qO5Gm/mPCfqW8zwbZ+/YIU1lySc=";

    public static void main(String[] args) {
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            log.info("Connected to database");

            // Check for views
            log.info("\n=== VIEWS ===");
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(
                     "SELECT schemaname, viewname FROM pg_views WHERE schemaname ILIKE 'ted'")) {
                while (rs.next()) {
                    log.info("View: {}.{}", rs.getString(1), rs.getString(2));
                }
            }

            // Check for functions
            log.info("\n=== FUNCTIONS ===");
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(
                     "SELECT n.nspname, p.proname FROM pg_proc p " +
                     "JOIN pg_namespace n ON p.pronamespace = n.oid " +
                     "WHERE n.nspname ILIKE 'ted'")) {
                while (rs.next()) {
                    log.info("Function: {}.{}", rs.getString(1), rs.getString(2));
                }
            }

            // Check for indexes
            log.info("\n=== INDEXES ===");
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(
                     "SELECT schemaname, tablename, indexname FROM pg_indexes WHERE schemaname ILIKE 'ted'")) {
                while (rs.next()) {
                    log.info("Index: {}.{} on table {}", rs.getString(1), rs.getString(3), rs.getString(2));
                }
            }

            // Check column types
            log.info("\n=== ENUM COLUMNS ===");
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(
                     "SELECT table_schema, table_name, column_name, udt_name " +
                     "FROM information_schema.columns " +
                     "WHERE table_schema ILIKE 'ted' AND table_name = 'procurement_document' " +
                     "AND column_name IN ('notice_type', 'contract_nature', 'procedure_type', 'vectorization_status')")) {
                while (rs.next()) {
                    log.info("Column: {}.{}.{} -> Type: {}",
                        rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
                }
            }

            // Check for ENUM types
            log.info("\n=== ENUM TYPES ===");
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(
                     "SELECT n.nspname, t.typname FROM pg_type t " +
                     "JOIN pg_namespace n ON t.typnamespace = n.oid " +
                     "WHERE n.nspname ILIKE 'ted' AND t.typtype = 'e'")) {
                while (rs.next()) {
                    log.info("ENUM Type: {}.{}", rs.getString(1), rs.getString(2));
                }
            }

        } catch (Exception e) {
            log.error("Error: {}", e.getMessage(), e);
        }
    }
}
