"""
Celery configuration for branch_system project
"""
import os
from celery import Celery
from celery.schedules import crontab

# Set the default Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')

app = Celery('branch_system')

# Load configuration from Django settings with CELERY namespace
app.config_from_object('django.conf:settings', namespace='CELERY')

# Auto-discover tasks from all installed apps
app.autodiscover_tasks()

# Celery Beat schedule for periodic tasks
app.conf.beat_schedule = {
    'apply-automatic-penalties-daily': {
        'task': 'loans.tasks.apply_automatic_penalties',
        'schedule': crontab(hour=1, minute=0),  # Run daily at 1:00 AM
    },
    'send-penalty-notifications': {
        'task': 'loans.tasks.send_penalty_notifications',
        'schedule': crontab(hour=9, minute=0),  # Run daily at 9:00 AM
    },
}

# Celery configuration
app.conf.update(
    task_serializer='json',
    accept_content=['json'],
    result_serializer='json',
    timezone='Africa/Nairobi',
    enable_utc=True,
    task_track_started=True,
    task_time_limit=30 * 60,  # 30 minutes
    task_soft_time_limit=25 * 60,  # 25 minutes
)


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')
