from __future__ import annotations

from typing import Final

import requests
from dopt_basics.io import combine_route
from pydantic import BaseModel
from requests import Response

from delta_barth.errors import (
    UnspecifiedRequestType,
)
from delta_barth.types import (
    ApiCredentials,
    HttpRequestTypes,
)


# ** login
class LoginRequest(BaseModel):
    userName: str
    password: str
    databaseName: str
    mandantName: str


class LoginResponse(BaseModel):
    token: str


def validate_credentials(
    username: str,
    password: str,
    database: str,
    mandant: str,
) -> ApiCredentials:
    return ApiCredentials(
        username=username,
        password=password,
        database=database,
        mandant=mandant,
    )


def ping(
    base_url: str,
    method: HttpRequestTypes,
) -> Response:
    ROUTE: Final[str] = "ping"
    URL: Final = combine_route(base_url, ROUTE)

    resp: Response
    if method == HttpRequestTypes.GET:
        resp = requests.get(URL)
    elif method == HttpRequestTypes.PUT:
        resp = requests.put(URL)
    elif method == HttpRequestTypes.DELETE:
        resp = requests.delete(URL)
    else:
        raise UnspecifiedRequestType(f"Request type {method} not defined for endpoint")

    return resp
