"""TCP transport correctness tests."""
import os
import shutil
import sys
import pytest

sys.path.insert(0, os.path.dirname(__file__))
from common import (
    PROJECT_ROOT, BUILD_DIR, TEST_DATA_DIR,
    run_client, run_client_posix,
    generate_test_files, verify_transfer, clean_dir, make_result,
    get_dest_received_dir, CLIENT_CMD,
)

SOURCE_DIR = os.path.join(TEST_DATA_DIR, "tcp_source")
DEST_DIR = os.path.join(TEST_DATA_DIR, "tcp_dest")


@pytest.fixture(scope="module", autouse=True)
def setup_test_data():
    generate_test_files(SOURCE_DIR, full=False)
    clean_dir(DEST_DIR)
    yield
    shutil.rmtree(TEST_DATA_DIR, ignore_errors=True)


def _run_tcp_test(name, port, flags, use_metadata=True, posix=False):
    """Run a single TCP test case against a shared server."""
    clean_dir(DEST_DIR)
    if posix:
        result, dur = run_client_posix(SOURCE_DIR, DEST_DIR,
                                       flags=(["-M"] if use_metadata else []) + flags,
                                       port=port)
    else:
        result, dur = run_client(SOURCE_DIR, DEST_DIR,
                                 flags=(["-M"] if use_metadata else []) + flags,
                                 port=port)

    if result.returncode != 0:
        return make_result(name, False, dur, f"Exit {result.returncode}: {(result.stderr or result.stdout)[:100]}")

    received = get_dest_received_dir(DEST_DIR, SOURCE_DIR)
    mismatches, missing = verify_transfer(SOURCE_DIR, received)
    if missing:
        return make_result(name, False, dur, f"Missing: {', '.join(missing[:5])}")
    if mismatches:
        return make_result(name, False, dur, f"Mismatch: {', '.join(mismatches[:3])}")
    return make_result(name, True, dur)


class TestTCPStandard:
    def test_standard(self, shared_server):
        r = _run_tcp_test("Standard", shared_server.port, [])
        assert r["status"] == "Success", r["error"]

    def test_posix_args(self, shared_server):
        r = _run_tcp_test("Posix Args", shared_server.port, [], posix=True)
        assert r["status"] == "Success", r["error"]

    def test_no_metadata(self, shared_server):
        r = _run_tcp_test("Standard (no metadata)", shared_server.port, [], use_metadata=False)
        assert r["status"] == "Success", r["error"]


class TestTCPFlags:
    def test_multithreading(self, shared_server):
        r = _run_tcp_test("Multithreading (-m)", shared_server.port, ["-m"])
        assert r["status"] == "Success", r["error"]

    def test_compression(self, shared_server):
        r = _run_tcp_test("Compression (-c)", shared_server.port, ["-c"])
        assert r["status"] == "Success", r["error"]

    def test_chunk_serialization(self, shared_server):
        r = _run_tcp_test("Chunk Serialization (-s)", shared_server.port, ["-s"])
        assert r["status"] == "Success", r["error"]

    def test_compression_chunk(self, shared_server):
        r = _run_tcp_test("Compression + Chunk (-c -s)", shared_server.port, ["-c", "-s"])
        assert r["status"] == "Success", r["error"]

    def test_multithread_compression(self, shared_server):
        r = _run_tcp_test("Multithreading + Compression (-m -c)", shared_server.port, ["-m", "-c"])
        assert r["status"] == "Success", r["error"]

    def test_multithread_chunk(self, shared_server):
        r = _run_tcp_test("Multithreading + Chunk (-m -s)", shared_server.port, ["-m", "-s"])
        assert r["status"] == "Success", r["error"]

    def test_all_flags(self, shared_server):
        r = _run_tcp_test("Multithread + Compression + Chunk (-m -c -s)", shared_server.port, ["-m", "-c", "-s"])
        assert r["status"] == "Success", r["error"]

    def test_sendfile(self, shared_server):
        r = _run_tcp_test("Sendfile (-f)", shared_server.port, ["-f"])
        assert r["status"] == "Success", r["error"]

    def test_sendfile_multithread(self, shared_server):
        r = _run_tcp_test("Sendfile + Multithreading (-f -m)", shared_server.port, ["-f", "-m"])
        assert r["status"] == "Success", r["error"]


class TestTCPChunkSize:
    def test_custom_chunk_size(self, shared_server):
        r = _run_tcp_test("Chunk size 5MB", shared_server.port, ["--chunk-size", "5242880"])
        assert r["status"] == "Success", r["error"]

    def test_small_chunk_size(self, shared_server):
        r = _run_tcp_test("Chunk size 1KB", shared_server.port, ["--chunk-size", "1024"])
        assert r["status"] == "Success", r["error"]
