################################################################################
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
# limitations under the License.
################################################################################

"""Daft version compatibility checks."""

from __future__ import annotations

from importlib.metadata import version

_daft_version: tuple[int, ...] | None = None


def _parse_daft_version() -> tuple[int, ...]:
    global _daft_version
    if _daft_version is not None:
        return _daft_version
    raw = version("daft")
    parts = []
    for seg in raw.split(".")[:3]:
        digits = ""
        for c in seg:
            if c.isdigit():
                digits += c
            else:
                break
        if digits:
            parts.append(int(digits))
    _daft_version = tuple(parts)
    return _daft_version


def has_file_range_reads() -> bool:
    """True if the installed Daft supports File range metadata (>= 0.7.11)."""
    return _parse_daft_version() >= (0, 7, 11)


def file_range_position_field() -> str:
    """Return Daft File's physical position field name."""
    return "position" if _parse_daft_version() >= (0, 7, 15) else "offset"


def file_range_size_field() -> str:
    """Return Daft File's physical size field name."""
    return "size" if _parse_daft_version() >= (0, 7, 15) else "length"


def require_file_range_reads(feature: str = "BLOB columns") -> None:
    """Raise if Daft is too old for File range metadata support."""
    if not has_file_range_reads():
        v = ".".join(str(x) for x in _parse_daft_version())
        raise NotImplementedError(
            f"{feature} require daft >= 0.7.11 (File range metadata support), "
            f"but found {v}. "
            f"Please upgrade: pip install 'daft>=0.7.11'"
        )
