"""
visualization.py

A reusable module for plotting spectral data from CSV or DataFrame.
"""

import pandas as pd
import matplotlib.pyplot as plt

def plot_spectra_from_csv(file_path):
    """
    Reads a CSV file (skipping the first row), transposes it,
    extracts wavelength information from the DataFrame index,
    and creates a line plot of all spectral columns.

    Args:
        file_path (str): Path to the CSV file.

    The CSV is assumed to have:
      - Row 0: Some header text or metadata (this is skipped).
      - Rows 1..n: Spectral data, one row per plot or measurement.
    """
    # 1. Read the CSV, skipping the first row
    df = pd.read_csv(file_path, skiprows=1)
    
    # 2. Transpose it so that columns become rows and rows become columns
    transposed_df = df.T
    
    # 3. Extract numeric wavelengths from the index (excluding the first entry)
    wavelength = pd.to_numeric(transposed_df.index[1:], errors="coerce")
    
    # 4. Plot each column on the same figure
    plt.figure(figsize=(15, 8))
    for col_name in transposed_df.columns[1:]:
        # Convert the data to numeric, ignoring non-numeric values
        y_values = pd.to_numeric(
            transposed_df.iloc[1:, transposed_df.columns.get_loc(col_name)],
            errors='coerce'
        )
        plt.plot(wavelength, y_values, label=str(col_name))
    
    # 5. Label and decorate the plot
    plt.xlabel('Wavelength')
    plt.ylabel('Reflectance (or other units)')
    plt.title('Line Plot of Different Plot IDs')
    plt.grid(True)
    # Uncomment the next line if you want a legend
    # plt.legend()
    plt.show()
