"""Pydantic models for GA4GH Passports v1.2.

GA4GH Passports are JWT tokens containing Visa claims that authorize
access to specific datasets. This module provides models for structured
validation of passport and visa claims.
"""

from __future__ import annotations

from enum import Enum
from typing import Any

from ai4gh.core.types import GA4GHBase


class VisaType(str, Enum):
    """Standard GA4GH Visa types."""

    AFFILIATION_AND_ROLE = "AffiliationAndRole"
    ACCEPTED_TERMS_AND_POLICIES = "AcceptedTermsAndPolicies"
    RESEARCHER_STATUS = "ResearcherStatus"
    CONTROLLED_ACCESS_GRANTS = "ControlledAccessGrants"
    LINKED_IDENTITIES = "LinkedIdentities"


class VisaAssertionSource(str, Enum):
    """How the Visa assertion was established."""

    SO = "so"
    SYSTEM = "system"


class VisaClaim(GA4GHBase):
    """A single GA4GH Visa claim within a Passport JWT."""

    type: VisaType
    value: str
    source: str
    by: VisaAssertionSource | None = None
    asserted: int | None = None
    conditions: list[list[dict[str, Any]]] | None = None


class PassportClaims(GA4GHBase):
    """Decoded claims from a GA4GH Passport JWT."""

    iss: str | None = None
    sub: str | None = None
    exp: int | None = None
    iat: int | None = None
    jti: str | None = None
    scope: str | None = None
    ga4gh_passport_v1: list[VisaClaim] | None = None


class AccessDecision(GA4GHBase):
    """Result of an access decision evaluation."""

    granted: bool
    reason: str
    matching_visas: list[VisaClaim] | None = None
