# Final fix for beach_design route

# Read the current file
with open(r'd:\bookbeach\backend\app\routes\admin_routes.py', 'r', encoding='utf-8') as f:
    content = f.read()

# The beach_design route
beach_design_route = '''

@admin_bp.route('/beaches/<beach_id>/design', methods=['GET'])
@login_required
def beach_design(beach_id):
    """Full screen beach design page"""
    # Check if user is logged in and is admin
    if not session.get('is_admin'):
        flash('You must be logged in as an administrator to access this page.', 'error')
        return redirect(url_for('admin.admin_login'))
    
    try:
        # Redirect to the static Vue application with just the beach ID
        return redirect(f"/static/BeachDesign/index.html?beach_id={beach_id}")
        
    except Exception as e:
        error_msg = f'Error loading beach design: {str(e)}'
        print(f"BEACH DESIGN ERROR: {error_msg}")
        flash(error_msg, 'error')
        # Redirect back to beach edit page
        return redirect(url_for('admin.edit_beach', beach_id=beach_id))
'''

# Append the route if it's not already there
if '@admin_bp.route(\'/beaches/<beach_id>/design\'' not in content:
    # Make sure the file ends with a newline
    if not content.endswith('\n'):
        content += '\n'
    
    content += beach_design_route
    
    # Write back to the file
    with open(r'd:\bookbeach\backend\app\routes\admin_routes.py', 'w', encoding='utf-8') as f:
        f.write(content)
    
    print("Successfully added beach_design route to admin_routes.py")
else:
    print("beach_design route already exists in admin_routes.py")