# -*- coding: utf-8 -*-
"""
LVT Shared Infrastructure âEUR" Qt5/Qt6 Compatibility Shim.

QGIS 4.x uses Qt6 (PyQt6) while QGIS 3.x uses Qt5 (PyQt5).
Qt6 made enums strictly namespace-aware:
    Qt5: Qt.AlignmentFlag.AlignCenter
    Qt6: Qt.AlignmentFlag.AlignCenter

This shim provides unified access to all Qt enum values used across
the LVT plugin suite, automatically selecting the correct form based
on the Qt version at runtime.

Usage:
    from .shared.qt_compat import QtCompat
    label.setAlignment(QtCompat.AlignCenter)

Author: Lá»(TM)c VÅ© Trung (LVT)
License: GPL-3.0
"""

from qgis.PyQt.QtCore import Qt, QT_VERSION_STR

_QT_MAJOR = int(QT_VERSION_STR.split('.')[0])
_IS_QT6 = _QT_MAJOR >= 6


class QtCompat:
    """Unified Qt enum constants for Qt5 and Qt6 compatibility."""

    # ------- Alignment -------
    if _IS_QT6:
        AlignLeft = Qt.AlignmentFlag.AlignLeft
        AlignRight = Qt.AlignmentFlag.AlignRight
        AlignCenter = Qt.AlignmentFlag.AlignCenter
        AlignHCenter = Qt.AlignmentFlag.AlignHCenter
        AlignVCenter = Qt.AlignmentFlag.AlignVCenter
        AlignTop = Qt.AlignmentFlag.AlignTop
        AlignBottom = Qt.AlignmentFlag.AlignBottom
        AlignJustify = Qt.AlignmentFlag.AlignJustify
    else:
        AlignLeft = Qt.AlignmentFlag.AlignLeft
        AlignRight = Qt.AlignmentFlag.AlignRight
        AlignCenter = Qt.AlignmentFlag.AlignCenter
        AlignHCenter = Qt.AlignmentFlag.AlignHCenter
        AlignVCenter = Qt.AlignmentFlag.AlignVCenter
        AlignTop = Qt.AlignmentFlag.AlignTop
        AlignBottom = Qt.AlignmentFlag.AlignBottom
        AlignJustify = Qt.AlignmentFlag.AlignJustify

    # ------- Orientation -------
    if _IS_QT6:
        Horizontal = Qt.Orientation.Horizontal
        Vertical = Qt.Orientation.Vertical
    else:
        Horizontal = Qt.Orientation.Horizontal
        Vertical = Qt.Orientation.Vertical

    # ------- CheckState -------
    if _IS_QT6:
        Checked = Qt.CheckState.Checked
        Unchecked = Qt.CheckState.Unchecked
        PartiallyChecked = Qt.CheckState.PartiallyChecked
    else:
        Checked = Qt.CheckState.Checked
        Unchecked = Qt.CheckState.Unchecked
        PartiallyChecked = Qt.CheckState.PartiallyChecked

    # ------- ItemFlag -------
    if _IS_QT6:
        ItemIsEnabled = Qt.ItemFlag.ItemIsEnabled
        ItemIsSelectable = Qt.ItemFlag.ItemIsSelectable
        ItemIsEditable = Qt.ItemFlag.ItemIsEditable
        ItemIsUserCheckable = Qt.ItemFlag.ItemIsUserCheckable
        ItemIsAutoTristate = Qt.ItemFlag.ItemIsAutoTristate
        NoItemFlags = Qt.ItemFlag(0)
    else:
        ItemIsEnabled = Qt.ItemFlag.ItemIsEnabled
        ItemIsSelectable = Qt.ItemFlag.ItemIsSelectable
        ItemIsEditable = Qt.ItemFlag.ItemIsEditable
        ItemIsUserCheckable = Qt.ItemFlag.ItemIsUserCheckable
        ItemIsAutoTristate = Qt.ItemFlag.ItemIsAutoTristate
        NoItemFlags = Qt.ItemFlag(0)

    # ------- ItemDataRole -------
    if _IS_QT6:
        UserRole = Qt.ItemDataRole.UserRole
        DisplayRole = Qt.ItemDataRole.DisplayRole
        DecorationRole = Qt.ItemDataRole.DecorationRole
        ToolTipRole = Qt.ItemDataRole.ToolTipRole
        CheckStateRole = Qt.ItemDataRole.CheckStateRole
    else:
        UserRole = Qt.ItemDataRole.UserRole
        DisplayRole = Qt.ItemDataRole.DisplayRole
        DecorationRole = Qt.ItemDataRole.DecorationRole
        ToolTipRole = Qt.ItemDataRole.ToolTipRole
        CheckStateRole = Qt.ItemDataRole.CheckStateRole

    # ------- AspectRatioMode -------
    if _IS_QT6:
        KeepAspectRatio = Qt.AspectRatioMode.KeepAspectRatio
        IgnoreAspectRatio = Qt.AspectRatioMode.IgnoreAspectRatio
        KeepAspectRatioByExpanding = Qt.AspectRatioMode.KeepAspectRatioByExpanding
    else:
        KeepAspectRatio = Qt.AspectRatioMode.KeepAspectRatio
        IgnoreAspectRatio = Qt.AspectRatioMode.IgnoreAspectRatio
        KeepAspectRatioByExpanding = Qt.AspectRatioMode.KeepAspectRatioByExpanding

    # ------- TransformationMode -------
    if _IS_QT6:
        SmoothTransformation = Qt.TransformationMode.SmoothTransformation
        FastTransformation = Qt.TransformationMode.FastTransformation
    else:
        SmoothTransformation = Qt.TransformationMode.SmoothTransformation
        FastTransformation = Qt.TransformationMode.FastTransformation

    # ------- ScrollBarPolicy -------
    if _IS_QT6:
        ScrollBarAlwaysOff = Qt.ScrollBarPolicy.ScrollBarAlwaysOff
        ScrollBarAlwaysOn = Qt.ScrollBarPolicy.ScrollBarAlwaysOn
        ScrollBarAsNeeded = Qt.ScrollBarPolicy.ScrollBarAsNeeded
    else:
        ScrollBarAlwaysOff = Qt.ScrollBarPolicy.ScrollBarAlwaysOff
        ScrollBarAlwaysOn = Qt.ScrollBarPolicy.ScrollBarAlwaysOn
        ScrollBarAsNeeded = Qt.ScrollBarPolicy.ScrollBarAsNeeded

    # ------- TextInteractionFlag -------
    if _IS_QT6:
        TextBrowserInteraction = Qt.TextInteractionFlag.TextBrowserInteraction
        TextSelectableByMouse = Qt.TextInteractionFlag.TextSelectableByMouse
        LinksAccessibleByMouse = Qt.TextInteractionFlag.LinksAccessibleByMouse
        TextEditorInteraction = Qt.TextInteractionFlag.TextEditorInteraction
    else:
        TextBrowserInteraction = Qt.TextInteractionFlag.TextBrowserInteraction
        TextSelectableByMouse = Qt.TextInteractionFlag.TextSelectableByMouse
        LinksAccessibleByMouse = Qt.TextInteractionFlag.LinksAccessibleByMouse
        TextEditorInteraction = Qt.TextInteractionFlag.TextEditorInteraction

    # ------- WindowModality -------
    if _IS_QT6:
        NonModal = Qt.WindowModality.NonModal
        ApplicationModal = Qt.WindowModality.ApplicationModal
        WindowModal = Qt.WindowModality.WindowModal
    else:
        NonModal = Qt.WindowModality.NonModal
        ApplicationModal = Qt.WindowModality.ApplicationModal
        WindowModal = Qt.WindowModality.WindowModal

    # ------- TextElideMode -------
    if _IS_QT6:
        ElideRight = Qt.TextElideMode.ElideRight
        ElideLeft = Qt.TextElideMode.ElideLeft
        ElideMiddle = Qt.TextElideMode.ElideMiddle
        ElideNone = Qt.TextElideMode.ElideNone
    else:
        ElideRight = Qt.TextElideMode.ElideRight
        ElideLeft = Qt.TextElideMode.ElideLeft
        ElideMiddle = Qt.TextElideMode.ElideMiddle
        ElideNone = Qt.TextElideMode.ElideNone

    # ------- CursorShape -------
    if _IS_QT6:
        ArrowCursor = Qt.CursorShape.ArrowCursor
        WaitCursor = Qt.CursorShape.WaitCursor
        PointingHandCursor = Qt.CursorShape.PointingHandCursor
        CrossCursor = Qt.CursorShape.CrossCursor
        SizeAllCursor = Qt.CursorShape.SizeAllCursor
        SplitHCursor = Qt.CursorShape.SplitHCursor
        SplitVCursor = Qt.CursorShape.SplitVCursor
    else:
        ArrowCursor = Qt.CursorShape.ArrowCursor
        WaitCursor = Qt.CursorShape.WaitCursor
        PointingHandCursor = Qt.CursorShape.PointingHandCursor
        CrossCursor = Qt.CursorShape.CrossCursor
        SizeAllCursor = Qt.CursorShape.SizeAllCursor
        SplitHCursor = Qt.CursorShape.SplitHCursor
        SplitVCursor = Qt.CursorShape.SplitVCursor

    # ------- ToolButtonStyle -------
    if _IS_QT6:
        ToolButtonTextUnderIcon = Qt.ToolButtonStyle.ToolButtonTextUnderIcon
        ToolButtonTextBesideIcon = Qt.ToolButtonStyle.ToolButtonTextBesideIcon
        ToolButtonIconOnly = Qt.ToolButtonStyle.ToolButtonIconOnly
    else:
        ToolButtonTextUnderIcon = Qt.ToolButtonStyle.ToolButtonTextUnderIcon
        ToolButtonTextBesideIcon = Qt.ToolButtonStyle.ToolButtonTextBesideIcon
        ToolButtonIconOnly = Qt.ToolButtonStyle.ToolButtonIconOnly

    # ------- DockWidgetArea -------
    if _IS_QT6:
        LeftDockWidgetArea = Qt.DockWidgetArea.LeftDockWidgetArea
        RightDockWidgetArea = Qt.DockWidgetArea.RightDockWidgetArea
        TopDockWidgetArea = Qt.DockWidgetArea.TopDockWidgetArea
        BottomDockWidgetArea = Qt.DockWidgetArea.BottomDockWidgetArea
        AllDockWidgetAreas = Qt.DockWidgetArea.AllDockWidgetAreas
    else:
        LeftDockWidgetArea = Qt.DockWidgetArea.LeftDockWidgetArea
        RightDockWidgetArea = Qt.DockWidgetArea.RightDockWidgetArea
        TopDockWidgetArea = Qt.DockWidgetArea.TopDockWidgetArea
        BottomDockWidgetArea = Qt.DockWidgetArea.BottomDockWidgetArea
        AllDockWidgetAreas = Qt.DockWidgetArea.AllDockWidgetAreas

    # ------- Key -------
    if _IS_QT6:
        Key_Escape = Qt.Key.Key_Escape
        Key_Return = Qt.Key.Key_Return
        Key_Enter = Qt.Key.Key_Enter
        Key_Delete = Qt.Key.Key_Delete
        Key_Backspace = Qt.Key.Key_Backspace
    else:
        Key_Escape = Qt.Key.Key_Escape
        Key_Return = Qt.Key.Key_Return
        Key_Enter = Qt.Key.Key_Enter
        Key_Delete = Qt.Key.Key_Delete
        Key_Backspace = Qt.Key.Key_Backspace

    # ------- PenStyle -------
    if _IS_QT6:
        SolidLine = Qt.PenStyle.SolidLine
        DashLine = Qt.PenStyle.DashLine
        DotLine = Qt.PenStyle.DotLine
        DashDotLine = Qt.PenStyle.DashDotLine
        DashDotDotLine = Qt.PenStyle.DashDotDotLine
        NoPen = Qt.PenStyle.NoPen
    else:
        SolidLine = Qt.PenStyle.SolidLine
        DashLine = Qt.PenStyle.DashLine
        DotLine = Qt.PenStyle.DotLine
        DashDotLine = Qt.PenStyle.DashDotLine
        DashDotDotLine = Qt.PenStyle.DashDotDotLine
        NoPen = Qt.PenStyle.NoPen

    # ------- MouseButton -------
    if _IS_QT6:
        LeftButton = Qt.MouseButton.LeftButton
        RightButton = Qt.MouseButton.RightButton
        MiddleButton = Qt.MouseButton.MiddleButton
        NoButton = Qt.MouseButton.NoButton
    else:
        LeftButton = Qt.MouseButton.LeftButton
        RightButton = Qt.MouseButton.RightButton
        MiddleButton = Qt.MouseButton.MiddleButton
        NoButton = Qt.MouseButton.NoButton

    # ------- TextFormat -------
    if _IS_QT6:
        RichText = Qt.TextFormat.RichText
        PlainText = Qt.TextFormat.PlainText
        AutoText = Qt.TextFormat.AutoText
    else:
        RichText = Qt.TextFormat.RichText
        PlainText = Qt.TextFormat.PlainText
        AutoText = Qt.TextFormat.AutoText

    # ------- WindowType / WindowFlags -------
    if _IS_QT6:
        Widget = Qt.WindowType.Widget
        Window = Qt.WindowType.Window
        Dialog = Qt.WindowType.Dialog
        Popup = Qt.WindowType.Popup
        Tool = Qt.WindowType.Tool
        WindowContextHelpButtonHint = Qt.WindowType.WindowContextHelpButtonHint
        WindowStaysOnTopHint = Qt.WindowType.WindowStaysOnTopHint
        FramelessWindowHint = Qt.WindowType.FramelessWindowHint
    else:
        Widget = Qt.WindowType.Widget
        Window = Qt.WindowType.Window
        Dialog = Qt.WindowType.Dialog
        Popup = Qt.WindowType.Popup
        Tool = Qt.WindowType.Tool
        WindowContextHelpButtonHint = Qt.WindowType.WindowContextHelpButtonHint
        WindowStaysOnTopHint = Qt.WindowType.WindowStaysOnTopHint
        FramelessWindowHint = Qt.WindowType.FramelessWindowHint

    # ------- GlobalColor / BrushStyle -------
    if _IS_QT6:
        transparent = Qt.GlobalColor.transparent
        white = Qt.GlobalColor.white
        black = Qt.GlobalColor.black
        red = Qt.GlobalColor.red
        green = Qt.GlobalColor.green
        blue = Qt.GlobalColor.blue
        yellow = Qt.GlobalColor.yellow
        gray = Qt.GlobalColor.gray
        darkGray = Qt.GlobalColor.darkGray
        lightGray = Qt.GlobalColor.lightGray
        NoBrush = Qt.BrushStyle.NoBrush
        SolidPattern = Qt.BrushStyle.SolidPattern
    else:
        transparent = Qt.GlobalColor.transparent
        white = Qt.GlobalColor.white
        black = Qt.GlobalColor.black
        red = Qt.GlobalColor.red
        green = Qt.GlobalColor.green
        blue = Qt.GlobalColor.blue
        yellow = Qt.GlobalColor.yellow
        gray = Qt.GlobalColor.gray
        darkGray = Qt.GlobalColor.darkGray
        lightGray = Qt.GlobalColor.lightGray
        NoBrush = Qt.BrushStyle.NoBrush
        SolidPattern = Qt.BrushStyle.SolidPattern

    # ------- SortOrder -------
    if _IS_QT6:
        AscendingOrder = Qt.SortOrder.AscendingOrder
        DescendingOrder = Qt.SortOrder.DescendingOrder
    else:
        AscendingOrder = Qt.SortOrder.AscendingOrder
        DescendingOrder = Qt.SortOrder.DescendingOrder

    # ------- MatchFlag -------
    if _IS_QT6:
        MatchExactly = Qt.MatchFlag.MatchExactly
        MatchContains = Qt.MatchFlag.MatchContains
        MatchStartsWith = Qt.MatchFlag.MatchStartsWith
        MatchEndsWith = Qt.MatchFlag.MatchEndsWith
        MatchFixedString = Qt.MatchFlag.MatchFixedString
        MatchCaseSensitive = Qt.MatchFlag.MatchCaseSensitive
        MatchRegularExpression = Qt.MatchFlag.MatchRegularExpression
    else:
        MatchExactly = Qt.MatchFlag.MatchExactly
        MatchContains = Qt.MatchFlag.MatchContains
        MatchStartsWith = Qt.MatchFlag.MatchStartsWith
        MatchEndsWith = Qt.MatchFlag.MatchEndsWith
        MatchFixedString = Qt.MatchFlag.MatchFixedString
        MatchCaseSensitive = Qt.MatchFlag.MatchCaseSensitive
        MatchRegularExpression = Qt.MatchRegExp


