import aiohttp
from flask import current_app
from . import BasePlatform


class SnapchatPlatform(BasePlatform):
    API_URL = "https://adsapi.snapchat.com/v1"

    async def publish(self, video_url: str, caption: str, access_token: str, **kwargs) -> dict:
        return {"status": "error", "error": "Snapchat only supports paid ads via Marketing API. Use Snap Ads Manager."}

    async def get_metrics(self, post_id: str, access_token: str) -> dict:
        return {"views": 0, "likes": 0, "comments": 0, "shares": 0}

    def get_oauth_url(self, redirect_uri: str) -> str:
        client_id = current_app.config.get("SNAPCHAT_CLIENT_ID", "")
        return f"https://accounts.snapchat.com/login/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=snapchat-marketing-api"

    async def handle_oauth_callback(self, code: str, redirect_uri: str) -> dict:
        client_id = current_app.config.get("SNAPCHAT_CLIENT_ID", "")
        client_secret = current_app.config.get("SNAPCHAT_CLIENT_SECRET", "")
        async with aiohttp.ClientSession() as session:
            async with session.post("https://accounts.snapchat.com/login/oauth2/access_token", data={"code": code, "client_id": client_id, "client_secret": client_secret, "grant_type": "authorization_code", "redirect_uri": redirect_uri}) as resp:
                return await resp.json()
