import os
from dotenv import load_dotenv
from t_tech.invest import Client

os.environ['SSL_CERT_FILE'] = '/etc/ssl/certs/ca-certificates.crt'
os.environ['GRPC_DEFAULT_SSL_ROOTS_FILE_PATH'] = '/etc/ssl/certs/ca-certificates.crt'

load_dotenv("tok.env")
TOKEN = os.getenv("TINKOFF_TOKEN")

with Client(TOKEN, target="invest-public-api.tbank.ru:443") as client:
    # 1. Получаем инфо о счете
    accounts = client.users.get_accounts().accounts
    for acc in accounts:
        print(f"\n--- ИНФОРМАЦИЯ О СЧЕТЕ ---")
        print(f"Имя: {acc.name} | ID: {acc.id}")
        print(f"Тип счета (API): {acc.type.name}") # Ищем ACCOUNT_TYPE_T_INVESTMENTS
        
        # 2. Проверяем баланс (реальный или фантики)
        p = client.operations.get_portfolio(account_id=acc.id)
        print(f"Баланс: {p.total_amount_currencies.units} {p.total_amount_currencies.currency}")

    # 3. Проверяем цену Сбера по актуальному UID
    SBER_UID = "e6123145-9665-43e0-8413-cd61b8aa9b13"
    lp = client.market_data.get_last_prices(instrument_id=[SBER_UID]).last_prices[0]
    print(f"\n--- ПРОВЕРКА РЫНКА ---")
    print(f"Цена SBER в API: {lp.price.units + lp.price.nano / 1e9}")
    print(f"Дата цены (UTC): {lp.time.strftime('%Y-%m-%d %H:%M:%S')}")
