import os
from celery import Celery
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

REDIS_URL = os.getenv('REDIS_URL', 'redis://localhost:6379/0')

app = Celery('config', broker=os.getenv('CELERY_BROKER_URL', REDIS_URL))

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Auto-discover tasks in installed apps
app.autodiscover_tasks()

app.conf.beat_schedule = {
    'process-scheduled-notification-campaigns-every-minute': {
        'task': 'apps.notifications.tasks.process_scheduled_notification_campaigns_task',
        'schedule': crontab(minute='*/1'),
    },
    'monthly-super-coin-conversion-job': {
        'task': 'apps.business.wallet.tasks.run_monthly_super_coin_conversion_task',
        'schedule': crontab(day_of_month='28-31', hour='23', minute='45'),
    },
    'monthly-withdrawal-generation-job': {
        'task': 'apps.business.wallet.tasks.run_monthly_withdrawal_generation_task',
        'schedule': crontab(day_of_month='28-31', hour='23', minute='50'),
    },
    # ── Binary Tree workers ───────────────────────────────────────────────────
    'rebuild-binary-metrics-nightly': {
        'task': 'apps.business.binary_tree.tasks.rebuild_binary_tree_metrics_task',
        'schedule': crontab(hour='2', minute='0'),           # 02:00 every night
        'kwargs': {'only_stale': True},
    },
    'expire-forfeit-power-stream-bonuses-daily': {
        'task': 'apps.business.binary_tree.tasks.expire_and_forfeit_power_stream_task',
        'schedule': crontab(hour='3', minute='0'),           # 03:00 every night
    },
    'validate-binary-integrity-weekly': {
        'task': 'apps.business.binary_tree.tasks.validate_binary_integrity_task',
        'schedule': crontab(day_of_week='0', hour='4', minute='0'),  # Sundays 04:00
    },
    # ── Telemetry / analytics pipeline workers ───────────────────────────────
    'telemetry-aggregate-daily-kpis': {
        'task': 'apps.telemetry.tasks.aggregate_daily_kpis_task',
        'schedule': crontab(minute='15', hour='0'),
    },
    'telemetry-aggregate-weekly-kpis': {
        'task': 'apps.telemetry.tasks.aggregate_weekly_kpis_task',
        'schedule': crontab(day_of_week='1', minute='20', hour='0'),
    },
    'telemetry-aggregate-monthly-kpis': {
        'task': 'apps.telemetry.tasks.aggregate_monthly_kpis_task',
        'schedule': crontab(day_of_month='1', minute='25', hour='0'),
    },
    'telemetry-compute-cohorts': {
        'task': 'apps.telemetry.tasks.compute_cohort_snapshots_task',
        'schedule': crontab(minute='30', hour='0'),
    },
    'telemetry-compute-retention': {
        'task': 'apps.telemetry.tasks.compute_retention_snapshots_task',
        'schedule': crontab(minute='35', hour='0'),
    },
    'telemetry-compute-funnels': {
        'task': 'apps.telemetry.tasks.compute_funnel_snapshots_task',
        'schedule': crontab(minute='40', hour='0'),
    },
    'telemetry-detect-anomalies': {
        'task': 'apps.telemetry.tasks.detect_telemetry_anomalies_task',
        'schedule': crontab(minute='*/15'),
    },
    'telemetry-compact-retention': {
        'task': 'apps.telemetry.tasks.compact_telemetry_data_task',
        'schedule': crontab(minute='50', hour='1'),
    },
    'orders-part-payment-cooling-conversion': {
        'task': 'apps.business.orders.tasks.run_part_payment_cooling_conversion_task',
        'schedule': crontab(hour='1', minute='5'),
    },
}


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')
