from typing import List
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from backend.db.models.guid import GUID

from .._model_base import SqlAlchemyBase

class Houses(SqlAlchemyBase):
  __tablename__ = 'houses'

  id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate, index=True)
  icon: Mapped[str | None] = mapped_column(String, index=True, nullable=False)
  name: Mapped[str | None] = mapped_column(String, index=True, nullable=False)
  address: Mapped[str | None] = mapped_column(String, index=True, nullable=False)

  areas: Mapped[List["Areas"]] = relationship()

  def __repr__(self):
    return f"{self.__class__.__name__}, name: {self.name}"

class Areas(SqlAlchemyBase):
  __tablename__ = 'areas'

  id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate, index=True)
  house_id: Mapped[GUID] = mapped_column(GUID, ForeignKey('houses.id'), index=True, nullable=False)
  name: Mapped[str | None] = mapped_column(String, index=True, nullable=False)
  desc: Mapped[str | None] = mapped_column(String, index=True, nullable=False)

  user: Mapped['Houses'] = relationship(back_populates="areas")

  def __repr__(self):
    return f"{self.__class__.__name__}, name: {self.name}"
