// lib/features/admin/tabs/messages/resizable_split_view.dart

import 'package:flutter/material.dart';

class ResizableSplitView extends StatefulWidget {
  final Widget leftWidget;
  final Widget rightWidget;
  final double minLeftWidth;
  final double maxLeftWidth;
  final double defaultLeftWidth;
  final double minRightWidth;

  const ResizableSplitView({
    super.key,
    required this.leftWidget,
    required this.rightWidget,
    this.minLeftWidth = 320,
    this.maxLeftWidth = 520,
    this.defaultLeftWidth = 390,
    this.minRightWidth = 480,
  });

  @override
  State<ResizableSplitView> createState() => _ResizableSplitViewState();
}

class _ResizableSplitViewState extends State<ResizableSplitView> {
  late double _leftWidth;
  bool _isHovering = false;
  bool _isDragging = false;

  static const double _dividerHitWidth = 10;

  @override
  void initState() {
    super.initState();
    _leftWidth = widget.defaultLeftWidth;
  }

  double _effectiveMaxLeft(double totalWidth) {
    final byRightPane = totalWidth - widget.minRightWidth - _dividerHitWidth;
    return byRightPane.clamp(widget.minLeftWidth, widget.maxLeftWidth).toDouble();
  }

  double _clampedLeft(double totalWidth) {
    final maxLeft = _effectiveMaxLeft(totalWidth);
    return _leftWidth.clamp(widget.minLeftWidth, maxLeft).toDouble();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final totalWidth = constraints.maxWidth;
        final minimumNeeded =
            widget.minLeftWidth + widget.minRightWidth + _dividerHitWidth;

        // Defensive fallback. The parent normally switches to a single-pane
        // layout before this point, but this keeps the widget safe if reused.
        if (totalWidth < minimumNeeded) {
          return widget.rightWidget;
        }

        final leftWidth = _clampedLeft(totalWidth);

        return Row(
          children: [
            SizedBox(
              width: leftWidth,
              child: ClipRect(child: widget.leftWidget),
            ),
            MouseRegion(
              cursor: SystemMouseCursors.resizeLeftRight,
              onEnter: (_) => setState(() => _isHovering = true),
              onExit: (_) => setState(() => _isHovering = false),
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onPanStart: (_) => setState(() => _isDragging = true),
                onPanEnd: (_) => setState(() => _isDragging = false),
                onPanCancel: () => setState(() => _isDragging = false),
                onPanUpdate: (details) {
                  final maxLeft = _effectiveMaxLeft(totalWidth);
                  setState(() {
                    _leftWidth = (_leftWidth + details.delta.dx)
                        .clamp(widget.minLeftWidth, maxLeft)
                        .toDouble();
                  });
                },
                child: SizedBox(
                  width: _dividerHitWidth,
                  child: Center(
                    child: AnimatedContainer(
                      duration: const Duration(milliseconds: 120),
                      width: _isDragging ? 4 : (_isHovering ? 3 : 1),
                      height: double.infinity,
                      color: (_isHovering || _isDragging)
                          ? const Color.fromARGB(255, 2, 105, 48)
                          : const Color(0xFFE2E8E4),
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: ClipRect(child: widget.rightWidget),
            ),
          ],
        );
      },
    );
  }
}
