import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

class DataPreprocessor:
    """
    Handles preprocessing tasks like feature type identification and scaling.
    """
    def __init__(self):
        self.scaler = StandardScaler()
        self.numerical_features_ = []
        self.categorical_features_ = []

    def preprocess(self, X):
        """
        Separates feature types and scales numerical features.

        Returns:
            tuple: A tuple containing the scaled numerical DataFrame and the categorical DataFrame.
        """
        if not isinstance(X, pd.DataFrame):
            X = pd.DataFrame(X)
            
        self.numerical_features_ = X.select_dtypes(include=np.number).columns.tolist()
        self.categorical_features_ = X.select_dtypes(exclude=np.number).columns.tolist()

        X_num = X[self.numerical_features_].copy()
        X_cat = X[self.categorical_features_].copy()

        X_num_scaled = self.scaler.fit_transform(X_num)
        X_num_scaled = pd.DataFrame(X_num_scaled, columns=self.numerical_features_, index=X.index)
        
        print("\n--- Data Preprocessing ---")
        print(f"Identified {len(self.numerical_features_)} numerical features: {self.numerical_features_}")
        print(f"Identified {len(self.categorical_features_)} categorical features: {self.categorical_features_}")
        print("Standardized numerical features.")
        
        return X_num_scaled, X_cat