# ---- QMessageBox StandardButton compat ----
try:
    from qgis.PyQt.QtWidgets import QMessageBox as _QMB

    class MsgButton:
        """QMessageBox standard button constants for Qt5/Qt6."""
        if _IS_QT6:
            Ok = _QMB.StandardButton.Ok
            Cancel = _QMB.StandardButton.Cancel
            Yes = _QMB.StandardButton.Yes
            No = _QMB.StandardButton.No
            Save = _QMB.StandardButton.Save
            Discard = _QMB.StandardButton.Discard
            Apply = _QMB.StandardButton.Apply
            Close = _QMB.StandardButton.Close
            YesAll = _QMB.StandardButton.YesToAll
            NoAll = _QMB.StandardButton.NoToAll
        else:
            Ok = _QMB.Ok
            Cancel = _QMB.Cancel
            Yes = _QMB.Yes
            No = _QMB.No
            Save = _QMB.Save
            Discard = _QMB.Discard
            Apply = _QMB.Apply
            Close = _QMB.Close
            YesAll = _QMB.YesToAll
            NoAll = _QMB.NoToAll

except Exception:
    class MsgButton:
        Ok = 0x00000400
        Cancel = 0x00400000
        Yes = 0x00004000
        No = 0x00010000


# ---- QgsMapLayer LayerType compat ----
try:
    from qgis.core import Qgis
    # QGIS 3.30+ and 4.x

    class LayerType:
        """QgsMapLayer type constants for QGIS 3.x and 4.x."""
        try:
            Vector = Qgis.LayerType.Vector
            Raster = Qgis.LayerType.Raster
            Plugin = Qgis.LayerType.Plugin
            Mesh = Qgis.LayerType.Mesh
            VectorTile = Qgis.LayerType.VectorTile
            Annotation = Qgis.LayerType.Annotation
            PointCloud = Qgis.LayerType.PointCloud
            Group = Qgis.LayerType.Group
            TiledScene = Qgis.LayerType.TiledScene
        except AttributeError:
            # QGIS < 3.28: use QgsMapLayer legacy
            from qgis.core import QgsMapLayer
            Vector = QgsMapLayer.LayerType.VectorLayer
            Raster = QgsMapLayer.LayerType.RasterLayer
            Plugin = QgsMapLayer.LayerType.PluginLayer
            Mesh = QgsMapLayer.LayerType.MeshLayer
