"""Tests for storage prefixes module."""

import pytest

from shared.storage.prefixes import PREFIXES, StoragePrefixes


class TestStoragePrefixes:
    """Tests for StoragePrefixes class."""

    def test_prefixes_are_strings(self) -> None:
        """All prefix constants should be strings."""
        assert isinstance(PREFIXES.DOCUMENTS, str)
        assert isinstance(PREFIXES.IMAGES, str)
        assert isinstance(PREFIXES.UPLOADS, str)
        assert isinstance(PREFIXES.RESULTS, str)
        assert isinstance(PREFIXES.EXPORTS, str)
        assert isinstance(PREFIXES.DATASETS, str)
        assert isinstance(PREFIXES.MODELS, str)
        assert isinstance(PREFIXES.RAW_PDFS, str)
        assert isinstance(PREFIXES.STRUCTURED_DATA, str)
        assert isinstance(PREFIXES.ADMIN_IMAGES, str)

    def test_prefixes_are_non_empty(self) -> None:
        """All prefix constants should be non-empty."""
        assert PREFIXES.DOCUMENTS
        assert PREFIXES.IMAGES
        assert PREFIXES.UPLOADS
        assert PREFIXES.RESULTS
        assert PREFIXES.EXPORTS
        assert PREFIXES.DATASETS
        assert PREFIXES.MODELS
        assert PREFIXES.RAW_PDFS
        assert PREFIXES.STRUCTURED_DATA
        assert PREFIXES.ADMIN_IMAGES

    def test_prefixes_have_no_leading_slash(self) -> None:
        """Prefixes should not start with a slash for portability."""
        assert not PREFIXES.DOCUMENTS.startswith("/")
        assert not PREFIXES.IMAGES.startswith("/")
        assert not PREFIXES.UPLOADS.startswith("/")
        assert not PREFIXES.RESULTS.startswith("/")

    def test_prefixes_have_no_trailing_slash(self) -> None:
        """Prefixes should not end with a slash."""
        assert not PREFIXES.DOCUMENTS.endswith("/")
        assert not PREFIXES.IMAGES.endswith("/")
        assert not PREFIXES.UPLOADS.endswith("/")
        assert not PREFIXES.RESULTS.endswith("/")

    def test_frozen_dataclass(self) -> None:
        """StoragePrefixes should be immutable."""
        with pytest.raises(Exception):  # FrozenInstanceError
            PREFIXES.DOCUMENTS = "new_value"  # type: ignore


class TestDocumentPath:
    """Tests for document_path helper."""

    def test_document_path_with_extension(self) -> None:
        """Should generate correct document path with extension."""
        path = PREFIXES.document_path("abc123", ".pdf")
        assert path == "documents/abc123.pdf"

    def test_document_path_without_leading_dot(self) -> None:
        """Should handle extension without leading dot."""
        path = PREFIXES.document_path("abc123", "pdf")
        assert path == "documents/abc123.pdf"

    def test_document_path_default_extension(self) -> None:
        """Should use .pdf as default extension."""
        path = PREFIXES.document_path("abc123")
        assert path == "documents/abc123.pdf"


class TestImagePath:
    """Tests for image_path helper."""

    def test_image_path_basic(self) -> None:
        """Should generate correct image path."""
        path = PREFIXES.image_path("doc123", 1)
        assert path == "images/doc123/page_1.png"

    def test_image_path_page_number(self) -> None:
        """Should include page number in path."""
        path = PREFIXES.image_path("doc123", 5)
        assert path == "images/doc123/page_5.png"

    def test_image_path_custom_extension(self) -> None:
        """Should support custom extension."""
        path = PREFIXES.image_path("doc123", 1, ".jpg")
        assert path == "images/doc123/page_1.jpg"


class TestUploadPath:
    """Tests for upload_path helper."""

    def test_upload_path_basic(self) -> None:
        """Should generate correct upload path."""
        path = PREFIXES.upload_path("invoice.pdf")
        assert path == "uploads/invoice.pdf"

    def test_upload_path_with_subfolder(self) -> None:
        """Should include subfolder when provided."""
        path = PREFIXES.upload_path("invoice.pdf", "async")
        assert path == "uploads/async/invoice.pdf"


class TestResultPath:
    """Tests for result_path helper."""

    def test_result_path_basic(self) -> None:
        """Should generate correct result path."""
        path = PREFIXES.result_path("output.json")
        assert path == "results/output.json"


class TestExportPath:
    """Tests for export_path helper."""

    def test_export_path_basic(self) -> None:
        """Should generate correct export path."""
        path = PREFIXES.export_path("exp123", "dataset.zip")
        assert path == "exports/exp123/dataset.zip"


class TestDatasetPath:
    """Tests for dataset_path helper."""

    def test_dataset_path_basic(self) -> None:
        """Should generate correct dataset path."""
        path = PREFIXES.dataset_path("ds123", "data.yaml")
        assert path == "datasets/ds123/data.yaml"


class TestModelPath:
    """Tests for model_path helper."""

    def test_model_path_basic(self) -> None:
        """Should generate correct model path."""
        path = PREFIXES.model_path("v1.0.0", "best.pt")
        assert path == "models/v1.0.0/best.pt"


class TestExportsFromInit:
    """Tests for exports from storage __init__.py."""

    def test_prefixes_exported(self) -> None:
        """PREFIXES should be exported from storage module."""
        from shared.storage import PREFIXES as exported_prefixes

        assert exported_prefixes is PREFIXES

    def test_storage_prefixes_exported(self) -> None:
        """StoragePrefixes should be exported from storage module."""
        from shared.storage import StoragePrefixes as exported_class

        assert exported_class is StoragePrefixes
