from app import db from datetime import datetime class Building(db.Model): __tablename__ = 'buildings' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False, index=True) code = db.Column(db.String(50), unique=True, nullable=True) address = db.Column(db.String(255), nullable=True) floors = db.Column(db.Integer, default=1, nullable=False) description = db.Column(db.Text, nullable=True) is_active = db.Column(db.Boolean, default=True, nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) # Relationships classrooms = db.relationship('Classroom', backref='building_entity', lazy=True, foreign_keys='Classroom.building_id') @property def is_virtual(self): """Identifica si la sede corresponde al Campus Virtual""" c = (self.code or '').upper() n = (self.name or '').upper() return 'VIRTUAL' in c or 'VIRTUAL' in n @property def physical_capacity(self): """Capacidad física instalada real (excluye aulas virtuales con capacidad 9999 o infinitas)""" if self.is_virtual: return 0 return sum(c.capacity for c in self.classrooms if c.is_active and (c.capacity or 0) < 999 and not getattr(c, 'is_virtual', False)) @property def total_capacity(self): """Capacidad para compatibilidad histórica""" if self.is_virtual: return 0 return self.physical_capacity @property def capacity_display(self): """Representación textual clara para UI/UX""" if self.is_virtual: return "Ilimitada (Virtual)" return str(self.physical_capacity) @property def floors_display(self): """Representación de pisos""" if self.is_virtual: return "Digital" return str(self.floors) @property def classrooms_count(self): """Number of active classrooms""" return len([c for c in self.classrooms if c.is_active]) def to_dict(self): return { 'id': self.id, 'name': self.name, 'code': self.code, 'address': self.address, 'floors': self.floors, 'floors_display': self.floors_display, 'description': self.description, 'is_active': self.is_active, 'is_virtual': self.is_virtual, 'classrooms_count': self.classrooms_count, 'physical_capacity': self.physical_capacity, 'total_capacity': self.physical_capacity if not self.is_virtual else 0, 'capacity_display': self.capacity_display, 'created_at': self.created_at.isoformat() if self.created_at else None } def __repr__(self): return f''