#!/usr/bin/env python3
# coding: utf-8
# Copyright (c) 2026 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------

"""Tiling class utilities for PyPTO Language DSL."""

from __future__ import annotations

__all__ = [
    "is_tiling_class",
    "get_tiling_fields",
    "get_tiling_tuple_type",
    "ScalarFieldInfo",
    "ArrayFieldInfo",
    "FieldInfo",
    "get_tiling_ctype_struct",
    "tiling_instance_to_bytes",
]


import ast
import ctypes
from dataclasses import dataclass, is_dataclass
import inspect
import textwrap
from typing import Union

from pypto.pypto_impl import ir
from pypto.pypto_impl.ir import DataType
from pypto_pro.language.parser.diagnostics import check_in_range

from ..._errors import InvalidArgument, InvalidOperation, InvalidType, message_of, source_lines_of

# Largest fixed-size array a tiling class field may declare.
_MAX_ARRAY_SIZE = 2048

_PYTHON_TYPE_TO_DTYPE: dict[type, DataType] = {
    int: DataType.INT64,
    float: DataType.FP32,
    bool: DataType.BOOL,
}

_PYTHON_TYPE_NAME_TO_TYPE: dict[str, type] = {type_.__name__: type_ for type_ in _PYTHON_TYPE_TO_DTYPE}


@dataclass(frozen=True)
class ScalarFieldInfo:
    """Field info for a scalar tiling field (int, float, or bool)."""

    dtype: DataType


@dataclass(frozen=True)
class ArrayFieldInfo:
    """Field info for a fixed-length array tiling field (T[N])."""

    dtype: DataType
    size: int


FieldInfo = Union[ScalarFieldInfo, ArrayFieldInfo]


def _parse_field_annotation(annotation: object) -> FieldInfo:
    """Normalize a scalar or fixed-array tiling annotation without evaluating it."""
    if isinstance(annotation, type) and annotation in _PYTHON_TYPE_TO_DTYPE:
        return ScalarFieldInfo(dtype=_PYTHON_TYPE_TO_DTYPE[annotation])
    if not isinstance(annotation, str):
        raise InvalidType("expected int, float, bool, or T[N]")

    try:
        expression = ast.parse(annotation, mode="eval").body
    except SyntaxError as exc:
        raise InvalidArgument(f"invalid annotation syntax {annotation!r}") from exc

    if isinstance(expression, ast.Name):
        dtype = _PYTHON_TYPE_NAME_TO_TYPE.get(expression.id)
        if dtype is None:
            raise InvalidType(f"unsupported scalar type {expression.id!r}")
        return ScalarFieldInfo(dtype=_PYTHON_TYPE_TO_DTYPE[dtype])

    if not isinstance(expression, ast.Subscript) or not isinstance(expression.value, ast.Name):
        raise InvalidType("expected a scalar type or fixed-array annotation T[N]")
    dtype = _PYTHON_TYPE_NAME_TO_TYPE.get(expression.value.id)
    if dtype is None:
        raise InvalidType(f"array element type must be int, float, or bool, got {expression.value.id!r}")
    size_node = expression.slice
    if not isinstance(size_node, ast.Constant):
        raise InvalidType("array size must be a positive integer literal")
    if not isinstance(size_node.value, int) or isinstance(size_node.value, bool) or size_node.value <= 0:
        raise InvalidType("array size must be a positive integer literal")
    size = size_node.value
    check_in_range(size, 1, _MAX_ARRAY_SIZE, subject="array size")
    return ArrayFieldInfo(dtype=_PYTHON_TYPE_TO_DTYPE[dtype], size=size)


def is_tiling_class(cls: object) -> bool:
    """Return True if cls is a user-defined tiling class.

    A tiling class is a dataclass with at least one field,
    all annotated as int, float, bool, or T[N].

    Args:
        cls: Object to check

    Returns:
        True if cls is a valid tiling class
    """
    if not isinstance(cls, type):
        return False
    if not is_dataclass(cls):
        return False
    annotations = getattr(cls, "__annotations__", {})
    if not annotations:
        return False

    _check_duplicate_annotations(cls)

    try:
        for annotation in annotations.values():
            _parse_field_annotation(annotation)
    except (TypeError, ValueError):
        return False
    return True


def _span_of_class_node(node: ast.AST, source_file: str, source_lines_raw: list[str], starting_line: int):
    """Span of *node*, parsed out of a dedented class body, back in file coordinates.

    ``ast.parse`` needs code at column zero, so the class source was dedented and
    re-numbered from one; both offsets have to go back on for the span to name a
    place in the file. None when the tracker cannot be built.
    """
    try:
        from pypto_pro.language.parser._span_tracker import SpanTracker
        from pypto_pro.runtime.kernel import _calculate_col_offset

        tracker = SpanTracker(
            source_file, source_lines_raw, starting_line - 1, _calculate_col_offset(source_lines_raw)
        )
        return tracker.get_span(node)
    except Exception:  # noqa: BLE001 - a location is a nicety; never mask the real error
        return None


