from __future__ import annotations

import typing as t

import strawberry
from sqlmodel import Field, Relationship, SQLModel

from .schema_generation import create_array_relationship_resolver, create_query_root


class AddressModel(SQLModel, table=True):
    __tablename__ = "addresses"
    id: t.Optional[int] = Field(
        default=None, primary_key=True, index=True, nullable=False
    )
    street: str
    state: str
    country: str
    zip: str
    users: t.List["UserModel"] = Relationship(back_populates="address")


class UserModel(SQLModel, table=True):
    __tablename__ = "users"
    id: t.Optional[int] = Field(
        default=None, primary_key=True, index=True, nullable=False
    )
    age: int
    password: t.Optional[str]
    address_id: t.Optional[int] = Field(default=None, foreign_key="addresses.id")
    address: t.Optional[AddressModel] = Relationship(back_populates="users")


@strawberry.experimental.pydantic.type(
    UserModel, fields=["id", "age", "password", "address_id", "address"]
)
class User:
    pass


@strawberry.experimental.pydantic.type(
    AddressModel, fields=["id", "street", "state", "country", "zip"]
)
class Address:
    users: t.List[create_array_relationship_resolver(User)] = strawberry.field(
        resolver=create_array_relationship_resolver(User)
    )


Query = create_query_root([User, Address])

schema = strawberry.Schema(query=Query)

strawberry.experimental.pydantic.type sqlmodel.Field strawberry.Schema sqlmodel.Relationship
[((906, 1017), 'strawberry.experimental.pydantic.type', 'strawberry.experimental.pydantic.type', (['UserModel'], {'fields': "['id', 'age', 'password', 'address_id', 'address']"}), "(UserModel, fields=['id', 'age',\n    'password', 'address_id', 'address'])\n", (943, 1017), False, 'import strawberry\n'), ((1044, 1151), 'strawberry.experimental.pydantic.type', 'strawberry.experimental.pydantic.type', (['AddressModel'], {'fields': "['id', 'street', 'state', 'country', 'zip']"}), "(AddressModel, fields=['id', 'street',\n    'state', 'country', 'zip'])\n", (1081, 1151), False, 'import strawberry\n'), ((1368, 1398), 'strawberry.Schema', 'strawberry.Schema', ([], {'query': 'Query'}), '(query=Query)\n', (1385, 1398), False, 'import strawberry\n'), ((313, 378), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)', 'index': '(True)', 'nullable': '(False)'}), '(default=None, primary_key=True, index=True, nullable=False)\n', (318, 378), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((487, 525), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""address"""'}), "(back_populates='address')\n", (499, 525), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((621, 686), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)', 'index': '(True)', 'nullable': '(False)'}), '(default=None, primary_key=True, index=True, nullable=False)\n', (626, 686), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((778, 825), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""addresses.id"""'}), "(default=None, foreign_key='addresses.id')\n", (783, 825), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((866, 902), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""users"""'}), "(back_populates='users')\n", (878, 902), False, 'from sqlmodel import Field, Relationship, SQLModel\n')]