"""Tests for rbsp.integration.filesystem -- FileOperation dataclass."""

import os
import sys
import time
import unittest

sys.path.insert(0, os.path.dirname(__file__))
from conftest import RBSPTestCase

from rbsp.integration.filesystem import FileOperation


# ---------------------------------------------------------------------------
# FileOperation -- field defaults and construction
# ---------------------------------------------------------------------------

class TestFileOperationDefaults(unittest.TestCase):
    """FileOperation default field values."""

    def test_minimal_construction(self):
        op = FileOperation(operation="read", path="/tmp/foo.txt")
        self.assertEqual(op.operation, "read")
        self.assertEqual(op.path, "/tmp/foo.txt")
        self.assertTrue(op.success)
        self.assertEqual(op.detail, "")

    def test_timestamp_auto_set(self):
        before = time.time()
        op = FileOperation(operation="write", path="/tmp/bar.txt")
        after = time.time()
        self.assertGreaterEqual(op.timestamp, before)
        self.assertLessEqual(op.timestamp, after)

    def test_explicit_timestamp(self):
        ts = 1700000000.0
        op = FileOperation(operation="read", path="x", timestamp=ts)
        self.assertEqual(op.timestamp, ts)


# ---------------------------------------------------------------------------
# FileOperation -- field values
# ---------------------------------------------------------------------------

class TestFileOperationFields(unittest.TestCase):
    """FileOperation stores all fields correctly."""

    def test_write_operation(self):
        op = FileOperation(
            operation="write",
            path="/project/src/main.py",
            success=True,
            detail="wrote 42 bytes",
        )
        self.assertEqual(op.operation, "write")
        self.assertEqual(op.path, "/project/src/main.py")
        self.assertTrue(op.success)
        self.assertEqual(op.detail, "wrote 42 bytes")

    def test_delete_operation(self):
        op = FileOperation(
            operation="delete",
            path="/project/old.txt",
            success=False,
            detail="file not found",
        )
        self.assertEqual(op.operation, "delete")
        self.assertFalse(op.success)
        self.assertEqual(op.detail, "file not found")

    def test_move_operation(self):
        op = FileOperation(
            operation="move",
            path="/src/a.py -> /src/b.py",
        )
        self.assertEqual(op.operation, "move")
        self.assertIn("->", op.path)

    def test_read_operation(self):
        op = FileOperation(operation="read", path="/etc/config")
        self.assertEqual(op.operation, "read")


# ---------------------------------------------------------------------------
# FileOperation -- equality (dataclass default)
# ---------------------------------------------------------------------------

class TestFileOperationEquality(unittest.TestCase):
    """Dataclass equality for FileOperation."""

    def test_same_fields_equal(self):
        ts = 1700000000.0
        a = FileOperation(operation="read", path="f.txt", timestamp=ts, success=True, detail="")
        b = FileOperation(operation="read", path="f.txt", timestamp=ts, success=True, detail="")
        self.assertEqual(a, b)

    def test_different_operation_not_equal(self):
        ts = 1700000000.0
        a = FileOperation(operation="read", path="f.txt", timestamp=ts)
        b = FileOperation(operation="write", path="f.txt", timestamp=ts)
        self.assertNotEqual(a, b)

    def test_different_path_not_equal(self):
        ts = 1700000000.0
        a = FileOperation(operation="read", path="a.txt", timestamp=ts)
        b = FileOperation(operation="read", path="b.txt", timestamp=ts)
        self.assertNotEqual(a, b)

    def test_different_success_not_equal(self):
        ts = 1700000000.0
        a = FileOperation(operation="read", path="f.txt", timestamp=ts, success=True)
        b = FileOperation(operation="read", path="f.txt", timestamp=ts, success=False)
        self.assertNotEqual(a, b)


# ---------------------------------------------------------------------------
# FileOperation -- repr
# ---------------------------------------------------------------------------

class TestFileOperationRepr(unittest.TestCase):
    """FileOperation repr output."""

    def test_repr_contains_class_name(self):
        op = FileOperation(operation="read", path="/tmp/x")
        r = repr(op)
        self.assertIn("FileOperation", r)

    def test_repr_contains_fields(self):
        op = FileOperation(operation="write", path="/tmp/y", success=False, detail="err")
        r = repr(op)
        self.assertIn("write", r)
        self.assertIn("/tmp/y", r)


# ---------------------------------------------------------------------------
# FileOperation -- operation types
# ---------------------------------------------------------------------------

class TestFileOperationTypes(unittest.TestCase):
    """Various operation type strings."""

    def test_all_operation_types(self):
        for op_type in ("read", "write", "delete", "move"):
            op = FileOperation(operation=op_type, path="/x")
            self.assertEqual(op.operation, op_type)

    def test_custom_operation_type(self):
        # Not restricted by enum -- any string works
        op = FileOperation(operation="custom_op", path="/y")
        self.assertEqual(op.operation, "custom_op")


if __name__ == "__main__":
    unittest.main()
