from datetime import datetime, date

import requests


# 虚拟货币每日价格
def crypto_currency_daily_price(symbol: str, alpha_vantage_api_key: str) -> list[tuple[date, float]]:
    date_fmt = '%Y-%m-%d'

    url = (f'https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&'
           f'symbol={symbol}&market=USD&apikey={alpha_vantage_api_key}')

    resp = requests.get(url)
    resp.raise_for_status()

    data = resp.json()
    time_series = data['Time Series (Digital Currency Daily)']

    result = []
    for asof_date_str, data_points in time_series.items():
        asof_date = datetime.strptime(asof_date_str, date_fmt).date()
        close_price = float(data_points['4. close'])

        result.append((asof_date, close_price))

    return result
