"""Export utilities for simulation data."""

import csv
from datetime import datetime
from pathlib import Path
from typing import Any

import numpy as np


def export_nozzle_profile_csv(
    profile: dict[str, np.ndarray],
    filepath: Path,
    metadata: dict[str, Any] | None = None
) -> bool:
    """
    Export nozzle profile data to CSV.

    Args:
        profile: Dict with 'area_ratio', 'mach', 'temperature', 'pressure', 'velocity'
        filepath: Output file path
        metadata: Optional header metadata (engine name, date, etc.)

    Returns:
        True if export successful
    """
    try:
        with open(filepath, 'w', newline='', encoding='utf-8') as f:
            writer = csv.writer(f)

            # Write metadata header
            if metadata:
                writer.writerow(["# EnSim Nozzle Profile Export"])
                writer.writerow([f"# Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"])
                for key, value in metadata.items():
                    writer.writerow([f"# {key}: {value}"])
                writer.writerow([])

            # Write column headers
            writer.writerow([
                "Area_Ratio_A/At",
                "Mach_Number",
                "Temperature_K",
                "Pressure_Pa",
                "Velocity_m/s",
                "P/Pc",
                "T/Tc"
            ])

            # Write data rows
            n = len(profile['area_ratio'])
            for i in range(n):
                writer.writerow([
                    f"{profile['area_ratio'][i]:.4f}",
                    f"{profile['mach'][i]:.4f}",
                    f"{profile['temperature'][i]:.2f}",
                    f"{profile['pressure'][i]:.2f}",
                    f"{profile['velocity'][i]:.2f}",
                    f"{profile['P_ratio'][i]:.6f}",
                    f"{profile['T_ratio'][i]:.6f}",
                ])

        return True
    except Exception as e:
        print(f"Error exporting CSV: {e}")
        return False


def generate_report_markdown(
    fuel: str,
    oxidizer: str,
    of_ratio: float,
    chamber_pressure_bar: float,
    expansion_ratio: float,
    temperature: float,
    isp_vacuum: float,
    isp_sea_level: float,
    thrust_kN: float,
    c_star: float,
    gamma: float,
    mean_mw: float,
    species_fractions: dict[str, float],
) -> str:
    """
    Generate a formatted simulation report.

    Returns:
        Markdown-formatted report string
    """
    date_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    report = f"""# EnSim Simulation Report
Generated: {date_str}

---

## Engine Configuration

| Parameter | Value |
|-----------|-------|
| Fuel | {fuel} |
| Oxidizer | {oxidizer} |
| O/F Ratio | {of_ratio:.2f} |
| Chamber Pressure | {chamber_pressure_bar:.1f} bar |
| Expansion Ratio (ε) | {expansion_ratio:.1f} |

---

## Performance Results

| Metric | Value |
|--------|-------|
| **Chamber Temperature** | {temperature:.0f} K |
| **Vacuum Isp** | {isp_vacuum:.1f} s |
| **Sea Level Isp** | {isp_sea_level:.1f} s |
| **Thrust (Vacuum)** | {thrust_kN:.2f} kN |
| **C*** | {c_star:.1f} m/s |
| **Gamma (γ)** | {gamma:.4f} |
| **Mean MW** | {mean_mw:.2f} g/mol |

---

## Combustion Products

| Species | Mole Fraction |
|---------|---------------|
"""

    for species, frac in sorted(species_fractions.items(),
                                 key=lambda x: x[1], reverse=True):
        report += f"| {species} | {frac*100:.2f}% |\n"

    report += """
---

## Notes

- Calculations based on Gordon & McBride equilibrium method
- Frozen flow assumed through nozzle expansion
- Dissociation species included (H, O, OH)

---

*Report generated by EnSim v1.0.0*
"""

    return report


def export_report(
    filepath: Path,
    **kwargs
) -> bool:
    """
    Export simulation report to file.

    Args:
        filepath: Output file path (.md or .txt)
        **kwargs: Report parameters (fuel, oxidizer, etc.)

    Returns:
        True if export successful
    """
    try:
        report = generate_report_markdown(**kwargs)

        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(report)

        return True
    except Exception as e:
        print(f"Error exporting report: {e}")
        return False
