from flask import Blueprint, jsonify, request
from flask_login import login_required, current_user
from models import EmailTemplate, db
from utils import admin_required

email_templates = Blueprint('email_templates', __name__)

@email_templates.route('/api/email-templates', methods=['GET'])
@login_required
def get_templates():
    templates = EmailTemplate.query.filter_by(is_active=True).all()
    return jsonify([{
        'id': template.id,
        'name': template.name,
        'subject': template.subject,
        'body': template.body,
        'created_at': template.created_at.isoformat(),
        'updated_at': template.updated_at.isoformat(),
        'created_by': template.created_by,
        'is_active': template.is_active
    } for template in templates])

@email_templates.route('/api/email-templates/<int:template_id>', methods=['GET'])
@login_required
def get_template(template_id):
    template = EmailTemplate.query.get_or_404(template_id)
    return jsonify({
        'id': template.id,
        'name': template.name,
        'subject': template.subject,
        'body': template.body,
        'created_at': template.created_at.isoformat(),
        'updated_at': template.updated_at.isoformat(),
        'created_by': template.created_by,
        'is_active': template.is_active
    })

@email_templates.route('/api/email-templates', methods=['POST'])
@login_required
@admin_required
def create_template():
    data = request.get_json()
    
    template = EmailTemplate(
        name=data['name'],
        subject=data['subject'],
        body=data['body'],
        created_by=current_user.id
    )
    
    db.session.add(template)
    db.session.commit()
    
    return jsonify({
        'id': template.id,
        'name': template.name,
        'subject': template.subject,
        'body': template.body,
        'created_at': template.created_at.isoformat(),
        'updated_at': template.updated_at.isoformat(),
        'created_by': template.created_by,
        'is_active': template.is_active
    }), 201

@email_templates.route('/api/email-templates/<int:template_id>', methods=['PUT'])
@login_required
@admin_required
def update_template(template_id):
    template = EmailTemplate.query.get_or_404(template_id)
    data = request.get_json()
    
    template.name = data.get('name', template.name)
    template.subject = data.get('subject', template.subject)
    template.body = data.get('body', template.body)
    template.is_active = data.get('is_active', template.is_active)
    
    db.session.commit()
    
    return jsonify({
        'id': template.id,
        'name': template.name,
        'subject': template.subject,
        'body': template.body,
        'created_at': template.created_at.isoformat(),
        'updated_at': template.updated_at.isoformat(),
        'created_by': template.created_by,
        'is_active': template.is_active
    })

@email_templates.route('/api/email-templates/<int:template_id>', methods=['DELETE'])
@login_required
@admin_required
def delete_template(template_id):
    template = EmailTemplate.query.get_or_404(template_id)
    template.is_active = False
    db.session.commit()
    return '', 204 