class InternalDatabaseRouter:
    """
    Router to control database operations for accessing internal dashboard data
    from the clients dashboard.
    """
    # Apps que viven en la base de datos de internal
    route_app_labels = {
        'core',          # Probablemente contiene modelos base
        'annotations',   # Anotaciones de partidos
        'sports',        # Deportes, equipos, jugadores, partidos
        'analytics',     # Análisis y estadísticas
    }
    
    def db_for_read(self, model, **hints):
        """Read operations for internal models go to tikistats_internal"""
        if model._meta.app_label in self.route_app_labels:
            return 'tikistats_internal'
        return None
    
    def db_for_write(self, model, **hints):
        """Write operations for internal models go to tikistats_internal"""
        if model._meta.app_label in self.route_app_labels:
            return 'tikistats_internal'
        return None
    
    def allow_relation(self, obj1, obj2, **hints):
        """Allow relations within internal app or both in clients db"""
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
            return True
        return None
    
    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """Ensure internal app only migrates to tikistats_internal"""
        if app_label in self.route_app_labels:
            return db == 'tikistats_internal'
        return None