except Exception:
    class LayerType:
        Vector = 0
        Raster = 1
        Plugin = 2
        Mesh = 3


# ---- QHeaderView ResizeMode compat (65 hits in dialogs) ----
try:
    from qgis.PyQt.QtWidgets import QHeaderView as _QHV

    class HeaderResize:
        """QHeaderView resize mode constants for Qt5/Qt6."""
        if _IS_QT6:
            Stretch         = _QHV.ResizeMode.Stretch
            ResizeToContents = _QHV.ResizeMode.ResizeToContents
            Fixed           = _QHV.ResizeMode.Fixed
            Interactive     = _QHV.ResizeMode.Interactive
        else:
            Stretch         = _QHV.Stretch
            ResizeToContents = _QHV.ResizeToContents
            Fixed           = _QHV.Fixed
            Interactive     = _QHV.Interactive

except Exception:
    class HeaderResize:
        Stretch = 1
        ResizeToContents = 3
        Fixed = 2
        Interactive = 0


# ---- QSizePolicy Policy compat (8 hits) ----
try:
    from qgis.PyQt.QtWidgets import QSizePolicy as _QSP

    class SizePolicy:
        """QSizePolicy policy constants for Qt5/Qt6."""
        if _IS_QT6:
            Fixed     = _QSP.Policy.Fixed
            Minimum   = _QSP.Policy.Minimum
            Maximum   = _QSP.Policy.Maximum
            Preferred = _QSP.Policy.Preferred
            Expanding = _QSP.Policy.Expanding
            MinimumExpanding = _QSP.Policy.MinimumExpanding
            Ignored   = _QSP.Policy.Ignored
        else:
            Fixed     = _QSP.Fixed
            Minimum   = _QSP.Minimum
            Maximum   = _QSP.Maximum
            Preferred = _QSP.Preferred
            Expanding = _QSP.Expanding
            MinimumExpanding = _QSP.MinimumExpanding
            Ignored   = _QSP.Ignored

