# Path: sqlviewer/glimpse/models.py
# class Version(models.Model):
#     number = models.IntegerField()
#     label = models.CharField(max_length=128, blank=False, null=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     model = models.ForeignKey(Model)
# 
#     def diagrams(self):
#         return Diagram.objects.filter(model_version=self).all()
# 
#     def save(self, *args, **kwargs):
#         if not self.number:
#             self.number = Version.objects.filter(model=self.model).count()
#         super(Version, self).save(*args, **kwargs)
# 
#     def to_json(self, shallow=False):
#         data = {"id": self.id,
#                 "number": self.number,
#                 "label": self.label}
#         if not shallow:
#             data['diagrams'] = [d.to_json(shallow=True) for d in self.diagrams()]
#         return data
# 
# class Table(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
#     comment = models.TextField(blank=True, null=True)
#     model_version = models.ForeignKey(Version)
# 
#     def columns(self):
#         return Column.objects.filter(table=self)
# 
#     def to_json(self, shallow=False):
#         data = {
#             "id": str(self.extid),
#             "name": self.name,
#             "comment": self.comment
#         }
# 
#         if not shallow:
#             data["columns"] = [col.to_json() for col in self.columns()]
#         return data
# 
# class ForeignKey(UUIDModel):
#     TYPE_COLUMN = 'column'
#     TYPE_TABLE = 'table'
#     TYPE_CHOICES = (
#         (TYPE_COLUMN, 'column'),
#         (TYPE_TABLE, 'table')
#     )
# 
#     type = models.CharField(max_length=10, choices=TYPE_CHOICES, default=TYPE_COLUMN)
#     target_table = models.ForeignKey(Table, related_name='target_table')
#     target_column = models.ForeignKey(Column, null=True, related_name='target_column')
#     source_table = models.ForeignKey(Table, related_name='source_table')
#     source_column = models.ForeignKey(Column, null=True, related_name='source_column')
#     model_version = models.ForeignKey(Version)
# 
#     def to_json(self):
#         return {
#             "id": str(self.extid),
#             "type": self.type,
#             "source": {
#                 "tableId": str(self.source_table.extid),
#                 "columnId": str(self.source_column.extid)
#             },
#             "target": {
#                 "tableId": str(self.target_table.extid),
#                 "columnId": str(self.target_column.extid)
#             }
#         }


from sqlviewer.glimpse.models import Version, Table, ForeignKey

__author__ = 'Stefan Martinov <stefan.martinov@gmail.com>'


def connect_tables(version: Version, source_table: str, target_table: str, depth: int = 5):
    search_graph = build_search_graph(version)
    results = breadth_first_search(search_graph, source_table, target_table, depth)
    return results



# Path: sqlviewer/glimpse/models.py
# class Version(models.Model):
#     number = models.IntegerField()
#     label = models.CharField(max_length=128, blank=False, null=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     model = models.ForeignKey(Model)
# 
#     def diagrams(self):
#         return Diagram.objects.filter(model_version=self).all()
# 
#     def save(self, *args, **kwargs):
#         if not self.number:
#             self.number = Version.objects.filter(model=self.model).count()
#         super(Version, self).save(*args, **kwargs)
# 
#     def to_json(self, shallow=False):
#         data = {"id": self.id,
#                 "number": self.number,
#                 "label": self.label}
#         if not shallow:
#             data['diagrams'] = [d.to_json(shallow=True) for d in self.diagrams()]
#         return data
# 
# class Table(UUIDModel):
#     name = models.CharField(max_length=128, blank=False, null=False)
#     comment = models.TextField(blank=True, null=True)
#     model_version = models.ForeignKey(Version)
# 
#     def columns(self):
#         return Column.objects.filter(table=self)
# 
#     def to_json(self, shallow=False):
#         data = {
#             "id": str(self.extid),
#             "name": self.name,
#             "comment": self.comment
#         }
# 
#         if not shallow:
#             data["columns"] = [col.to_json() for col in self.columns()]
#         return data
# 
# class ForeignKey(UUIDModel):
#     TYPE_COLUMN = 'column'
#     TYPE_TABLE = 'table'
#     TYPE_CHOICES = (
#         (TYPE_COLUMN, 'column'),
#         (TYPE_TABLE, 'table')
#     )
# 
#     type = models.CharField(max_length=10, choices=TYPE_CHOICES, default=TYPE_COLUMN)
#     target_table = models.ForeignKey(Table, related_name='target_table')
#     target_column = models.ForeignKey(Column, null=True, related_name='target_column')
#     source_table = models.ForeignKey(Table, related_name='source_table')
#     source_column = models.ForeignKey(Column, null=True, related_name='source_column')
#     model_version = models.ForeignKey(Version)
# 
#     def to_json(self):
#         return {
#             "id": str(self.extid),
#             "type": self.type,
#             "source": {
#                 "tableId": str(self.source_table.extid),
#                 "columnId": str(self.source_column.extid)
#             },
#             "target": {
#                 "tableId": str(self.target_table.extid),
#                 "columnId": str(self.target_column.extid)
#             }
#         }


# Path: sqlviewer/connect/services.py
from sqlviewer.glimpse.models import Version, Table, ForeignKey


__author__ = 'Stefan Martinov <stefan.martinov@gmail.com>'


def connect_tables(version: Version, source_table: str, target_table: str, depth: int = 5):
    search_graph = build_search_graph(version)
    results = breadth_first_search(search_graph, source_table, target_table, depth)
    return results



def build_search_graph(version: Version) -> dict: