"""Create basic notification templates for RateRight notification system"""
import sys
import os

# Add the parent directory to sys.path to import app modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import create_app
from app.extensions import db
from app.models.notification import NotificationTemplate, NotificationType, DeliveryChannel

def create_basic_templates():
    """Create basic notification templates for common notification types"""
    app = create_app()
    
    with app.app_context():
        templates = [
            # Email templates
            {
                'notification_type': NotificationType.RATING_RECEIVED,
                'channel': DeliveryChannel.EMAIL,
                'template_name': 'Rating Received Email',
                'subject_template': 'New Rating Received - {rating}/5 Stars!',
                'body_template': '''G'day {user_name},

You've received a new rating from {reviewer_name} for your work on "{job_title}":

Rating: {stars} ({rating}/5)

{comment}

Keep up the great work! Building your reputation is key to success on RateRight.

View your full rating profile: {action_url}

Cheers,
The RateRight Team

---
RateRight - Australian Construction Marketplace
{unsubscribe_url}''',
                'html_template': '''
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
    <h2 style="color: #2c5aa0;">New Rating Received!</h2>
    <p>G'day {user_name},</p>
    
    <p>You've received a new rating from <strong>{reviewer_name}</strong> for your work on "<strong>{job_title}</strong>":</p>
    
    <div style="background: #f8f9fa; padding: 20px; border-radius: 5px; margin: 20px 0;">
        <h3 style="color: #28a745;">Rating: {stars} ({rating}/5)</h3>
        <p style="font-style: italic;">"{content}"</p>
    </div>
    
    <p>Keep up the great work! Building your reputation is key to success on RateRight.</p>
    
    <p><a href="{action_url}" style="background: #2c5aa0; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">View Rating Profile</a></p>
    
    <hr style="margin: 30px 0;">
    <p style="color: #666; font-size: 12px;">
        RateRight - Australian Construction Marketplace<br>
        <a href="{unsubscribe_url}">Unsubscribe</a>
    </p>
</div>''',
                'includes_unsubscribe': True,
                'description': 'Email template for rating received notifications'
            },
            
            {
                'notification_type': NotificationType.PAYMENT_RECEIVED,
                'channel': DeliveryChannel.EMAIL,
                'template_name': 'Payment Received Email',
                'subject_template': 'Payment Received - ${amount} for {job_title}',
                'body_template': '''G'day {user_name},

Great news! You've received a payment of ${amount} for "{job_title}".

Payment Details:
- Amount: ${amount}
- Job: {job_title}
- Date: {payment_date}

The funds should appear in your account within 1-2 business days.

View payment details: {action_url}

Cheers,
The RateRight Team

---
RateRight - Australian Construction Marketplace
{unsubscribe_url}''',
                'html_template': '''
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
    <h2 style="color: #28a745;">Payment Received! 💰</h2>
    <p>G'day {user_name},</p>
    
    <p>Great news! You've received a payment for your work.</p>
    
    <div style="background: #d4edda; padding: 20px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #28a745;">
        <h3 style="color: #155724; margin-top: 0;">Payment Details</h3>
        <ul style="list-style: none; padding: 0;">
            <li><strong>Amount:</strong> ${amount}</li>
            <li><strong>Job:</strong> {job_title}</li>
            <li><strong>Date:</strong> {payment_date}</li>
        </ul>
    </div>
    
    <p>The funds should appear in your account within 1-2 business days.</p>
    
    <p><a href="{action_url}" style="background: #28a745; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">View Payment Details</a></p>
    
    <hr style="margin: 30px 0;">
    <p style="color: #666; font-size: 12px;">
        RateRight - Australian Construction Marketplace<br>
        <a href="{unsubscribe_url}">Unsubscribe</a>
    </p>
</div>''',
                'includes_unsubscribe': True,
                'description': 'Email template for payment received notifications'
            },
            
            {
                'notification_type': NotificationType.JOB_MATCH,
                'channel': DeliveryChannel.EMAIL,
                'template_name': 'Job Match Email',
                'subject_template': 'New Job Match: {job_title}',
                'body_template': '''G'day {user_name},

We've found a job that matches your skills and location!

Job: {job_title}
Match Score: {match_score}%
Location: {job_location}
Posted: {job_posted_date}

This job looks like a great fit for your experience. Don't wait too long - good jobs go fast!

View job details: {action_url}

Cheers,
The RateRight Team

---
RateRight - Australian Construction Marketplace
{unsubscribe_url}''',
                'html_template': '''
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
    <h2 style="color: #2c5aa0;">New Job Match Found! 🔨</h2>
    <p>G'day {user_name},</p>
    
    <p>We've found a job that matches your skills and location!</p>
    
    <div style="background: #e3f2fd; padding: 20px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #2c5aa0;">
        <h3 style="color: #1565c0; margin-top: 0;">{job_title}</h3>
        <ul style="list-style: none; padding: 0;">
            <li><strong>Match Score:</strong> {match_score}%</li>
            <li><strong>Location:</strong> {job_location}</li>
            <li><strong>Posted:</strong> {job_posted_date}</li>
        </ul>
    </div>
    
    <p>This job looks like a great fit for your experience. Don't wait too long - good jobs go fast!</p>
    
    <p><a href="{action_url}" style="background: #2c5aa0; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">View Job Details</a></p>
    
    <hr style="margin: 30px 0;">
    <p style="color: #666; font-size: 12px;">
        RateRight - Australian Construction Marketplace<br>
        <a href="{unsubscribe_url}">Unsubscribe</a>
    </p>
</div>''',
                'includes_unsubscribe': True,
                'description': 'Email template for job match notifications'
            },
            
            # SMS templates (simple text only)
            {
                'notification_type': NotificationType.RATING_RECEIVED,
                'channel': DeliveryChannel.SMS,
                'template_name': 'Rating Received SMS',
                'body_template': 'RateRight: New {rating}/5 star rating from {reviewer_name} for "{job_title}". View: {action_url}',
                'description': 'SMS template for rating received notifications'
            },
            
            {
                'notification_type': NotificationType.PAYMENT_RECEIVED,
                'channel': DeliveryChannel.SMS,
                'template_name': 'Payment Received SMS',
                'body_template': 'RateRight: Payment of ${amount} received for "{job_title}". Funds available in 1-2 days. View: {action_url}',
                'description': 'SMS template for payment received notifications'
            },
            
            # Push notification templates
            {
                'notification_type': NotificationType.ACHIEVEMENT_UNLOCKED,
                'channel': DeliveryChannel.PUSH_WEB,
                'template_name': 'Achievement Unlocked Push',
                'subject_template': 'Achievement Unlocked! 🏆',
                'body_template': '{achievement_name}: {achievement_description} (+{points_earned} points)',
                'description': 'Push notification template for achievements'
            },
            
            {
                'notification_type': NotificationType.MESSAGE_RECEIVED,
                'channel': DeliveryChannel.PUSH_WEB,
                'template_name': 'Message Received Push',
                'subject_template': 'New Message',
                'body_template': '{sender_name}: {message_preview}',
                'description': 'Push notification template for messages'
            },
            
            # Security alert template
            {
                'notification_type': NotificationType.SECURITY_ALERT,
                'channel': DeliveryChannel.EMAIL,
                'template_name': 'Security Alert Email',
                'subject_template': 'RateRight Security Alert 🔒',
                'body_template': '''G'day {user_name},

SECURITY ALERT: {alert_type}

Details:
{content}

If this was not you, please:
1. Change your password immediately
2. Review your recent account activity
3. Contact our support team

Secure your account: {action_url}

Stay safe,
The RateRight Security Team

---
RateRight - Australian Construction Marketplace''',
                'html_template': '''
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
    <h2 style="color: #dc3545;">🔒 Security Alert</h2>
    <p>G'day {user_name},</p>
    
    <div style="background: #f8d7da; padding: 20px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #dc3545;">
        <h3 style="color: #721c24; margin-top: 0;">SECURITY ALERT: {alert_type}</h3>
        <p>{content}</p>
    </div>
    
    <p><strong>If this was not you, please:</strong></p>
    <ol>
        <li>Change your password immediately</li>
        <li>Review your recent account activity</li>
        <li>Contact our support team</li>
    </ol>
    
    <p><a href="{action_url}" style="background: #dc3545; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Secure Your Account</a></p>
    
    <hr style="margin: 30px 0;">
    <p style="color: #666; font-size: 12px;">
        RateRight - Australian Construction Marketplace<br>
        Security Team
    </p>
</div>''',
                'includes_unsubscribe': False,  # Security alerts should not be unsubscribable
                'description': 'Email template for security alerts'
            }
        ]
        
        created_count = 0
        for template_data in templates:
            # Check if template already exists
            existing = NotificationTemplate.query.filter_by(
                notification_type=template_data['notification_type'],
                channel=template_data['channel']
            ).first()
            
            if not existing:
                template = NotificationTemplate(**template_data)
                db.session.add(template)
                created_count += 1
                print(f"Created template: {template_data['template_name']}")
            else:
                print(f"Template already exists: {template_data['template_name']}")
        
        db.session.commit()
        print(f"\nCreated {created_count} new notification templates")
        print("Notification templates setup complete!")

if __name__ == "__main__":
    create_basic_templates()