except Exception:
    class SizePolicy:
        Fixed = 0
        Minimum = 1
        Maximum = 4
        Preferred = 5
        Expanding = 7
        MinimumExpanding = 3
        Ignored = 13


# ---- QFrame Shape/Shadow compat (6 hits) ----
try:
    from qgis.PyQt.QtWidgets import QFrame as _QFR

    class FrameShape:
        """QFrame shape/shadow constants for Qt5/Qt6."""
        if _IS_QT6:
            NoFrame     = _QFR.Shape.NoFrame
            Box         = _QFR.Shape.Box
            Panel       = _QFR.Shape.Panel
            StyledPanel = _QFR.Shape.StyledPanel
            HLine       = _QFR.Shape.HLine
            VLine       = _QFR.Shape.VLine
        else:
            NoFrame     = _QFR.NoFrame
            Box         = _QFR.Box
            Panel       = _QFR.Panel
            StyledPanel = _QFR.StyledPanel
            HLine       = _QFR.HLine
            VLine       = _QFR.VLine

    class FrameShadow:
        """QFrame shadow constants for Qt5/Qt6."""
        if _IS_QT6:
            Plain   = _QFR.Shadow.Plain
            Raised  = _QFR.Shadow.Raised
            Sunken  = _QFR.Shadow.Sunken
        else:
            Plain   = _QFR.Plain
            Raised  = _QFR.Raised
            Sunken  = _QFR.Sunken

