"""
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")

# Apply zoneinfo compatibility patch for django-celery-beat with Python 3.12+
# This must be done before Celery app is created
from config.celery_scheduler import apply_tzcrontab_patch

apply_tzcrontab_patch()

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 = {
    # Fetch patient data from HIS daily at 7:00 AM
    "fetch-his-surveys": {
        "task": "apps.integrations.tasks.fetch_his_surveys",
        "schedule": crontab(hour=7, minute=0),
        "options": {
            "expires": 3600,
        },
    },
    # TEST TASK - Fetch from JSON file (uncomment for testing, remove when done)
    # 'test-fetch-his-surveys-from-json': {
    #     'task': 'apps.integrations.tasks.test_fetch_his_surveys_from_json',
    #     'schedule': crontab(minute='*/5'),  # Every 5 minutes
    # },
    # Send pending scheduled surveys every 10 minutes
    # "send-pending-scheduled-surveys": {
    #     "task": "apps.surveys.tasks.send_pending_scheduled_surveys",
    #     "schedule": crontab(minute="*/10"),  # Every 10 minutes
    # },
    # 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
    },
    # Check for overdue inquiries every 15 minutes
    "check-overdue-inquiries": {
        "task": "apps.complaints.tasks.check_overdue_inquiries",
        "schedule": crontab(minute="*/15"),
    },
    # Send inquiry SLA reminders every hour
    "send-inquiry-sla-reminders": {
        "task": "apps.complaints.tasks.send_inquiry_sla_reminders",
        "schedule": crontab(minute=0),  # Every hour at minute 0
    },
    # Check for overdue observations every 15 minutes
    "check-overdue-observations": {
        "task": "apps.observations.tasks.check_overdue_observations",
        "schedule": crontab(minute="*/15"),
    },
    # Send observation SLA reminders every hour
    "send-observation-sla-reminders": {
        "task": "apps.observations.tasks.send_observation_sla_reminders",
        "schedule": crontab(minute=0),  # Every hour at minute 0
    },
    # Check overdue inquiry dept responses every 15 minutes
    "check-overdue-inquiry-dept-responses": {
        "task": "apps.complaints.tasks.check_overdue_inquiry_dept_responses",
        "schedule": crontab(minute="*/15"),
    },
    # Send inquiry dept response reminders every hour
    "send-inquiry-dept-response-reminders": {
        "task": "apps.complaints.tasks.send_inquiry_dept_response_reminders",
        "schedule": crontab(minute=0),
    },
    # Check overdue observation dept responses every 15 minutes
    "check-overdue-observation-dept-responses": {
        "task": "apps.observations.tasks.check_overdue_observation_dept_responses",
        "schedule": crontab(minute="*/15"),
    },
    # Send observation dept response reminders every hour
    "send-observation-dept-response-reminders": {
        "task": "apps.observations.tasks.send_observation_dept_response_reminders",
        "schedule": crontab(minute=0),
    },
    # Send onboarding reminders every hour
    "send-onboarding-reminders": {
        "task": "apps.accounts.tasks.send_onboarding_reminders",
        "schedule": crontab(minute=30),  # Every hour at minute 30
    },
    # Clean up expired invitations daily at midnight
    "cleanup-expired-invitations": {
        "task": "apps.accounts.tasks.cleanup_expired_invitations",
        "schedule": crontab(hour=0, minute=0),  # Daily at midnight
    },
    # Calculate daily KPIs at 1 AM
    "calculate-daily-kpis": {
        "task": "apps.analytics.tasks.calculate_daily_kpis",
        "schedule": crontab(hour=1, minute=0),
    },
    # Fetch doctor ratings from HIS on the 1st of each month at 1 AM (before aggregation)
    "fetch-his-doctor-ratings-monthly": {
        "task": "apps.physicians.tasks.fetch_his_doctor_ratings_monthly",
        "schedule": crontab(hour=1, minute=0, day_of_month=1),
    },
    # Fetch doctor ratings from HIS daily at 1:30 AM (yesterday's ratings)
    "fetch-his-doctor-ratings-daily": {
        "task": "apps.physicians.tasks.fetch_his_doctor_ratings_daily",
        "schedule": crontab(hour=1, minute=30),
    },
    # Auto-aggregate physician individual ratings into monthly ratings daily at 2 AM
    "auto-aggregate-doctor-ratings-daily": {
        "task": "apps.physicians.tasks.auto_aggregate_daily",
        "schedule": crontab(hour=2, minute=0),
    },
    # 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},
    },
    # Process scheduled reports every 15 minutes
    "process-scheduled-reports": {
        "task": "apps.reports.tasks.process_scheduled_reports",
        "schedule": crontab(minute="*/15"),
    },
    # Schedule observation monthly follow-ups daily at 6 AM
    "schedule-observation-monthly-followups": {
        "task": "apps.observations.tasks.schedule_monthly_followups",
        "schedule": crontab(hour=6, minute=0),
    },
    # Process observation monthly follow-ups daily at 7 AM
    "process-observation-monthly-followups": {
        "task": "apps.observations.tasks.process_monthly_followups",
        "schedule": crontab(hour=7, minute=0),
    },
    # Analyze feedback sentiment every 30 minutes
    "analyze-feedback-sentiment": {
        "task": "apps.feedback.tasks.process_pending_sentiment_analysis",
        "schedule": crontab(minute="*/30"),
    },
    # Analyze survey text responses every 30 minutes
    "analyze-survey-text-responses": {
        "task": "apps.surveys.tasks.process_survey_text_analysis",
        "schedule": crontab(minute="*/30"),
    },
    # Pre-compute analytics dashboard cache daily at 3 AM
    "precompute-analytics-cache": {
        "task": "apps.analytics.tasks.precompute_dashboard_cache_task",
        "schedule": crontab(hour=3, minute=0),
    },
    # Pre-compute ALL AI analytics every hour (replaces slow synchronous page loads)
    "precompute-all-ai-analytics-hourly": {
        "task": "apps.analytics.tasks.precompute_all_ai_analytics_task",
        "schedule": crontab(minute=0),  # Every hour
    },
    # Generate AI executive summary daily at 6 AM (kept for backward compat)
    "generate-daily-executive-summary": {
        "task": "apps.analytics.tasks.generate_executive_summary_task",
        "schedule": crontab(hour=6, minute=0),
    },
    # Generate AI action recommendations daily at 6:30 AM (kept for backward compat)
    "generate-daily-action-recommendations": {
        "task": "apps.analytics.tasks.generate_action_recommendations_task",
        "schedule": crontab(hour=6, minute=30),
    },
    # Send weekly PX digest email every Monday at 8 AM
    "send-weekly-px-digest": {
        "task": "apps.analytics.tasks_digest.send_weekly_digest_task",
        "schedule": crontab(hour=8, minute=0, day_of_week=1),  # Monday
    },
    # Send monthly PX digest email on 1st of each month at 8 AM
    "send-monthly-px-digest": {
        "task": "apps.analytics.tasks_digest.send_monthly_digest_task",
        "schedule": crontab(hour=8, minute=0, day_of_month=1),
    },
    # Executive Summary Tasks
    # Calculate daily metrics at 1 AM
    "calculate-daily-executive-metrics": {
        "task": "apps.executive_summary.tasks.calculate_daily_metrics",
        "schedule": crontab(hour=1, minute=0),
    },
    # Generate predictive insights every 6 hours
    "generate-predictive-insights": {
        "task": "apps.executive_summary.tasks.generate_predictive_insights",
        "schedule": crontab(hour="*/6"),  # Every 6 hours
    },
    # Generate AI recommendations daily at 3 AM
    "generate-ai-recommendations": {
        "task": "apps.executive_summary.tasks.generate_ai_recommendations",
        "schedule": crontab(hour=3, minute=0),
    },
    # Generate weekly executive summary every Monday at 6 AM
    "generate-weekly-executive-summary": {
        "task": "apps.executive_summary.tasks.generate_weekly_executive_summary",
        "schedule": crontab(hour=6, minute=0, day_of_week=1),  # Monday
    },
    # Generate monthly executive summary on 1st at 7 AM
    "generate-monthly-executive-summary": {
        "task": "apps.executive_summary.tasks.generate_monthly_executive_summary",
        "schedule": crontab(hour=7, minute=0, day_of_month=1),
    },
    # Send executive PDF reports every Monday at 9 AM
    "send-executive-pdf-report": {
        "task": "apps.executive_summary.tasks.send_executive_pdf_report",
        "schedule": crontab(hour=9, minute=0, day_of_week=1),  # Monday
    },
}


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