from models import EmailTemplate, User, db

def create_default_templates():
    """Create default email templates if they don't exist."""
    # Get the first admin user to be the creator
    admin = User.query.filter_by(is_admin=True).first()
    if not admin:
        print("No admin user found. Please create an admin user first.")
        return

    # Define default templates for each notification type
    default_templates = [
        {
            'name': 'Account Created',
            'subject': 'Welcome to DocuPulse!',
            'body': '''
            <h2>Welcome to DocuPulse!</h2>
            <p>Dear {{ user.username }},</p>
            <p>Your account has been successfully created by {{ created_by }}. To get started, you need to set up your password.</p>
            <p>Click the link below to set up your password (this link will expire in 24 hours):</p>
            <p><a href="{{ setup_link }}">Set Up Password</a></p>
            <p>If you didn't receive this email or if the link has expired, please contact your administrator to request a new password setup link.</p>
            <p>If you have any questions, please don't hesitate to contact our support team.</p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Password Reset',
            'subject': 'Password Reset Request',
            'body': '''
            <h2>Password Reset Request</h2>
            <p>Dear {{ user.username }},</p>
            <p>We received a request to reset your password. Click the link below to set a new password:</p>
            <p><a href="{{ reset_link }}">Reset Password</a></p>
            <p>If you didn't request this, please ignore this email or contact support if you have concerns.</p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Account Deleted',
            'subject': 'Your DocuPulse Account Has Been Deleted',
            'body': '''
            <h2>Account Deletion Confirmation</h2>
            <p>Dear {{ user.username }},</p>
            <p>Your DocuPulse account has been successfully deleted as requested.</p>
            <p>We're sorry to see you go. If you change your mind, you can create a new account at any time.</p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Account Updated',
            'subject': 'Your DocuPulse Account Has Been Updated',
            'body': '''
            <h2>Account Update Confirmation</h2>
            <p>Dear {{ user.username }},</p>
            <p>Your DocuPulse account has been successfully updated.</p>
            <p>If you didn't make these changes, please contact our support team immediately.</p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Room Invite',
            'subject': 'You\'ve Been Invited to a Room',
            'body': '''
            <h2>Room Invitation</h2>
            <p>Dear {{ user.username }},</p>
            <p>{{ sender.username }} has invited you to join the room "{{ room_name }}".</p>
            <p>Click the link below to view the room:</p>
            <p><a href="{{ room_link }}">View Room</a></p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Room Invite Removed',
            'subject': 'Room Invitation Removed',
            'body': '''
            <h2>Room Invitation Removed</h2>
            <p>Dear {{ user.username }},</p>
            <p>Your invitation to the room "{{ room_name }}" has been removed.</p>
            <p>If you believe this is an error, please contact the room administrator.</p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Conversation Invite',
            'subject': 'You\'ve Been Invited to a Conversation',
            'body': '''
            <h2>Conversation Invitation</h2>
            <p>Dear {{ user.username }},</p>
            <p>{{ sender.username }} has invited you to join a conversation.</p>
            <p>Click the link below to view the conversation:</p>
            <p><a href="{{ conversation_link }}">View Conversation</a></p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Conversation Invite Removed',
            'subject': 'Conversation Invitation Removed',
            'body': '''
            <h2>Conversation Invitation Removed</h2>
            <p>Dear {{ user.username }},</p>
            <p>Your invitation to the conversation has been removed.</p>
            <p>If you believe this is an error, please contact the conversation administrator.</p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        },
        {
            'name': 'Conversation Message',
            'subject': 'New Message in Conversation',
            'body': '''
            <h2>New Message</h2>
            <p>Dear {{ user.username }},</p>
            <p>{{ sender.username }} has sent a new message in your conversation.</p>
            <p>Click the link below to view the message:</p>
            <p><a href="{{ message_link }}">View Message</a></p>
            <p>Best regards,<br>The DocuPulse Team</p>
            '''
        }
    ]

    # Create templates if they don't exist
    for template_data in default_templates:
        existing_template = EmailTemplate.query.filter_by(name=template_data['name']).first()
        if not existing_template:
            template = EmailTemplate(
                name=template_data['name'],
                subject=template_data['subject'],
                body=template_data['body'],
                created_by=admin.id,
                is_active=True
            )
            db.session.add(template)
            print(f"Created template: {template_data['name']}")
        else:
            print(f"Template already exists: {template_data['name']}")

    try:
        db.session.commit()
        print("All default templates have been created successfully!")
    except Exception as e:
        db.session.rollback()
        print(f"Error creating templates: {str(e)}") 