from flask import Blueprint, render_template, request, redirect, url_for, flash
from controllers.auth_controller import login_required, admin_required
from models.temperament_model import TemperamentModel
from utils.i18n import t

temperament_bp = Blueprint('temperament', __name__)

@temperament_bp.route('/')
@login_required
def index():
    temperaments = TemperamentModel.get_all_temperaments()
    return render_template('temperaments/index.html', temperaments=temperaments)

@temperament_bp.route('/create', methods=['GET', 'POST'])
@login_required
@admin_required
def create():
    if request.method == 'POST':
        name = request.form.get('name')
        description = request.form.get('description', '')
        color = request.form.get('color', '#007bff')
        is_active = request.form.get('is_active') == 'on'
        
        if not name:
            flash(t('messages.name_required', 'Name is required'), 'error')
            return render_template('temperaments/create.html')
        
        TemperamentModel.create_temperament(name, description, color, is_active)
        flash(t('messages.temperament_created', 'Temperament created successfully!'), 'success')
        return redirect(url_for('temperament.index'))
    
    return render_template('temperaments/create.html')

@temperament_bp.route('/edit/<int:temperament_id>', methods=['GET', 'POST'])
@login_required
@admin_required
def edit(temperament_id):
    temperament = TemperamentModel.get_temperament_by_id(temperament_id)
    if not temperament:
        flash(t('messages.temperament_not_found', 'Temperament not found'), 'error')
        return redirect(url_for('temperament.index'))
    
    if request.method == 'POST':
        name = request.form.get('name')
        description = request.form.get('description', '')
        color = request.form.get('color', '#007bff')
        is_active = request.form.get('is_active') == 'on'
        
        if not name:
            flash(t('messages.name_required', 'Name is required'), 'error')
            return render_template('temperaments/edit.html', temperament=temperament)
        
        TemperamentModel.update_temperament(temperament_id, name, description, color, is_active)
        flash(t('messages.temperament_updated', 'Temperament updated successfully!'), 'success')
        return redirect(url_for('temperament.index'))
    
    return render_template('temperaments/edit.html', temperament=temperament)

@temperament_bp.route('/delete/<int:temperament_id>')
@login_required
@admin_required
def delete(temperament_id):
    temperament = TemperamentModel.get_temperament_by_id(temperament_id)
    if not temperament:
        flash(t('messages.temperament_not_found', 'Temperament not found'), 'error')
        return redirect(url_for('temperament.index'))
    
    TemperamentModel.delete_temperament(temperament_id)
    flash(t('messages.temperament_deleted', 'Temperament deleted successfully!'), 'success')
    return redirect(url_for('temperament.index'))
