from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel
from pydantic.networks import HttpUrl
from sqlmodel import Field, SQLModel

PageType = str


class Ecoindex(SQLModel):
    grade: Optional[str] = Field(
        default=None,
        title="Ecoindex grade",
        description="Is the corresponding ecoindex grade of the page (from A to G)",
    )
    score: Optional[float] = Field(
        default=None,
        title="Ecoindex score",
        description="Is the corresponding ecoindex score of the page (0 to 100)",
        ge=0,
        le=100,
    )
    ges: Optional[float] = Field(
        default=None,
        title="Ecoindex GES equivalent",
        description="Is the equivalent of greenhouse gases emission (in `gCO2e`) of the page",
        ge=0,
    )
    water: Optional[float] = Field(
        default=None,
        title="Ecoindex Water equivalent",
        description="Is the equivalent water consumption (in `cl`) of the page",
        ge=0,
    )


class Page(BaseModel):
    logs: List
    outer_html: str
    nodes: List


class PageMetrics(SQLModel):
    size: float = Field(
        default=...,
        title="Page size",
        description="Is the size of the page and of the downloaded elements of the page in KB",
        ge=0,
    )
    nodes: int = Field(
        default=...,
        title="Page nodes",
        description="Is the number of the DOM elements in the page",
        ge=0,
    )
    requests: int = Field(
        default=...,
        title="Page requests",
        description="Is the number of external requests made by the page",
        ge=0,
    )


class WindowSize(BaseModel):
    height: int = Field(
        default=...,
        title="Window height",
        description="Height of the simulated window in pixel",
    )
    width: int = Field(
        default=...,
        title="Window width",
        description="Width of the simulated window in pixel",
    )

    def __str__(self) -> str:
        return f"{self.width},{self.height}"


class WebPage(SQLModel):
    width: Optional[int] = Field(
        default=None,
        title="Page Width",
        description="Width of the simulated window in pixel",
    )
    height: Optional[int] = Field(
        default=None,
        title="Page Height",
        description="Height of the simulated window in pixel",
    )
    url: Optional[HttpUrl] = Field(
        default=None, title="Page url", description="Url of the analysed page"
    )


class Result(Ecoindex, PageMetrics, WebPage):
    date: Optional[datetime] = Field(
        default=None, title="Analysis datetime", description="Date of the analysis"
    )
    page_type: Optional[PageType] = Field(
        default=None,
        title="Page type",
        description="Is the type of the page, based ton the [opengraph type tag](https://ogp.me/#types)",
    )

sqlmodel.Field
[((242, 367), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Ecoindex grade"""', 'description': '"""Is the corresponding ecoindex grade of the page (from A to G)"""'}), "(default=None, title='Ecoindex grade', description=\n    'Is the corresponding ecoindex grade of the page (from A to G)')\n", (247, 367), False, 'from sqlmodel import Field, SQLModel\n'), ((423, 559), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Ecoindex score"""', 'description': '"""Is the corresponding ecoindex score of the page (0 to 100)"""', 'ge': '(0)', 'le': '(100)'}), "(default=None, title='Ecoindex score', description=\n    'Is the corresponding ecoindex score of the page (0 to 100)', ge=0, le=100)\n", (428, 559), False, 'from sqlmodel import Field, SQLModel\n'), ((629, 783), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Ecoindex GES equivalent"""', 'description': '"""Is the equivalent of greenhouse gases emission (in `gCO2e`) of the page"""', 'ge': '(0)'}), "(default=None, title='Ecoindex GES equivalent', description=\n    'Is the equivalent of greenhouse gases emission (in `gCO2e`) of the page',\n    ge=0)\n", (634, 783), False, 'from sqlmodel import Field, SQLModel\n'), ((843, 981), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Ecoindex Water equivalent"""', 'description': '"""Is the equivalent water consumption (in `cl`) of the page"""', 'ge': '(0)'}), "(default=None, title='Ecoindex Water equivalent', description=\n    'Is the equivalent water consumption (in `cl`) of the page', ge=0)\n", (848, 981), False, 'from sqlmodel import Field, SQLModel\n'), ((1141, 1281), 'sqlmodel.Field', 'Field', ([], {'default': '...', 'title': '"""Page size"""', 'description': '"""Is the size of the page and of the downloaded elements of the page in KB"""', 'ge': '(0)'}), "(default=..., title='Page size', description=\n    'Is the size of the page and of the downloaded elements of the page in KB',\n    ge=0)\n", (1146, 1281), False, 'from sqlmodel import Field, SQLModel\n'), ((1329, 1439), 'sqlmodel.Field', 'Field', ([], {'default': '...', 'title': '"""Page nodes"""', 'description': '"""Is the number of the DOM elements in the page"""', 'ge': '(0)'}), "(default=..., title='Page nodes', description=\n    'Is the number of the DOM elements in the page', ge=0)\n", (1334, 1439), False, 'from sqlmodel import Field, SQLModel\n'), ((1494, 1613), 'sqlmodel.Field', 'Field', ([], {'default': '...', 'title': '"""Page requests"""', 'description': '"""Is the number of external requests made by the page"""', 'ge': '(0)'}), "(default=..., title='Page requests', description=\n    'Is the number of external requests made by the page', ge=0)\n", (1499, 1613), False, 'from sqlmodel import Field, SQLModel\n'), ((1697, 1798), 'sqlmodel.Field', 'Field', ([], {'default': '...', 'title': '"""Window height"""', 'description': '"""Height of the simulated window in pixel"""'}), "(default=..., title='Window height', description=\n    'Height of the simulated window in pixel')\n", (1702, 1798), False, 'from sqlmodel import Field, SQLModel\n'), ((1842, 1941), 'sqlmodel.Field', 'Field', ([], {'default': '...', 'title': '"""Window width"""', 'description': '"""Width of the simulated window in pixel"""'}), "(default=..., title='Window width', description=\n    'Width of the simulated window in pixel')\n", (1847, 1941), False, 'from sqlmodel import Field, SQLModel\n'), ((2098, 2196), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Page Width"""', 'description': '"""Width of the simulated window in pixel"""'}), "(default=None, title='Page Width', description=\n    'Width of the simulated window in pixel')\n", (2103, 2196), False, 'from sqlmodel import Field, SQLModel\n'), ((2251, 2351), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Page Height"""', 'description': '"""Height of the simulated window in pixel"""'}), "(default=None, title='Page Height', description=\n    'Height of the simulated window in pixel')\n", (2256, 2351), False, 'from sqlmodel import Field, SQLModel\n'), ((2407, 2484), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Page url"""', 'description': '"""Url of the analysed page"""'}), "(default=None, title='Page url', description='Url of the analysed page')\n", (2412, 2484), False, 'from sqlmodel import Field, SQLModel\n'), ((2578, 2665), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Analysis datetime"""', 'description': '"""Date of the analysis"""'}), "(default=None, title='Analysis datetime', description=\n    'Date of the analysis')\n", (2583, 2665), False, 'from sqlmodel import Field, SQLModel\n'), ((2711, 2857), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'title': '"""Page type"""', 'description': '"""Is the type of the page, based ton the [opengraph type tag](https://ogp.me/#types)"""'}), "(default=None, title='Page type', description=\n    'Is the type of the page, based ton the [opengraph type tag](https://ogp.me/#types)'\n    )\n", (2716, 2857), False, 'from sqlmodel import Field, SQLModel\n')]