from typing import Any, Dict, Optional

from app.utils.douyinRequest import DouyinRequest
from app.models.user_oauth import UserOAuth


class DouyinApi:
    def __init__(self):
        self.request = DouyinRequest()

    async def get_advertiser_by_agent(self, oauth_id: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        if not oauth_id:
            raise RuntimeError('OAuth ID is not set.')

        url = "https://api.oceanengine.com/open_api/2/agent/advertiser/select/"
        return await self.request.request_with_token_with_context(
            oauth_id,
            url,
            'GET',
            {'params': params or {}}
        )

    async def upload_image_material(self, oauth_id: str, data: Optional[Dict[str, Any]] = None, files: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        if not oauth_id:
            raise RuntimeError('OAuth ID is not set.')

        url = "https://api.oceanengine.com/open_api/2/file/image/ad/"
        options: Dict[str, Any] = {}
        if data:
            options['data'] = data
        if files:
            options['files'] = files
        return await self.request.request_with_token_with_context(
            oauth_id,
            url,
            'POST',
            options
        )

    async def upload_video_material(self, oauth_id: str, data: Optional[Dict[str, Any]] = None, files: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        if not oauth_id:
            raise RuntimeError('OAuth ID is not set.')

        url = "https://api.oceanengine.com/open_api/2/file/video/ad/"
        options: Dict[str, Any] = {}
        if data:
            options['data'] = data
        if files:
            options['files'] = files
        return await self.request.request_with_token_with_context(
            oauth_id,
            url,
            'POST',
            options
        )
    
    #获取区域信息
    async def get_area(self, oauth_id: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        if not oauth_id:
            raise RuntimeError('OAuth ID is not set.')

        url = "https://api.oceanengine.com/open_api/2/tools/admin/info/"
        return await self.request.request_with_token_with_context(
            oauth_id,
            url,
            'GET',
            {'params': params or {}}
        )