"""
Example tasks demonstrating how to use django-cronjob-utils.

This file shows various task patterns and can be used as a reference.
Copy tasks from here to your own app's tasks.py file.
"""

from datetime import date
from django_cronjob_utils import CronTask, register_task, ExecutionPattern


# Example 1: Standard Task (Run Once Per Day)
@register_task('daily-report', 'A001')
class DailyReportTask(CronTask):
    """Generate daily report - runs once per day"""
    
    def execute(self, date: date) -> dict:
        # Your business logic here
        # For example: generate_report(date)
        
        return {
            'error': False,
            'message': f'Daily report generated for {date}'
        }


# Example 2: Task with Retry on Failure
@register_task('sync-external-api', 'A002',
               execution_pattern=ExecutionPattern.RERUN_ON_FAILURE,
               retry_on_failure=True,
               max_retries=3,
               retry_delay=300)  # 5 minutes between retries
class SyncExternalAPITask(CronTask):
    """Sync data from external API - retries on failure"""
    
    def execute(self, date: date) -> dict:
        # Your API sync logic here
        # For example: api_client.sync(date)
        
        return {
            'error': False,
            'message': f'API sync completed for {date}'
        }


# Example 3: Always Execute Task (No Duplicate Check)
@register_task('update-cache', 'A003',
               execution_pattern=ExecutionPattern.ALWAYS)
class UpdateCacheTask(CronTask):
    """Update cache - safe to run multiple times"""
    
    def execute(self, date: date) -> dict:
        # Your cache update logic here
        # For example: cache_service.refresh()
        
        return {
            'error': False,
            'message': 'Cache updated successfully'
        }


# Example 4: Rate-Limited Task (Prevent Concurrent Execution)
@register_task('process-large-batch', 'A004',
               execution_pattern=ExecutionPattern.RATE_LIMITED,
               timeout=7200)  # 2 hour timeout
class ProcessLargeBatchTask(CronTask):
    """Process large batch - prevents concurrent execution"""
    
    def execute(self, date: date) -> dict:
        # Your batch processing logic here
        # For example: batch_processor.process(date)
        
        return {
            'error': False,
            'message': f'Processed batch for {date}'
        }


# Example 5: Task with Custom Validation
@register_task('process-payment', 'A005')
class ProcessPaymentTask(CronTask):
    """Process payments - requires custom validation"""
    
    def validate(self):
        """Custom validation logic"""
        super().validate()  # Call parent validation
        
        # Check if it's a business day
        if self.execution_date.weekday() >= 5:  # Saturday or Sunday
            raise ValueError("Payments can only be processed on weekdays")
    
    def execute(self, date: date) -> dict:
        # Your payment processing logic here
        
        return {
            'error': False,
            'message': f'Payments processed for {date}'
        }


# Example 6: Task with Error Handling
@register_task('import-data', 'A006')
class ImportDataTask(CronTask):
    """Import data from file - custom error handling"""
    
    def execute(self, date: date) -> dict:
        try:
            # Your import logic here
            # For example: importer.import_from_file(date)
            
            return {
                'error': False,
                'message': f'Data imported for {date}'
            }
        except FileNotFoundError:
            return {
                'error': True,
                'message': f'Import file not found for {date}',
                'error_code': 'FILE_NOT_FOUND'
            }
        except Exception as e:
            return {
                'error': True,
                'message': f'Unexpected error: {str(e)}',
                'error_code': 'UNEXPECTED_ERROR'
            }