except Exception:
    class FrameShape:
        NoFrame = 0; Box = 1; Panel = 2; StyledPanel = 6; HLine = 4; VLine = 5

    class FrameShadow:
        Plain = 16; Raised = 32; Sunken = 48


# ---- QLineEdit EchoMode compat (5 hits) ----
try:
    from qgis.PyQt.QtWidgets import QLineEdit as _QLE

    class EchoMode:
        """QLineEdit echo mode constants for Qt5/Qt6."""
        if _IS_QT6:
            Normal          = _QLE.EchoMode.Normal
            Password        = _QLE.EchoMode.Password
            NoEcho          = _QLE.EchoMode.NoEcho
            PasswordEchoOnEdit = _QLE.EchoMode.PasswordEchoOnEdit
        else:
            Normal          = _QLE.Normal
            Password        = _QLE.Password
            NoEcho          = _QLE.NoEcho
            PasswordEchoOnEdit = _QLE.PasswordEchoOnEdit

except Exception:
    class EchoMode:
        Normal = 0; Password = 2; NoEcho = 1; PasswordEchoOnEdit = 3


# ---- QAbstractItemView SelectionMode compat (5 hits) ----
try:
    from qgis.PyQt.QtWidgets import QAbstractItemView as _QAIV

    class SelectionMode:
        """QAbstractItemView selection mode constants for Qt5/Qt6."""
        if _IS_QT6:
            NoSelection       = _QAIV.SelectionMode.NoSelection
            SingleSelection   = _QAIV.SelectionMode.SingleSelection
            MultiSelection    = _QAIV.SelectionMode.MultiSelection
            ExtendedSelection = _QAIV.SelectionMode.ExtendedSelection
            ContiguousSelection = _QAIV.SelectionMode.ContiguousSelection
        else:
            NoSelection       = _QAIV.NoSelection
            SingleSelection   = _QAIV.SingleSelection
            MultiSelection    = _QAIV.MultiSelection
            ExtendedSelection = _QAIV.ExtendedSelection
            ContiguousSelection = _QAIV.ContiguousSelection

