# download.py
import numpy as np
from .E_values import F_ph_array
import os

# Check that F_ph_array is defined
if F_ph_array is None:
    raise ValueError("F_ph_array is not defined. Run modulo1 first")

print("\n" + "="*50)
print("SAVING DATA TO FILES")
print("="*50)

num_archivos = len(F_ph_array)
print(f"{num_archivos} file(s) will be created")

# Simulate data arrays for each F_ph
# Replace these with your real arrays: longitud_array, Sigma1_array, Sigma2_array, Sigma3_array, Fuente_Luz_array
longitud_array = np.linspace(400, 700, 1001)          # example
Sigma1_array = np.random.rand(1001) * 1e-7
Sigma2_array = np.random.rand(1001) * 1e-7
Sigma3_array = np.random.rand(1001) * 1e-7
Fuente_Luz_array = np.exp(-((longitud_array - 500) ** 2) / (2 * 5**2))

pf1_v, pf2_v, pf3_v = [], [], []

# Save each file
for fph_valor in F_ph_array:
    file_name = f"photo_stimulation_Fph_{fph_valor:.1f}nm.txt"

    # Combine arrays into a single matrix
    data_fph = np.column_stack((
        longitud_array,
        Sigma1_array,
        Sigma2_array,
        Sigma3_array,
        Fuente_Luz_array
    ))

    np.savetxt(file_name, data_fph,
               delimiter='\t',
               fmt=['%.2f', '%.6e', '%.6e', '%.6e', '%.6f'],
               header='LONG\tSIGMA1\tSIGMA2\tSIGMA3\tSOURCE',
               comments='')

    print(f"✅ File created: {file_name}")

    # Save example sums
    pf1_v.append(Sigma1_array.sum())
    pf2_v.append(Sigma2_array.sum())
    pf3_v.append(Sigma3_array.sum())

# Create summary file if there is more than one F_ph
if len(F_ph_array) > 1:
    summary_file = "summary_parameters.txt"
    summary_data = np.column_stack((F_ph_array, pf1_v, pf2_v, pf3_v))
    np.savetxt(summary_file, summary_data,
               delimiter='\t',
               fmt=['%.2f', '%.6e', '%.6e', '%.6e'],
               header='F_ph\tpf1\tpf2\tpf3',
               comments='')

    print("\nParameter summary:")
    print("F_ph\tpf1\tpf2\tpf3")
    for i in range(len(F_ph_array)):
        print(f"{F_ph_array[i]:.2f}\t{pf1_v[i]:.6e}\t{pf2_v[i]:.6e}\t{pf3_v[i]:.6e}")

# ======================================
# Download files in Colab
# ======================================
try:
    from google.colab import files
    print("\nDOWNLOADING FILES IN COLAB...")
    for fph_valor in F_ph_array:
        file_name = f"photo_stimulation_Fph_{fph_valor:.1f}nm.txt"
        files.download(file_name)
        print(f"📥 Downloaded: {file_name}")

    if len(F_ph_array) > 1:
        files.download(summary_file)
        print(f"📥 Downloaded: {summary_file}")

except ImportError:
    print("\n⚠️ Google Colab not found - files saved locally")
    print("Files created:")
    for fph_valor in F_ph_array:
        print(f" • photo_stimulation_Fph_{fph_valor:.1f}nm.txt")
    if len(F_ph_array) > 1:
        print(f" • {summary_file}")

print("\nPROCESS COMPLETED")
print("="*50)
