import os
import requests
from datetime import datetime
from zipfile import ZipFile, ZIP_DEFLATED
from dotenv import load_dotenv

# Загрузка переменных из .env
load_dotenv()

TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
CHAT_ID = os.getenv('CHAT_ID')
DB_PATH = os.path.join(os.getenv('DB_PATH'), 'database.sqlite')

def send_document(filename, caption):
    url = f'https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendDocument'
    with open(filename, 'rb') as f:
        response = requests.post(
            url,
            data={'chat_id': CHAT_ID, 'caption': caption},
            files={'document': f}
        )
    print(response.json())

def main():
    if not TELEGRAM_TOKEN or not CHAT_ID or not DB_PATH:
        print("Помилка: TELEGRAM_TOKEN, CHAT_ID або DB_PATH не задано в .env.")
        return

    if os.path.exists(DB_PATH):
        timestamp = datetime.now().strftime("%d.%m.%Y  %H:%M")
        caption = f"Backup Sheep Service DB - {timestamp}"
        send_document(DB_PATH, caption)
    else:
        print("ZIP file not created")

if __name__ == "__main__":
    main()