"""load_data handler — load CSV files and store as dataset."""
import os
from pathlib import Path
from typing import Optional

import polars as pl

from ..data_loader import load_csv_directory
from ..session_store import SessionStore, _generate_id
from analysis.constants import MAX_DISTRIBUTION_SAMPLE


async def _handle_load_data(
    csv_glob: str,
    config_path: Optional[str] = None,
    encoding: str = 'utf-8',
    delimiter: str = ',',
    schema_strict: bool = False,
    recursive: bool = False,
) -> dict:
    """Load, validate, merge CSVs, and store in session."""
    try:
        lf, schema, row_count, file_count, memory_mb = load_csv_directory(
            glob_pattern=csv_glob,
            encoding=encoding,
            delimiter=delimiter,
            config_path=config_path,
            sample_rows=MAX_DISTRIBUTION_SAMPLE,
            schema_strict=schema_strict,
            recursive=recursive,
        )
    except FileNotFoundError as e:
        return {'error': f'File not found: {e}', 'truncated': False}
    except ValueError as e:
        return {'error': f'Schema validation failed: {e}', 'truncated': False}

    store = SessionStore()
    dataset_id = _generate_id('ds')
    store.store_dataset(
        dataset_id=dataset_id,
        lazyframe=lf,
        schema=schema,
        metadata={
            'row_count': row_count,
            'file_count': file_count,
            'memory_mb': round(memory_mb, 2),
            'csv_glob': csv_glob,
            'encoding': encoding,
        },
    )

    return {
        'dataset_id': dataset_id,
        'schema': schema,
        'row_count': row_count,
        'file_count': file_count,
        'memory_mb': round(memory_mb, 2),
    }
