"""LinkedIn Organizations API mock responses based on API documentation.

Reference: https://learn.microsoft.com/en-us/linkedin/marketing/community-management/organizations/organization-lookup-api

The organizationAcls endpoint returns organizations the authenticated user
can administer with their respective roles.
"""
# mypy: disable-error-code=list-item

from typing import Any

# Sample organization data based on API docs
SAMPLE_ORGANIZATIONS = {
    "acme_corp": {
        "organization_urn": "urn:li:organization:12345",
        "organization_id": "12345",
        "name": "Acme Corp",
        "vanity_name": "acme-corp",
        "logo_url": "https://media.licdn.com/dms/image/C4E0BAQEexample/company-logo_200_200/0/acme-logo.png",
        "website_url": "https://www.acmecorp.com",
        "description": "Leading provider of innovative solutions",
        "industry": "Technology, Information and Internet",
        "staff_count_range": "EMPLOYEES_501_TO_1000",
    },
    "tech_startup": {
        "organization_urn": "urn:li:organization:67890",
        "organization_id": "67890",
        "name": "TechStartup Inc",
        "vanity_name": "techstartup",
        "logo_url": "https://media.licdn.com/dms/image/C4E0BAQEexample/company-logo_200_200/0/techstartup-logo.png",
        "website_url": "https://www.techstartup.io",
        "description": "Building the future of AI",
        "industry": "Software Development",
        "staff_count_range": "EMPLOYEES_11_TO_50",
    },
    "enterprise_solutions": {
        "organization_urn": "urn:li:organization:11111",
        "organization_id": "11111",
        "name": "Enterprise Solutions Ltd",
        "vanity_name": "enterprise-solutions",
        "logo_url": "https://media.licdn.com/dms/image/C4E0BAQEexample/company-logo_200_200/0/enterprise-logo.png",
        "website_url": "https://www.enterprise-solutions.com",
        "description": "Enterprise-grade business solutions",
        "industry": "IT Services and IT Consulting",
        "staff_count_range": "EMPLOYEES_1001_TO_5000",
    },
}


def mock_organizations_response(
    organization_keys: list[str] | None = None,
    include_role: bool = True,
) -> dict[str, Any]:
    """Generate a mock organizationAcls response.

    Args:
        organization_keys: Keys from SAMPLE_ORGANIZATIONS to include.
                          Defaults to ["acme_corp", "tech_startup"]
        include_role: Whether to include role information

    Returns:
        Dict matching LinkedIn's organizationAcls response format
    """
    if organization_keys is None:
        organization_keys = ["acme_corp", "tech_startup"]

    elements = []
    for key in organization_keys:
        org = SAMPLE_ORGANIZATIONS.get(key)
        if org:
            element = {
                "organizationalEntity": org["organization_urn"],
                "role": "ADMINISTRATOR" if include_role else None,
                "state": "APPROVED",
            }
            if include_role:
                element["role"] = "ADMINISTRATOR"
            elements.append(element)

    return {
        "elements": elements,
        "paging": {
            "count": len(elements),
            "start": 0,
            "links": [],
        },
    }


def mock_organization_details(
    organization_key: str = "acme_corp",
    include_all_fields: bool = True,
) -> dict[str, Any]:
    """Generate a mock organization details response.

    Args:
        organization_key: Key from SAMPLE_ORGANIZATIONS
        include_all_fields: Whether to include all optional fields

    Returns:
        Dict matching LinkedIn's organization response format
    """
    org = SAMPLE_ORGANIZATIONS.get(organization_key, SAMPLE_ORGANIZATIONS["acme_corp"])

    response = {
        "id": org["organization_id"],
        "vanityName": org["vanity_name"],
        "localizedName": org["name"],
        "logoV2": {
            "original": org["logo_url"],
            "cropped": org["logo_url"],
        },
    }

    if include_all_fields:
        response.update(
            {
                "localizedWebsite": org.get("website_url", ""),
                "description": {
                    "localized": {"en_US": org.get("description", "")},
                    "preferredLocale": {"country": "US", "language": "en"},
                },
                "industries": [org.get("industry", "Technology")],
                "staffCountRange": org.get("staff_count_range", "EMPLOYEES_11_TO_50"),
                "organizationType": "PUBLIC_COMPANY",
                "foundedOn": {"year": 2020},
                "specialties": ["Software", "Technology", "Innovation"],
                "locations": [
                    {
                        "country": "US",
                        "geographicArea": "California",
                        "city": "San Francisco",
                        "postalCode": "94105",
                        "streetAddressLine1": "123 Tech Street",
                    }
                ],
            }
        )

    return response


def mock_organization_lookup_response(
    organization_urns: list[str] | None = None,
) -> dict[str, Any]:
    """Generate a mock organization lookup (batch) response.

    Args:
        organization_urns: List of organization URNs to look up

    Returns:
        Dict matching LinkedIn's organization batch lookup response
    """
    if organization_urns is None:
        organization_urns = [
            SAMPLE_ORGANIZATIONS["acme_corp"]["organization_urn"],
        ]

    results = {}
    for urn in organization_urns:
        # Find matching org by URN
        for org in SAMPLE_ORGANIZATIONS.values():
            if org["organization_urn"] == urn:
                results[urn] = mock_organization_details(
                    organization_key=next(k for k, v in SAMPLE_ORGANIZATIONS.items() if v["organization_urn"] == urn)
                )
                break

    return {"results": results}


# Pre-built scenarios
ORG_SCENARIOS = {
    "single_admin": mock_organizations_response(["acme_corp"]),
    "multiple_admin": mock_organizations_response(["acme_corp", "tech_startup"]),
    "all_orgs": mock_organizations_response(list(SAMPLE_ORGANIZATIONS.keys())),
    "empty": {"elements": [], "paging": {"count": 0, "start": 0, "links": []}},
}