except Exception:
    class SelectionMode:
        NoSelection = 0; SingleSelection = 1; MultiSelection = 2
        ExtendedSelection = 3; ContiguousSelection = 4


# ---- QDialog result code compat (5 hits) ----
try:
    from qgis.PyQt.QtWidgets import QDialog as _QDlg

    class DialogCode:
        """QDialog result code constants for Qt5/Qt6."""
        if _IS_QT6:
            Accepted = _QDlg.DialogCode.Accepted
            Rejected = _QDlg.DialogCode.Rejected
        else:
            Accepted = _QDlg.Accepted
            Rejected = _QDlg.Rejected

except Exception:
    class DialogCode:
        Accepted = 1
        Rejected = 0


# ---- exec_compat() helper - replaces .exec_() for Qt5/Qt6 ----
def exec_compat(widget):
    """Call widget.exec() - works on both PyQt5 and PyQt6."""
    return widget.exec()


# ---- QVariantType — Qt5/Qt6 safe field type constants ----
# In Qt5 (PyQt5), field types are QVariant.Int, QVariant.String, etc.
# In Qt6 (PyQt6), QVariant is not exposed; use QMetaType.Type instead.
# All code should import QVariantType from this module instead of
# using QVariant directly.

class _QVariantTypeQt5:
    """Qt5 fallback using QVariant enum values (int literals)."""
    String   = 10
    Int      = 2
    LongLong = 4
    Double   = 6
    Bool     = 1
    UInt     = 3
    ULongLong = 5
    Date     = 14
    Time     = 15
    DateTime = 16

