from enum import StrEnum

from pydantic import BaseModel, HttpUrl, field_validator

from starlette_proxy_router.types import AuthType, PathURL, ProxyPathURL


class MethodHTTP(StrEnum):
    GET = "get"
    POST = "post"
    DELETE = "delete"
    PUT = "put"
    PATCH = "patch"


class Endpoint(BaseModel):
    path: PathURL
    target: ProxyPathURL | None = None
    method: MethodHTTP | None = None
    is_auth: bool | None = None
    timeout: int | None = 10

    @field_validator("path", "target", mode="before")
    @classmethod
    def startswith(cls, value: PathURL | ProxyPathURL) -> PathURL | ProxyPathURL:
        if not value.startswith("/"):
            return f"/{value}"
        return value


class Service(BaseModel):
    service: str
    endpoints: list[Endpoint]


class ConfigAuthenfication(BaseModel):
    default_type: AuthType | None = None
    fields: list[str] | None = None
    default_on_endpoint: bool = False


class ConfigValidate(BaseModel):
    services: list[Service]
    authenfication: ConfigAuthenfication = ConfigAuthenfication()


class HostValidate(BaseModel):
    host: HttpUrl
