import requests
import pandas as pd
import os
from dotenv import load_dotenv
import glob

load_dotenv()
API_KEY = os.getenv("MY_KEY")

url = "https://api.exchangerate.host/timeframe"

with open("currencies.txt", "r") as f:
    currencies_list = [line.strip() for line in f if line.strip()]

currencies_str = ",".join(currencies_list)

params = {
    "start_date": "2025-01-02",
    "end_date": "2026-01-01",
    "base": "EUR",
    "currencies": currencies_str,
    "access_key": API_KEY
}

# --- 1️⃣ Получаем данные из API ---
resp = requests.get(url, params=params)
data = resp.json()

if not data.get("success", False):
    raise Exception(f"API Error: {data.get('error')}")

rows = []
for date_str, daily_quotes in data["quotes"].items():
    for pair, rate in daily_quotes.items():
        rows.append({
            "Date": date_str,
            "Base": pair[:3],
            "Currency": pair[3:],
            "Rate": rate
        })

df = pd.DataFrame(rows)
df["Date"] = pd.to_datetime(df["Date"])
df = df.sort_values(["Date", "Currency"])

batch_folder = "historical_usa"
os.makedirs(batch_folder, exist_ok=True)

existing_batches = glob.glob(os.path.join(
    batch_folder, "historical_rates_usa_*.csv"))
if existing_batches:
    existing_numbers = [int(os.path.splitext(os.path.basename(f))[
                            0].split("_")[1]) for f in existing_batches]
    next_number = max(existing_numbers) + 1
else:
    next_number = 1

batch_file = os.path.join(
    batch_folder, f"historical_rates_usa_{next_number}.csv")
df.to_csv(batch_file, index=False)
print(f"Batch saved: {batch_file}")

batch_files = glob.glob(os.path.join(batch_folder, "*.csv"))
dfs = []

for file in batch_files:
    df_batch = pd.read_csv(file)
    df_batch["Date"] = pd.to_datetime(df_batch["Date"], errors='coerce')
    dfs.append(df_batch)

df_all = pd.concat(dfs, ignore_index=True)
df_all = df_all.drop_duplicates(subset=["Date", "Base", "Currency"])
df_all = df_all.sort_values(
    ["Date", "Currency", "Base"]).reset_index(drop=True)

merged_file = "us_rates.csv"
df_all.to_csv(merged_file, index=False)
print(f"All batches merged: {merged_file}")
print(df_all.head())
