"""Mailbox resource — Outlook, Gmail, Facebook, Instagram, Hotmail Creator."""
from __future__ import annotations

from typing import List, Optional
from .._http import HttpClient


class MailboxResource:
    def __init__(self, http: HttpClient):
        self._http = http

    # ── Outlook ──────────────────────────────────────────────────────────────

    def read_outlook(
        self,
        email: str,
        refresh_token: str,
        client_id: str,
        method: str = "graph",
    ) -> dict:
        """
        Read Outlook inbox messages with OTP/code extraction.

        Args:
            email:         Outlook email address.
            refresh_token: OAuth2 refresh token.
            client_id:     Azure AD app client ID.
            method:        ``'graph'`` (default) or ``'oauth2'``.
        """
        return self._http.post("/v1/mailbox/outlook/read", {
            "email": email, "refreshToken": refresh_token,
            "clientId": client_id, "method": method,
        })

    def refresh_outlook_token(
        self,
        client_id: str,
        refresh_token: str,
        method: str = "graph",
    ) -> dict:
        """
        Refresh an Outlook OAuth access token.

        Returns:
            dict: ``{ accessToken, expiresIn, refreshToken, tokenType }``
        """
        return self._http.post("/v1/mailbox/outlook/refresh", {
            "clientId": client_id, "refreshToken": refresh_token, "method": method,
        })

    def check_outlook(
        self,
        email: str,
        refresh_token: str,
        client_id: str,
        method: str = "graph",
    ) -> dict:
        """
        Check if an Outlook account is live or dead.

        Returns:
            dict: ``{ email, accountStatus: 'Live' | 'Die', checkedMethod }``
        """
        return self._http.post("/v1/mailbox/outlook/check", {
            "email": email, "refreshToken": refresh_token,
            "clientId": client_id, "method": method,
        })

    # ── Gmail ────────────────────────────────────────────────────────────────

    def read_gmail(self, email: str, order_id: str) -> dict:
        """
        Read Gmail messages for an account purchased via HS Mail.
        Requires GMAIL subscription.
        """
        return self._http.post("/v1/mailbox/gmail/read", {
            "email": email, "orderId": order_id,
        })

    def check_gmail(self, email: str, proxy: Optional[str] = None) -> dict:
        """
        Check if a Gmail account is live or dead.
        Requires GMAIL subscription.
        """
        body: dict = {"email": email}
        if proxy is not None:
            body["proxy"] = proxy
        return self._http.post("/v1/gmail/check", body)

    # ── Facebook ─────────────────────────────────────────────────────────────

    def check_facebook_bulk(self, accounts: List[str]) -> dict:
        """
        Bulk live/dead check for Facebook accounts.
        Requires FB subscription.

        Args:
            accounts: List of account strings (e.g. ``['email:pass', ...]``).

        Returns:
            dict: ``{ totalChecked, liveCount, deadCount, results[] }``
        """
        return self._http.post("/v1/mailbox/facebook/live-check", {"accounts": accounts})

    def check_facebook(self, user_id: str, proxy: Optional[str] = None) -> dict:
        """
        Single Facebook account check by profile ID.
        Requires FB subscription.
        """
        body: dict = {"id": user_id}
        if proxy is not None:
            body["proxy"] = proxy
        return self._http.post("/v1/social/facebook/check", body)

    # ── Instagram ────────────────────────────────────────────────────────────

    def check_instagram(self, username: str, proxy: Optional[str] = None) -> dict:
        """
        Check if an Instagram account is live.
        Requires IG subscription.
        """
        body: dict = {"username": username}
        if proxy is not None:
            body["proxy"] = proxy
        return self._http.post("/v1/social/instagram/check", body)

    # ── Hotmail Creator ───────────────────────────────────────────────────────

    def create_hotmail_order(
        self,
        emails: List[str],
        password_type: str = "USER_ONLY",
        global_password: Optional[str] = None,
    ) -> dict:
        """
        Place a bulk Hotmail account creation order.

        Args:
            emails:          List of email addresses to process (max 1,000).
            password_type:   ``'USER_ONLY'`` or ``'USER_PASS'``.
            global_password: Required when password_type is ``'USER_PASS'``.

        Returns:
            dict: ``{ orderId, totalBdt, totalUsd, quantity, source }``
        """
        body: dict = {"emails": emails, "passwordType": password_type}
        if global_password is not None:
            body["globalPassword"] = global_password
        return self._http.post("/v1/mailbox/hotmail/create-order", body)
