"""
GHL OAuth Configuration

Stores Client ID, Client Secret, and OAuth endpoints.
Client ID and Secret come from the GHL Marketplace App registration.
"""

import os
from dataclasses import dataclass, field
from typing import List


@dataclass
class GHLOAuthConfig:
    """GHL OAuth Marketplace App configuration."""

    # OAuth App credentials (from GHL Marketplace Developer Portal)
    client_id: str = os.environ.get("GHL_OAUTH_CLIENT_ID", "")
    client_secret: str = os.environ.get("GHL_OAUTH_CLIENT_SECRET", "")

    # OAuth endpoints (GHL standard — do not change)
    auth_url: str = "https://marketplace.gohighlevel.com/oauth/chooselocation"
    token_url: str = "https://services.leadconnectorhq.com/oauth/token"
    location_token_url: str = "https://services.leadconnectorhq.com/oauth/locationToken"

    # Callback URL (where GHL sends the auth code after user approves)
    redirect_uri: str = os.environ.get(
        "GHL_OAUTH_REDIRECT_URI",
        "https://api.sunaivadigital.com/api/ghl/oauth/callback"
    )

    # Token lifecycle
    access_token_ttl_seconds: int = 86399  # ~24 hours
    refresh_token_ttl_days: int = 365
    refresh_buffer_seconds: int = 300  # refresh 5 min before expiry

    # Agency info
    company_id: str = os.environ.get("GHL_COMPANY_ID", "FNZ2Np4IXV65UZfYGO3A")

    # API version
    api_version: str = "2021-07-28"
    api_base_url: str = "https://services.leadconnectorhq.com"

    # Required scopes for full sub-account access
    scopes: List[str] = field(default_factory=lambda: [
        "contacts.readonly",
        "contacts.write",
        "conversations.readonly",
        "conversations.write",
        "conversations/message.readonly",
        "conversations/message.write",
        "calendars.readonly",
        "calendars.write",
        "calendars/events.readonly",
        "calendars/events.write",
        "opportunities.readonly",
        "opportunities.write",
        "locations.readonly",
        "locations.write",
        "locations/customFields.readonly",
        "locations/customFields.write",
        "locations/customValues.readonly",
        "locations/customValues.write",
        "locations/tags.readonly",
        "locations/tags.write",
        "workflows.readonly",
        "funnels/redirect.readonly",
        "funnels/redirect.write",
        "oauth.readonly",
        "oauth.write",
    ])

    @property
    def scope_string(self) -> str:
        return " ".join(self.scopes)

    @property
    def is_configured(self) -> bool:
        return bool(self.client_id and self.client_secret)

    def get_auth_url(self, state: str) -> str:
        """Build the full authorization URL for the OAuth flow."""
        from urllib.parse import urlencode
        params = {
            "response_type": "code",
            "redirect_uri": self.redirect_uri,
            "client_id": self.client_id,
            "scope": self.scope_string,
            "state": state,
        }
        return f"{self.auth_url}?{urlencode(params)}"