def _check_duplicate_annotations(cls: type) -> None:
    """Parse class source AST to detect duplicate annotated field names.

    Python deduplicates __annotations__ at class creation, so runtime
    introspection cannot see duplicates. Use AST parsing as a best-effort check.
    """
    try:
        source_lines_raw, starting_line = inspect.getsourcelines(cls)
        source_file = inspect.getfile(cls)
    except (TypeError, OSError):
        return
    src = textwrap.dedent("".join(source_lines_raw))
    tree = ast.parse(src)
    for node in ast.walk(tree):
        if isinstance(node, ast.ClassDef) and node.name == cls.__name__:
            declared: dict[str, ast.AnnAssign] = {}
            for stmt in node.body:
                if not (isinstance(stmt, ast.AnnAssign) and isinstance(stmt.target, ast.Name)):
                    continue
                name = stmt.target.id
                if name in declared:
                    # The class body is already parsed here, so the repeated field is
                    # a node in hand: point at it rather than leaving the reader to
                    # find it. The dedent above shifted the columns and the source
                    # starts at the class, so both offsets go back on.
                    raise InvalidOperation(
                        f"Tiling class '{cls.__name__}' has duplicate field '{name}'. "
                        f"All field names must be unique.",
                        span=_span_of_class_node(stmt, source_file, source_lines_raw, starting_line),
                        source_lines=source_lines_of(source_file),
                        previous_span=_span_of_class_node(
                            declared[name], source_file, source_lines_raw, starting_line
                        ),
                    )
                declared[name] = stmt
            return


def get_tiling_fields(cls: type) -> dict[str, FieldInfo]:
    """Return ordered {field_name: FieldInfo} for a validated tiling class.

    Args:
        cls: A tiling class (validated by is_tiling_class)

    Returns:
        Ordered dict mapping field names to their FieldInfo (ScalarFieldInfo or ArrayFieldInfo)

    Raises:
        ValueError: If cls is not a valid tiling class
    """
    if not isinstance(cls, type) or not is_dataclass(cls):
        raise InvalidArgument(f"Not a valid tiling class: {cls!r}. Expected a dataclass.")
    annotations = getattr(cls, "__annotations__", {})
    if not annotations:
        raise InvalidArgument(f"Not a valid tiling class: {cls!r}. At least one field is required.")
    result: dict[str, FieldInfo] = {}
    for name, annotation in annotations.items():
        try:
            result[name] = _parse_field_annotation(annotation)
        except (TypeError, ValueError) as exc:
            raise InvalidArgument(
                f"Invalid tiling field {name!r} annotation {annotation!r}: {message_of(exc)}"
            ) from exc
    return result


def get_tiling_tuple_type(cls: type) -> ir.TupleType:
    """Lower the fields of a tiling class to the IR tuple type used by kernel parameters."""
    elem_types: list[ir.Type] = []
    for field_info in get_tiling_fields(cls).values():
        scalar_type = ir.ScalarType(field_info.dtype)
        if isinstance(field_info, ArrayFieldInfo):
            elem_types.append(ir.TupleType([scalar_type] * field_info.size))
        else:
            elem_types.append(scalar_type)
    return ir.TupleType(elem_types)


def _field_ctype(dtype: DataType) -> type:
    # Reuse the single DataType→ctype map maintained in the runtime (lazy import to avoid a
    # module-level cycle; jit imports this module inside functions). Must stay in sync with
    # the C struct member types emitted by CCE codegen's CppTypeForField (e.g. INT64 → int64_t).
    from pypto_pro.runtime.jit import _PL_DTYPE_TO_CTYPE

    ctype = _PL_DTYPE_TO_CTYPE.get(str(dtype))
    if ctype is None:
        raise InvalidType(f"No ctypes mapping for tiling field dtype {dtype}")
    return ctype


# Cache the generated ctypes.Structure subclass per tiling class.
_CTYPE_STRUCT_CACHE: dict[type, type] = {}


def get_tiling_ctype_struct(cls: type) -> type:
    """Return a ctypes.Structure subclass whose layout matches the C ``struct Tiling``.

    Scalar field → one ctypes member; T[N] → a length-N ctypes array member.
    The native ctypes alignment rules match the C struct emitted by codegen, so the
    byte layout (sizeof + field offsets) is identical on both sides.
    """
    cached = _CTYPE_STRUCT_CACHE.get(cls)
    if cached is not None:
        return cached
    fields: list[tuple[str, type]] = []
    for name, info in get_tiling_fields(cls).items():
        base = _field_ctype(info.dtype)
        if isinstance(info, ArrayFieldInfo):
            fields.append((name, base * info.size))
        else:
            fields.append((name, base))
    struct_cls = type(f"{cls.__name__}_CStruct", (ctypes.Structure,), {"_fields_": fields})
    _CTYPE_STRUCT_CACHE[cls] = struct_cls
    return struct_cls


def tiling_instance_to_bytes(instance: object) -> bytes:
    """Serialize a tiling-class instance to its C-struct byte layout."""
    cls = type(instance)
    struct_cls = get_tiling_ctype_struct(cls)
    values: list = []
    for name, info in get_tiling_fields(cls).items():
        val = getattr(instance, name)
        if isinstance(info, ArrayFieldInfo):
            if (
                isinstance(val, (str, bytes, bytearray))
                or not hasattr(val, "__getitem__")
                or not hasattr(val, "__len__")
            ):
                raise InvalidType(
                    f"Tiling field '{name}' is T[{info.size}]: expected an indexable "
                    f"sequence, got {type(val).__name__!r}"
                )
            if len(val) != info.size:
                raise InvalidType(f"Tiling field '{name}' expected {info.size} elements, got {len(val)}")
            base = _field_ctype(info.dtype)
            values.append((base * info.size)(*[val[i] for i in range(info.size)]))
        else:
            values.append(val)
    return bytes(struct_cls(*values))