# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_push_data_to_wandb.ipynb.

# %% auto 0
__all__ = ['pushDatasetToWandb']

# %% ../nbs/02_push_data_to_wandb.ipynb 3
import pandas as pd
import os
import wandb
from dotenv import load_dotenv

load_dotenv()

def pushDatasetToWandb(file_path: str, job_type: str, description: str = 'no description',
                  artifact_type: str = 'dataset'):
    """
    Uploads a dataset to Weights & Biases as an artifact.
    
    Args:
        file_path (str): Path to the file to upload.
        job_type (str): The type of job (e.g., "training", "evaluation").
        description (str, optional): Description of the artifact. Defaults to 'no description'.
        artifact_type (str, optional): Type of the artifact (e.g., "dataset"). Defaults to 'dataset'.
    
    Raises:
        ValueError: If file_path or job_type are not provided or if the file cannot be read.
    """
    
    # Check for required arguments
    if not file_path:
        raise ValueError("File path cannot be empty. Please provide a valid path to the data file.")
    
    if not job_type:
        raise ValueError("Job type cannot be empty. Please provide a valid job type.")

    # Initialize Weights & Biases
    try:
        wandb.login(key=os.environ["WANDB_API_KEY"])
        wandb.init(project="Risk Assessment", job_type=job_type)
    except wandb.errors.CommError as e:
        raise ConnectionError("Failed to connect to Weights & Biases. Check your API key and internet connection.") from e

    # Load the data and create artifact
    try:
        data = pd.read_csv(file_path)
    except FileNotFoundError:
        raise FileNotFoundError(f"File not found: {file_path}. Please check the file path.")
    except pd.errors.EmptyDataError:
        raise ValueError(f"File at {file_path} is empty or invalid.")

    artifact = wandb.Artifact(
        name=f"{job_type}_{os.path.basename(file_path)}",
        type=artifact_type,
        description=description,
    )
    artifact.add_file(file_path)

    # Log artifact and metadata
    try:
        wandb.log_artifact(artifact)
        wandb.log({
            'dataset_size': len(data),
            'num_features': len(data.columns),
            'memory_usage_mb': data.memory_usage(deep=True).sum() / (1024 ** 2)
        })
    finally:
        wandb.finish()

