#!/usr/bin/env python3
import os

# Create monitoring directory structure
repo_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
monitoring_dir = os.path.join(repo_root, 'monitoring')
grafana_dir = os.path.join(monitoring_dir, 'grafana')

datasources_dir = os.path.join(grafana_dir, 'provisioning', 'datasources')
providers_dir = os.path.join(grafana_dir, 'provisioning', 'dashboards')

os.makedirs(datasources_dir, exist_ok=True)
os.makedirs(providers_dir, exist_ok=True)

# Essential datasource configurations
datasources = {
    'prometheus.yml': """apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    url: http://prometheus:9090
    isDefault: true
    editable: true
    jsonData:
      timeInterval: "5s"
""",
    'loki.yml': """apiVersion: 1
datasources:
  - name: Loki
    type: loki
    access: proxy
    url: http://loki:3100
    editable: true
    jsonData:
      maxLines: 1000
""",
    'dashboards.yml': """apiVersion: 1
providers:
  - name: 'default'
    orgId: 1
    folder: ''
    type: file
    disableDeletion: false
    updateIntervalSeconds: 10
    allowUiUpdates: true
    options:
      path: /var/lib/grafana/dashboards
"""
}

# Write configuration files
for filename, content in datasources.items():
    if filename == 'dashboards.yml':
        path = os.path.join(providers_dir, filename)
    else:
        path = os.path.join(datasources_dir, filename)
    
    with open(path, 'w') as f:
        f.write(content)

print("Monitoring setup completed!")