from schedule.models import EmailTemplate
import json

html_content = """
<body style="margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; background-color: #f5f5f5;">
    <table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f5f5f5; padding: 40px 20px;">
        <tbody><tr>
            <td align="center">
                <table width="600" cellpadding="0" cellspacing="0" style="background-color: #ffffff; border-radius: 6px; box-shadow: 0 4px 6px rgba(0,0,0,0.05), 0 1px 3px rgba(0,0,0,0.08);">
                    <!-- Header -->
                    <tbody><tr>
                        <td style="background-color: #4f46e5; padding: 30px; text-align: center; border-radius: 6px 6px 0 0;">
                            <h1 style="margin: 0; color: #ffffff; font-size: 28px; font-weight: 600;">Appointment Confirmed</h1>
                        </td>
                    </tr>

                    <!-- Body -->
                    <tr>
                        <td style="padding: 40px 30px;">
                            <p style="margin: 0 0 20px 0; color: #374151; font-size: 16px; line-height: 1.6;">
                                Hello <strong>{{CUSTOMER_NAME}}</strong>,
                            </p>

                            <p style="margin: 0 0 30px 0; color: #374151; font-size: 16px; line-height: 1.6;">
                                Your appointment has been confirmed. We look forward to seeing you!
                            </p>

                            <!-- Appointment Details Card -->
                            <table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f5f5f5; border-radius: 6px; border: 1px solid #e5e7eb; margin-bottom: 30px;">
                                <tbody><tr>
                                    <td style="padding: 20px;">
                                        <table width="100%" cellpadding="8" cellspacing="0">
                                            <tbody><tr>
                                                <td style="color: #6b7280; font-size: 14px; font-weight: 600;">Service:</td>
                                                <td style="color: #111827; font-size: 14px; text-align: right;">{{SERVICE_NAME}}</td>
                                            </tr>
                                            <tr>
                                                <td style="color: #6b7280; font-size: 14px; font-weight: 600;">Date &amp; Time:</td>
                                                <td style="color: #111827; font-size: 14px; text-align: right;">{{EVENT_START_DATETIME}}</td>
                                            </tr>
                                            <tr>
                                                <td style="color: #6b7280; font-size: 14px; font-weight: 600;">Duration:</td>
                                                <td style="color: #111827; font-size: 14px; text-align: right;">{{SERVICE_DURATION}} minutes</td>
                                            </tr>
                                            <tr>
                                                <td style="color: #6b7280; font-size: 14px; font-weight: 600;">With:</td>
                                                <td style="color: #111827; font-size: 14px; text-align: right;">{{STAFF_NAME}}</td>
                                            </tr>
                                        </tbody></table>
                                    </td>
                                </tr>
                            </tbody></table>
                            
                            <!-- Call to Action Button (example - not in original but good to show professional button style) -->
                            <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-bottom: 30px;">
                                <tr>
                                    <td align="center" style="padding-top: 10px;">
                                        <table border="0" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td align="center" bgcolor="#4f46e5" style="border-radius: 5px; background-color: #4f46e5; padding: 12px 25px;">
                                                    <a href="{{VIEW_APPOINTMENT_LINK}}" target="_blank" style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; color: #ffffff; text-decoration: none; font-weight: 600; display: inline-block;">View My Appointment</a>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>

                            <p style="margin: 0 0 20px 0; color: #6b7280; font-size: 14px; line-height: 1.6;">
                                If you need to reschedule or cancel, please contact us at least 24 hours in advance.
                            </p>
                        </td>
                    </tr>

                    <!-- Footer -->
                    <tr>
                        <td style="background-color: #f9fafb; padding: 20px 30px; text-align: center; border-radius: 0 0 6px 6px; border-top: 1px solid #e5e7eb;">
                            <p style="margin: 0; color: #6b7280; font-size: 14px;">
                                <strong>{{BUSINESS_NAME}}</strong><br>
                                {{BUSINESS_EMAIL}} | {{BUSINESS_PHONE}}
                            </p>
                        </td>
                    </tr>
                </tbody></table>
            </td>
        </tr>
    </tbody></table>

</body>
"""

text_content = """
Hello {{CUSTOMER_NAME}},

Your appointment has been confirmed. We look forward to seeing you!

---
Appointment Details:
Service: {{SERVICE_NAME}}
Date & Time: {{EVENT_START_DATETIME}}
Duration: {{SERVICE_DURATION}} minutes
With: {{STAFF_NAME}}
---

If you need to reschedule or cancel, please contact us at least 24 hours in advance.

View your appointment: {{VIEW_APPOINTMENT_LINK}}

---
{{BUSINESS_NAME}}
{{BUSINESS_EMAIL}} | {{BUSINESS_PHONE}}
"""

template_name = "Appointment Confirmed" # Assuming this is the name of the template to update

try:
    template = EmailTemplate.objects.get(name=template_name)
    template.html_content = html_content
    template.text_content = text_content
    template.save()
    print(f"Successfully updated template '{template_name}'.")
except EmailTemplate.DoesNotExist:
    print(f"Error: Template '{template_name}' not found.")
except Exception as e:
    print(f"An error occurred: {e}")
