
from django.core.management.base import BaseCommand
from notifications.models import NotificationTemplate

class Command(BaseCommand):
    help = 'Set up default ticket notification templates'

    def handle(self, *args, **options):
        templates = [
            {
                'name': 'Ticket Assigned to Technician',
                'template_type': 'sms_ticket_assigned',
                'content': 'New {{priority}} ticket {{ticket_id}} assigned to you: {{title}}. Customer: {{customer_name}}. Location: {{location}}. Scheduled: {{scheduled_date}}',
                'variables_help': 'ticket_id, title, customer_name, priority, scheduled_date, location'
            },
            {
                'name': 'Appointment Reminder',
                'template_type': 'sms_appointment_reminder',
                'content': 'Reminder: Your {{service_type}} appointment is scheduled for {{scheduled_time}} today. Technician: {{technician_name}}. Ticket: {{ticket_id}}',
                'variables_help': 'customer_name, ticket_id, scheduled_time, technician_name, service_type'
            },
            {
                'name': 'Appointment Confirmed',
                'template_type': 'sms_appointment_confirmed',
                'content': 'Hello {{customer_name}}, your {{service_type}} appointment is confirmed for {{scheduled_date}} at {{scheduled_time}}. {{technician_name}} will visit {{location}}.',
                'variables_help': 'customer_name, ticket_id, scheduled_date, scheduled_time, service_type, technician_name, location'
            },
            {
                'name': 'Technician Arriving',
                'template_type': 'sms_technician_arriving',
                'content': 'Hello {{customer_name}}, our technician {{technician_name}} is arriving shortly for ticket {{ticket_id}}.',
                'variables_help': 'customer_name, technician_name, ticket_id'
            },
            {
                'name': 'Service Completed',
                'template_type': 'sms_service_completed',
                'content': 'Service completed! Ticket {{ticket_id}} for {{service_type}} has been resolved at {{completion_time}}. Thank you for choosing our services.',
                'variables_help': 'customer_name, ticket_id, service_type, completion_time'
            },
            {
                'name': 'Ticket Overdue Alert',
                'template_type': 'sms_ticket_overdue',
                'content': 'OVERDUE: Ticket {{ticket_id}} for {{customer_name}} is overdue by {{overdue_time}} minutes.',
                'variables_help': 'ticket_id, customer_name, overdue_time'
            },
            {
                'name': 'Ticket Status Change',
                'template_type': 'sms_ticket_status_change',
                'content': 'Hello {{customer_name}}, your ticket {{ticket_id}} status has been updated to {{new_status}}.',
                'variables_help': 'customer_name, ticket_id, old_status, new_status, title, assigned_person'
            }
        ]

        created_count = 0
        updated_count = 0

        for template_data in templates:
            template, created = NotificationTemplate.objects.get_or_create(
                template_type=template_data['template_type'],
                defaults=template_data
            )
            
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'Created template: {template.name}')
                )
            else:
                # Update existing template if content has changed
                if template.content != template_data['content']:
                    template.content = template_data['content']
                    template.variables_help = template_data['variables_help']
                    template.save()
                    updated_count += 1
                    self.stdout.write(
                        self.style.WARNING(f'Updated template: {template.name}')
                    )

        self.stdout.write(
            self.style.SUCCESS(
                f'Successfully processed ticket notification templates. '
                f'Created: {created_count}, Updated: {updated_count}'
            )
        )
