"""Tests for Crypt4GH models."""

from __future__ import annotations

from ai4gh.specs.crypt4gh.models import (
    MAGIC_BYTES,
    VERSION,
    Crypt4GHHeader,
    DataEncryptionMethod,
    EncryptionResult,
    HeaderPacket,
    HeaderPacketType,
    KeyPair,
)


class TestConstants:
    def test_magic_bytes(self) -> None:
        assert MAGIC_BYTES == b"crypt4gh"

    def test_version(self) -> None:
        assert VERSION == 1


class TestHeaderPacketType:
    def test_enum_values(self) -> None:
        assert HeaderPacketType.DATA_ENCRYPTION_PARAMETERS == 0
        assert HeaderPacketType.DATA_EDIT_LIST == 1


class TestDataEncryptionMethod:
    def test_enum_values(self) -> None:
        assert DataEncryptionMethod.CHACHA20_IETF_POLY1305 == 0


class TestKeyPair:
    def test_creation(self) -> None:
        kp = KeyPair(
            public_key=b"public_key_bytes",
            private_key=b"private_key_bytes",
        )
        assert kp.public_key == b"public_key_bytes"
        assert kp.private_key == b"private_key_bytes"


class TestHeaderPacket:
    def test_data_encryption(self) -> None:
        packet = HeaderPacket(
            packet_type=HeaderPacketType.DATA_ENCRYPTION_PARAMETERS,
            data_encryption_method=DataEncryptionMethod.CHACHA20_IETF_POLY1305,
            data_key=b"symmetric_key_32bytes_padding_00",
        )
        assert packet.packet_type is HeaderPacketType.DATA_ENCRYPTION_PARAMETERS
        assert packet.data_key is not None

    def test_edit_list(self) -> None:
        packet = HeaderPacket(
            packet_type=HeaderPacketType.DATA_EDIT_LIST,
            edit_list=[(0, 1000), (2000, 3000)],
        )
        assert packet.packet_type is HeaderPacketType.DATA_EDIT_LIST
        assert len(packet.edit_list) == 2


class TestCrypt4GHHeader:
    def test_defaults(self) -> None:
        header = Crypt4GHHeader()
        assert header.magic == MAGIC_BYTES
        assert header.version == VERSION
        assert header.header_packet_count == 0

    def test_with_packets(self) -> None:
        header = Crypt4GHHeader(
            header_packet_count=1,
            packets=[
                HeaderPacket(
                    packet_type=HeaderPacketType.DATA_ENCRYPTION_PARAMETERS,
                    data_encryption_method=DataEncryptionMethod.CHACHA20_IETF_POLY1305,
                    data_key=b"key",
                )
            ],
        )
        assert header.header_packet_count == 1
        assert len(header.packets) == 1


class TestEncryptionResult:
    def test_creation(self) -> None:
        result = EncryptionResult(
            encrypted_data=b"encrypted_bytes",
            header=Crypt4GHHeader(),
            data_key=b"symmetric_key",
        )
        assert result.encrypted_data == b"encrypted_bytes"
        assert result.data_key == b"symmetric_key"
