import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';

@immutable
class DevInspectorInfo {
  const DevInspectorInfo({
    required this.widgetType,
    required this.boundingBox,
    required this.properties,
    required this.ancestors,
    this.screenHint,
    this.semanticWidget,
    this.searchHints = const <String>[],
  });

  final String widgetType;
  final Rect boundingBox;
  final List<String> properties;
  final List<String> ancestors;
  final String? screenHint;
  final String? semanticWidget;
  final List<String> searchHints;

  String toAIClipboard({String? routeName}) {
    final buf = StringBuffer();
    buf.writeln('## Inspected widget');
    final resolvedScreen = (routeName != null && routeName.isNotEmpty)
        ? routeName
        : screenHint;
    if (resolvedScreen != null && resolvedScreen.isNotEmpty) {
      buf.writeln('**Screen:** `$resolvedScreen`');
    }
    if (semanticWidget != null && semanticWidget!.isNotEmpty) {
      buf.writeln('**Semantic target:** `$semanticWidget`');
    }
    buf.writeln('**Type:** `$widgetType`');

    if (ancestors.isNotEmpty) {
      final chain = [...ancestors.reversed, widgetType].join(' › ');
      buf.writeln('**Tree:** `$chain`');
    }

    final w = boundingBox.width.toStringAsFixed(0);
    final h = boundingBox.height.toStringAsFixed(0);
    buf.writeln('**Size:** $w×${h}px');

    if (properties.isNotEmpty) {
      buf.writeln();
      buf.writeln('**Properties:**');
      for (final p in properties) {
        buf.writeln('- `$p`');
      }
    }
    if (searchHints.isNotEmpty) {
      buf.writeln();
      buf.writeln('**Search hints:**');
      for (final hint in searchHints) {
        buf.writeln('- `$hint`');
      }
    }

    buf.writeln();
    buf.writeln(
      'Update the `${semanticWidget ?? widgetType}` widget above. '
      'This project uses Flutter + Riverpod. '
      'Edit the correct file in `lib/`.',
    );
    return buf.toString();
  }
}
