#!/bin/bash

# Check if input file is provided
if [ $# -ne 1 ]; then
    echo "Usage: $0 <sql_file>"
    exit 1
fi

SQL_FILE="$1"
BASE_DIR="/usr/src/app/sql_objects"  # Use absolute path

# Create directory structure with proper permissions
echo "Creating directory structure at $BASE_DIR/tables"
mkdir -p "$BASE_DIR/tables"
chmod -R 777 "$BASE_DIR"  # Give full permissions for testing

# Function to check if table is temporary
is_temporary_table() {
    local table_name="$1"
    if echo "$table_name" | grep -iE "^(#|tmp_|temp_|temporary_|import_|viewimport_)" >/dev/null; then
        return 0 # true in bash
    else
        return 1 # false in bash
    fi
}

# Function to clean filename
clean_filename() {
    # Remove brackets, convert dots and spaces to underscores, remove parentheses
    echo "$1" | sed 's/\[\|\]//g' | tr '.' '_' | tr ' ' '_' | sed 's/[()]//g' | sed 's/__*/_/g' | sed 's/_$//'
}

# Function to extract table name with better schema handling
extract_table_name() {
    local create_statement="$1"
    # First, try to match schema.table pattern with brackets
    local table_name=$(echo "$create_statement" | sed -nE 's/.*CREATE[[:space:]]+TABLE[[:space:]]+\[([^]]+)\]\.\[([^]]+)\].*/\1.\2/p')
    
    if [ -z "$table_name" ]; then
        # Try to match schema.table pattern without brackets
        table_name=$(echo "$create_statement" | sed -nE 's/.*CREATE[[:space:]]+TABLE[[:space:]]+([^.[:space:]]+)\.([^.[:space:]]+).*/\1.\2/p')
    fi
    
    if [ -z "$table_name" ]; then
        # Try to match just table name with brackets
        table_name=$(echo "$create_statement" | sed -nE 's/.*CREATE[[:space:]]+TABLE[[:space:]]+\[([^]]+)\].*/\1/p')
    fi
    
    if [ -z "$table_name" ]; then
        # Try to match just table name without brackets
        table_name=$(echo "$create_statement" | sed -nE 's/.*CREATE[[:space:]]+TABLE[[:space:]]+([^[:space:]]+).*/\1/p')
    fi
    
    echo "$table_name"
}

# Function to process each CREATE TABLE statement
process_table() {
    local table_statement="$1"
    local table_name=$(extract_table_name "$table_statement")
    
    # Debug output
    echo "Extracted table name: $table_name"
    
    # Skip if we couldn't extract table name
    if [ -z "$table_name" ]; then
        echo "Warning: Could not extract table name from statement:"
        echo "$table_statement" | head -n 1
        return
    fi
    
    # Check if it's a temporary table
    if is_temporary_table "$table_name"; then
        echo "Skipping temporary table: $table_name"
        return
    fi
    
    # Clean filename and create full path
    clean_name=$(clean_filename "${table_name}")
    output_file="$BASE_DIR/tables/${clean_name}.sql"
    
    echo "Attempting to write to: $output_file"
    # Create the file with proper error handling
    if ! echo "$table_statement" > "$output_file" 2>/dev/null; then
        echo "Error writing to $output_file"
        echo "Current permissions:"
        ls -la "$BASE_DIR/tables"
        return 1
    fi
    
    echo "Successfully created: $output_file"
}

# Main processing function
extract_tables() {
    local current_statement=""
    local capture_table=false
    
    # Read the file line by line
    while IFS= read -r line || [ -n "$line" ]; do
        # Skip empty lines and pure comment lines
        if [ -z "${line// }" ] || [[ "$line" =~ ^[[:space:]]*-- ]]; then
            continue
        fi
        
        # Check for CREATE TABLE statement
        if echo "$line" | grep -i "CREATE[[:space:]]\+TABLE" > /dev/null; then
            # Debug output
            echo "Found CREATE TABLE statement: $line"
            local table_name=$(extract_table_name "$line")
            echo "Extracted table name from line: $table_name"
            
            if ! is_temporary_table "$table_name"; then
                capture_table=true
                current_statement="$line"
            else
                echo "Skipping temporary table: $table_name"
            fi
            continue
        fi

        # If we're capturing a table
        if $capture_table; then
            current_statement+=$'\n'"$line"
            
            # If we find a GO statement, process the table
            if echo "$line" | grep -i "^[[:space:]]*GO[[:space:]]*$" > /dev/null; then
                process_table "$current_statement"
                capture_table=false
                current_statement=""
            fi
        fi
    done < "$SQL_FILE"

    # Process any remaining table statement
    if $capture_table && [ -n "$current_statement" ]; then
        process_table "$current_statement"
    fi
}

# Remove comment lines and process
echo "Starting table extraction from $SQL_FILE to $BASE_DIR/tables"
echo "Current working directory: $(pwd)"
echo "Directory contents before extraction:"
ls -la "$BASE_DIR/tables"

sed 's/--.*$//' "$SQL_FILE" | extract_tables

echo "Extraction completed. Directory contents after extraction:"
ls -la "$BASE_DIR/tables"