"""
Celery configuration for PX360 project.
"""
import os

from celery import Celery
from celery.schedules import crontab

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')

app = Celery('px360')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django apps.
app.autodiscover_tasks()

# Celery Beat schedule for periodic tasks
app.conf.beat_schedule = {
    # Process unprocessed integration events every 1 minute
    'process-integration-events': {
        'task': 'apps.integrations.tasks.process_pending_events',
        'schedule': crontab(minute='*/1'),
    },
    # Check for overdue complaints every 15 minutes
    'check-overdue-complaints': {
        'task': 'apps.complaints.tasks.check_overdue_complaints',
        'schedule': crontab(minute='*/15'),
    },
    # Check for overdue actions every 15 minutes
    'check-overdue-actions': {
        'task': 'apps.px_action_center.tasks.check_overdue_actions',
        'schedule': crontab(minute='*/15'),
    },
    # Send SLA reminders every hour
    'send-sla-reminders': {
        'task': 'apps.complaints.tasks.send_sla_reminders',
        'schedule': crontab(minute=0),  # Every hour at minute 0
    },
    # Check for overdue explanation requests every 15 minutes
    'check-overdue-explanation-requests': {
        'task': 'apps.complaints.tasks.check_overdue_explanation_requests',
        'schedule': crontab(minute='*/15'),
    },
    # Send explanation reminders every hour
    'send-explanation-reminders': {
        'task': 'apps.complaints.tasks.send_explanation_reminders',
        'schedule': crontab(minute=0),  # Every hour at minute 0
    },
    # Calculate daily KPIs at 1 AM
    'calculate-daily-kpis': {
        'task': 'apps.analytics.tasks.calculate_daily_kpis',
        'schedule': crontab(hour=1, minute=0),
    },
    # Calculate physician monthly ratings on the 1st of each month at 2 AM
    'calculate-physician-ratings': {
        'task': 'apps.physicians.tasks.calculate_monthly_ratings',
        'schedule': crontab(hour=2, minute=0, day_of_month=1),
    },





    # Scraping schedules
    'scrape-youtube-hourly': {
        'task': 'social.tasks.scrape_youtube_comments',
        'schedule': 3600.0,  # Every hour (in seconds)
    },
    'scrape-facebook-every-6-hours': {
        'task': 'social.tasks.scrape_facebook_comments',
        'schedule': 6 * 3600.0,  # Every 6 hours
    },
    'scrape-instagram-daily': {
        'task': 'social.tasks.scrape_instagram_comments',
        'schedule': crontab(hour=8, minute=0),  # Daily at 8:00 AM
    },
    'scrape-twitter-every-2-hours': {
        'task': 'social.tasks.scrape_twitter_comments',
        'schedule': 2 * 3600.0,  # Every 2 hours
    },
    'scrape-linkedin-daily': {
        'task': 'social.tasks.scrape_linkedin_comments',
        'schedule': crontab(hour=9, minute=0),  # Daily at 9:00 AM
    },
    'scrape-google-reviews-daily': {
        'task': 'social.tasks.scrape_google_reviews',
        'schedule': crontab(hour=10, minute=0),  # Daily at 10:00 AM
    },

    
    
    # Commented out - individual platform tasks provide sufficient coverage
    # 'scrape-all-platforms-daily': {
    #     'task': 'social.tasks.scrape_all_platforms',
    #     'schedule': crontab(hour=2, minute=0),  # Daily at 2:00 AM
    # },
    
    # Analysis schedules
    'analyze-comments-fallback': {
        'task': 'social.tasks.analyze_pending_comments',
        'schedule': 30 * 60.0,  # Every 30 minutes
        'kwargs': {'limit': 100},
    },
}


@app.task(bind=True, ignore_result=True)
def debug_task(self):
    """Debug task to test Celery is working."""
    print(f'Request: {self.request!r}')
