"""
Celery configuration for asynchronous task processing
"""
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', 'aibimagics.settings')

app = Celery('aibimagics')

# 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')

# Load task modules from all registered Django apps.
app.autodiscover_tasks()

# Scheduled tasks
app.conf.beat_schedule = {
    'send-abandoned-cart-emails': {
        'task': 'cart.tasks.send_abandoned_cart_emails',
        'schedule': crontab(hour=10, minute=0),  # Daily at 10 AM
    },
    'update-ai-recommendations': {
        'task': 'ai_features.tasks.update_product_recommendations',
        'schedule': crontab(hour='*/6'),  # Every 6 hours
    },
    'generate-sales-reports': {
        'task': 'analytics.tasks.generate_daily_sales_report',
        'schedule': crontab(hour=23, minute=0),  # Daily at 11 PM
    },
    'check-low-inventory': {
        'task': 'inventory.tasks.check_low_stock_alerts',
        'schedule': crontab(hour=9, minute=0),  # Daily at 9 AM
    },
}

@app.task(bind=True, ignore_result=True)
def debug_task(self):
    print(f'Request: {self.request!r}')
