"""Author: Vasiliy Zdanovskiy
email: vasilyvz@gmail.com

Unit tests for mcp_proxy_adapter.transfer.models.
"""

from __future__ import annotations

from typing import cast

import pytest

from mcp_proxy_adapter.transfer.models import (
    TransferReceipt,
    TransferSession,
    TransferStatus,
)


def test_transfer_session_round_trip_dict() -> None:
    session = TransferSession(
        transfer_id="tr_test1",
        filename="a.txt",
        direction="upload",
        size_bytes=10,
        checksum_value="0" * 64,
        chunk_size=5,
        offset=3,
        status=TransferStatus.UPLOADING.value,
        storage_path="/tmp/x.upload",
        metadata_path="/tmp/x.json",
        expires_at="2099-01-01T00:00:00+00:00",
        error_message=None,
    )
    d = session.to_dict()
    session2 = TransferSession.from_dict(d)
    assert session2.transfer_id == session.transfer_id
    assert session2.filename == session.filename
    assert session2.direction == session.direction
    assert session2.size_bytes == session.size_bytes
    assert session2.checksum_algorithm == session.checksum_algorithm
    assert session2.checksum_value == session.checksum_value
    assert session2.compression == session.compression
    assert session2.chunk_size == session.chunk_size
    assert session2.offset == session.offset
    assert session2.status == session.status
    assert session2.storage_path == session.storage_path
    assert session2.metadata_path == session.metadata_path
    assert session2.expires_at == session.expires_at
    assert session2.error_message == session.error_message


def test_transfer_receipt_round_trip_dict() -> None:
    receipt = TransferReceipt(
        transfer_id="tr_r1",
        filename="f.bin",
        size_bytes=100,
        checksum_algorithm="sha256",
        checksum_value="f" * 64,
        compression="identity",
        chunk_size=10,
        offset=40,
        status=TransferStatus.UPLOADING.value,
        completed=False,
        expires_at=None,
    )
    d = receipt.to_dict()
    assert d["offset"] == 40
    assert d["completed"] is False
    assert d["transfer_id"] == "tr_r1"
    expires_at = d["expires_at"]
    assert expires_at is None
    reconstructed = TransferReceipt(
        transfer_id=cast(str, d["transfer_id"]),
        filename=cast(str, d["filename"]),
        size_bytes=cast(int, d["size_bytes"]),
        checksum_algorithm=cast(str, d["checksum_algorithm"]),
        checksum_value=cast(str, d["checksum_value"]),
        compression=cast(str, d["compression"]),
        chunk_size=cast(int, d["chunk_size"]),
        offset=cast(int, d["offset"]),
        status=cast(str, d["status"]),
        completed=cast(bool, d["completed"]),
        expires_at=expires_at,
    )
    assert reconstructed.offset == 40
    assert reconstructed.completed is False


def test_transfer_status_values_are_stable_strings() -> None:
    assert TransferStatus.CREATED.value == "created"
    assert TransferStatus.UPLOADING.value == "uploading"
    assert TransferStatus.UPLOADED.value == "uploaded"
    assert TransferStatus.DOWNLOADING.value == "downloading"
    assert TransferStatus.COMPLETED.value == "completed"
    assert TransferStatus.FAILED.value == "failed"
    assert TransferStatus.EXPIRED.value == "expired"


def test_transfer_session_from_dict_rejects_bad_checksum() -> None:
    payload = {
        "transfer_id": "tr_bad",
        "filename": "a.txt",
        "direction": "upload",
        "size_bytes": 10,
        "checksum_value": "GGGG",
        "chunk_size": 5,
        "status": TransferStatus.CREATED.value,
        "storage_path": "/tmp/x.upload",
        "metadata_path": "/tmp/x.json",
        "offset": 0,
    }
    with pytest.raises(ValueError):
        TransferSession.from_dict(payload)
