from flask import Blueprint, render_template
from definitions.models import Asset
from config import item_attributes
from functions.auth import login_required
from config import BrandingConfig

viewall_bp = Blueprint('viewall', __name__)

@viewall_bp.route('/viewall/', methods=['GET'])
@login_required
def view_list():
    # Fetch all items from the database
    items = Asset.query.all()

    # Identify the primary attribute
    primary_attrib = next((attrib for attrib in item_attributes if attrib.primary), None)
    if not primary_attrib:
        return "Primary attribute not defined in configuration."

    # Render the template with items, attributes, and primary attribute
    return render_template(
        'viewList.html',
        items=items,
        item_attributes=item_attributes,
        primary_attrib=primary_attrib.attrib_name,
        brandingconfig=BrandingConfig
    )