# -*- coding: utf-8 -*-
# @Time    : 2023/10/27 下午8:24
# @Author  : sudoskys
# @File    : private.py
# @Software: PyCharm
import time

from loguru import logger
from pydantic import BaseModel, Field

from config import provider_settings
from . import resign_provider
from .schema import BaseProvider, ProviderException
from ...sdk.endpoint import Driver

WHITE_LIST = []
if provider_settings.get("private", default=None) is not None:
    WHITE_LIST = provider_settings.private.get("private_white_list", default=[])
    logger.debug(f"🍦 Private Provider Config Loaded, WHITE_LIST({WHITE_LIST})")


class UserToday(BaseModel):
    count: int = 0
    time: int = Field(default=time.strftime("%Y%m%d", time.localtime()))


@resign_provider()
class PrivateProvider(BaseProvider):
    name = "private"

    def __database_key(self, uid: str):
        return f"driver:{self.name}:{uid}"

    def config_docs(self):
        return "This instance is only available to authorized users :)"

    async def authenticate(self, uid, token, status) -> bool:
        if uid in WHITE_LIST:
            return True
        if not Driver.from_public_env().available:
            raise ProviderException(
                "\nYou are using a public and free instance.\nThe current instance key is not configured.",
                provider=self.name
            )
        raise ProviderException(
            "This is a private instance."
            "\nPlease contact the administrator to apply for a private instance."
            f"\n You id is {uid}",
            provider=self.name
        )

    async def request_driver(self, uid, token) -> Driver:
        return Driver.from_public_env()
