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

class Command(BaseCommand):
    help = 'Setup default notification templates'

    def handle(self, *args, **options):
        templates = [
            {
                'name': 'Payment Received SMS',
                'template_type': 'sms_payment_received',
                'content': 'Dear {{customer_name}}, your payment of KES {{amount}} has been received successfully. Receipt: {{payment_id}}. Thank you! - OptiNet',
                'variables_help': 'customer_name, amount, payment_id, payment_method, date, customer_id'
            },
            {
                'name': 'Payment Pending SMS',
                'template_type': 'sms_payment_pending',
                'content': 'Dear {{customer_name}}, please complete payment of KES {{amount}} on your phone. Check your phone for M-Pesa prompt. - OptiNet',
                'variables_help': 'customer_name, amount, payment_id, customer_id'
            },
            {
                'name': 'Payment Received Email',
                'template_type': 'email_payment_received',
                'subject': 'Payment Confirmation - {{payment_id}}',
                'content': '''Dear {{customer_name}},

We have successfully received your payment of KES {{amount}} via {{payment_method}}.

Payment Details:
- Payment ID: {{payment_id}}
- Amount: KES {{amount}}
- Method: {{payment_method}}
- Date: {{date}}
- Customer ID: {{customer_id}}

Thank you for your payment!

Best regards,
OptiNet Team''',
                'variables_help': 'customer_name, amount, payment_id, payment_method, date, customer_id, invoice_number'
            },
            {
                'name': 'Payment Pending Email',
                'template_type': 'email_payment_pending',
                'subject': 'Payment Request - {{customer_id}}',
                'content': '''Dear {{customer_name}},

A payment request for KES {{amount}} has been sent to your phone.

Please check your mobile phone for the M-Pesa payment prompt and complete the transaction.

Payment Details:
- Amount: KES {{amount}}
- Customer ID: {{customer_id}}
- Date: {{date}}

If you need assistance, please contact our support team.

Best regards,
OptiNet Team''',
                'variables_help': 'customer_name, amount, customer_id, date'
            }
        ]

        for template_data in templates:
            template, created = NotificationTemplate.objects.get_or_create(
                template_type=template_data['template_type'],
                defaults=template_data
            )

            if created:
                self.stdout.write(
                    self.style.SUCCESS(f'Created template: {template.name}')
                )
            else:
                self.stdout.write(
                    self.style.WARNING(f'Template already exists: {template.name}')
                )

        self.stdout.write(
            self.style.SUCCESS('Default notification templates setup completed!')
        )