try:
    from qgis.PyQt.QtCore import QMetaType as _QMT

    class QVariantType:
        """Qt6 (QMetaType.Type) field type constants — also works in Qt5."""
        String    = _QMT.Type.QString
        Int       = _QMT.Type.Int
        LongLong  = _QMT.Type.LongLong
        Double    = _QMT.Type.Double
        Bool      = _QMT.Type.Bool
        UInt      = _QMT.Type.UInt
        ULongLong = _QMT.Type.ULongLong
        Date      = _QMT.Type.QDate
        Time      = _QMT.Type.QTime
        DateTime  = _QMT.Type.QDateTime

        # Numeric set for field-type filtering
        NUMERIC = frozenset([
            _QMT.Type.Int, _QMT.Type.LongLong, _QMT.Type.Double,
            _QMT.Type.Float, _QMT.Type.UInt, _QMT.Type.ULongLong,
        ])

except (ImportError, AttributeError):
    # Qt5 without QMetaType — use raw integer values (same as QVariant.*)
    class QVariantType(_QVariantTypeQt5):  # type: ignore[no-redef]
        NUMERIC = frozenset([2, 4, 6, 3, 5])  # Int, LongLong, Double, UInt, ULongLong


# Re-export a QVariant shim so existing code can do:
#   from ..shared.qt_compat import QVariant
# without breaking on Qt6.
QVariant = QVariantType
