from models.request import GetTokenRequest
from utils.response_builder import ResponseBuilder
from config.settings import settings
from typing import Dict, Union


class TokenService:
    """Token管理服务"""

    def __init__(self):
        self.project_counter = settings.INITIAL_PROJECT_ID
        self.tokens = {}  # projectId -> token_data

    def create_token(self, request: Union[GetTokenRequest, Dict]) -> Dict:
        """创建Token

        Args:
            request: 获取Token请求（可以是 GetTokenRequest 对象或字典）

        Returns:
            Token响应字典
        """
        # 支持 dict 或 GetTokenRequest 对象
        if isinstance(request, dict):
            project_no = request.get("projectNo")
            entity_name = request.get("entityName")
        else:
            project_no = request.projectNo
            entity_name = request.entityName

        # 生成唯一项目ID
        self.project_counter += 1
        project_id = self.project_counter

        # 构建响应
        response = ResponseBuilder.build_success_response(
            "token",
            project_id=project_id,
            project_no=project_no,
            entity_name=entity_name
        )

        # 存储token信息
        self.tokens[project_id] = response.get("data")

        return response

    def get_project(self, project_id: int) -> Dict:
        """获取项目信息

        Args:
            project_id: 项目ID

        Returns:
            项目信息字典
        """
        return self.tokens.get(project_id)
