import os
from datetime import timedelta
from urllib.parse import quote_plus

#Настройки телеграм
TOKEN = os.getenv('TELEGRAM_TOKEN')
ADMINS = os.getenv('TELEGRAM_ADMINS', '')
ADMINS_LIST = [int(admin_id.strip()) for admin_id in ADMINS.split(',') if admin_id.strip().isdigit()]
REGIONS_PER_PAGE = os.getenv('REGIONS_PER_PAGE', 10)
#Настройки Zabbix
ZABBIX_API_TOKEN = os.getenv('ZABBIX_API_TOKEN')
ZABBIX_URL = os.getenv('ZABBIX_URL')
ZABBIX_VERIFY_SSL = os.getenv('ZABBIX_VERIFY_SSL', True)
ZABBIX_TZ = os.getenv('ZABBIX_TZ', 'Europe/Moscow')
#Настройки Flask и Telegram bot
basedir = os.path.abspath(os.path.dirname(__file__))
DB_ABS_PATH = os.path.join(basedir, 'db/telezab.db')
SQLALCHEMY_DATABASE_URI = f'sqlite:///{DB_ABS_PATH}'
SUPPORT_EMAIL = os.getenv("SUPPORT_EMAIL", "shiftsupport-rtmis@rtmis.ru")
HELP_URL = os.getenv("HELP_URL", "https://confluence.is-mis.ru/pages/viewpage.action?pageId=416785183")
#Настройки RabbitMQ
RABBITMQ_HOST = os.getenv('RABBITMQ_HOST', 'localhost')
RABBITMQ_PORT = int(os.environ.get("RABBITMQ_PORT", "5672"))
RABBITMQ_LOGIN = os.getenv('RABBITMQ_LOGIN', 'admin')
RABBITMQ_PASS = os.getenv('RABBITMQ_PASS', 'admin')
RABBITMQ_QUEUE = os.environ.get("RABBITMQ_QUEUE", "telegram_notifications")
RABBITMQ_NOTIFICATIONS_QUEUE = os.environ.get("RABBITMQ_NOTIFICATIONS_QUEUE", "notifications_queue")
RABBITMQ_VHOST = os.getenv("RABBITMQ_VHOST", "/")
RABBITMQ_VHOST_ENCODED = quote_plus(RABBITMQ_VHOST)

RABBITMQ_URL_FULL = (
    f"amqp://{RABBITMQ_LOGIN}:{RABBITMQ_PASS}@{RABBITMQ_HOST}:{RABBITMQ_PORT}/{RABBITMQ_VHOST_ENCODED}"
)
# Mailing settings
MAILING_MAX_WORKERS = int(os.environ.get("MAILING_MAX_WORKERS", "16"))
MAILING_RATE_LIMIT = int(os.environ.get("MAILING_RATE_LIMIT", "25"))

# Настройки Flask-LDAP3-login
LDAP_HOST = os.getenv('LDAP_HOST', 'localhost')
LDAP_PORT = int(os.getenv('LDAP_PORT', 389))
LDAP_USE_SSL = os.getenv('LDAP_USE_SSL', 'False').lower() == 'true'
LDAP_BASE_DN = os.getenv('LDAP_BASE_DN', 'DC=tech,DC=local')
LDAP_BIND_USER_DN = os.getenv('LDAP_BIND_USER_DN', 'CN=sa_tgbot,OU=Service Accounts,DC=tech,DC=local')
LDAP_USER_DN = os.getenv('LDAP_USER_DN', 'RMIS')
LDAP_USER_PASSWORD = os.getenv('LDAP_USER_PASSWORD', '***')
LDAP_USER_OBJECT_FILTER = os.getenv('LDAP_USER_OBJECT_FILTER', '(objectClass=person)')
LDAP_USER_RDN_ATTR = os.getenv('LDAP_USER_RDN_ATTR', 'sAMAccountName')
LDAP_USER_LOGIN_ATTR = os.getenv('LDAP_USER_LOGIN_ATTR', 'sAMAccountName')
LDAP_USER_SEARCH_SCOPE = os.getenv('LDAP_USER_SEARCH_SCOPE', 'SUBTREE')
LDAP_SCHEMA = os.getenv('LDAP_SCHEMA', 'active_directory')



class Config:
    basedir = os.path.abspath(os.path.dirname(__file__))

    # SQLAlchemy
    SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(basedir, 'db/telezab.db')}"
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # Flask session
    SECRET_KEY = os.environ.get('SECRET_KEY', 'your-secret-key')
    SESSION_COOKIE_SECURE = os.getenv('SESSION_COOKIE_SECURE', 'True').lower() == 'true'
    SESSION_COOKIE_HTTPONLY = os.getenv('SESSION_COOKIE_HTTPONLY', 'True').lower() == 'true'
    SESSION_COOKIE_SAMESITE = os.getenv('SESSION_COOKIE_SAMESITE', 'Lax')
    PERMANENT_SESSION_LIFETIME = timedelta(
        seconds=int(os.getenv('PERMANENT_SESSION_LIFETIME_SECONDS', 3600))
    )
    SESSION_COOKIE_MAX_AGE = int(os.getenv('SESSION_COOKIE_MAX_AGE', 3600))

    # Дополнительное (если используется)
    TIMEZONE = os.getenv('TZ', 'Europe/Moscow')
