#    Copyright 2024 Seznam.cz, a.s.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

from __future__ import annotations

import json
from dataclasses import dataclass

from nats_jwt.v2.version import LIB_VERSION
from nats_jwt.v2.account_claims import AccountClaims
from nats_jwt.v2.claims import (
    AccountClaim, Claims, ClaimsData, ClaimType, GenericFields, OperatorClaim, safe_url_base64_decode, UserClaim
)
from nats_jwt.v2.header import parse_headers
from nats_jwt.v2.operator_claims import OperatorClaims
from nats_jwt.v2.user_claims import UserClaims


@dataclass
class _Identifier:
    type: ClaimType = ""
    nats: GenericFields = None

    def kind(self) -> ClaimType:
        return self.type or self.nats.type

    def version(self) -> int:
        return 1 if self.type else self.nats.version


def load_claims(data: bytearray | bytes | str) -> tuple[int, Claims]:
    loads = json.loads(data)
    nats = loads.get("nats", {})
    _id = _Identifier(
        type=loads.get("type"),
        nats=GenericFields(
            type=nats.get("type"),
            version=nats.get("version")
        )
    )

    if _id.version() > LIB_VERSION:
        raise ValueError(f"JWT was generated by a newer version - {_id.version()}")

    def error(claims: str):
        raise ValueError(f"{claims} are not supported")

    return _id.version(), {
        OperatorClaim: OperatorClaims.load,
        AccountClaim: AccountClaims.load,
        UserClaim: UserClaims.load,
        "cluster": lambda x, y: error("ClusterClaims"),
        "server": lambda x, y: error("ServerClaims"),
    }.get(
        # key
        _id.kind(),
        # default
        lambda _, __: ClaimsData(**loads)
    )(loads, _id.version())


def decode(token: str) -> Claims:
    """ Decode takes a JWT string decodes it and validates it
    and return the embedded Claims. If the token header
    doesn't match the expected algorithm, or the claim is
    not valid or verification fails an error is returned

    Raises:
        ValueError:
        TypeError: initializer problem (extra arguments, missing arguments, etc.)
    """
    chunks = token.split(".")
    if len(chunks) != 3:
        raise ValueError("expected 3 chunks")

    chunks[0] = safe_url_base64_decode(chunks[0].encode()).decode()
    chunks[1] = safe_url_base64_decode(chunks[1].encode()).decode()

    parse_headers(chunks[0])  # throws TypeError, ValueError

    data = chunks[1].encode()
    version, claim = load_claims(data)  # throws TypeError, ValueError, binascii.Error

    # padding
    _sig = chunks[2] + "=" * (-len(chunks[2]) % 4)
    sig = safe_url_base64_decode(_sig.encode())  # throws binascii.Error

    if version <= 1 and not claim.verify(chunks[1].encode(), sig):
        raise ValueError("claim failed V1 signature verification")
    elif claim.verify(token[:len(chunks[0]) + len(chunks[1]) + 1].encode(), sig):
        raise ValueError("claim failed V2 signature verification")

    return claim
