import pandas as pd
from datetime import datetime, UTC
import os

CLEAN_DIR = "/opt/airflow/data/processed"
FEATURE_DIR = "/opt/airflow/data/features"

def create_features(asset: str):
    file_path = os.path.join(CLEAN_DIR, f"{asset}_clean.parquet")
    df = pd.read_parquet(file_path)

    # Пример простых признаков
    df["return"] = df["close"].pct_change()
    df["volatility"] = df["return"].rolling(5).std()

    # Убираем первые строки с NaN после rolling
    df.dropna(inplace=True)

    # Добавляем timestamp
    df["feature_timestamp"] = datetime.now(UTC)

    os.makedirs(FEATURE_DIR, exist_ok=True)
    feature_file = os.path.join(FEATURE_DIR, f"{asset}_features.parquet")
    df.to_parquet(feature_file, index=False)

    print(f"Features saved to {feature_file}")

if __name__ == "__main__":
    create_features()