import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:social_app/features/chat/data/models/tool_result.dart';
import 'package:social_app/features/chat/ui/widgets/ui_schema_renderer.dart';

void main() {
  group('UiSchemaRenderer', () {
    testWidgets('calendar_card.v1 renders title', (tester) async {
      final card = UiCard(
        cardType: 'calendar_card.v1',
        data: CalendarCardData(
          id: 'evt_001',
          title: 'Team Meeting',
          startAt: '2026-03-01T10:00:00Z',
        ).toJson(),
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('Team Meeting'), findsOneWidget);
    });

    testWidgets('calendar_card.v1 renders time', (tester) async {
      final card = UiCard(
        cardType: 'calendar_card.v1',
        data: CalendarCardData(
          id: 'evt_001',
          title: 'Meeting',
          startAt: '2026-03-01T10:00:00Z',
          endAt: '2026-03-01T11:30:00Z',
        ).toJson(),
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.textContaining('3月1日'), findsOneWidget);
    });

    testWidgets('calendar_card.v1 renders location', (tester) async {
      final card = UiCard(
        cardType: 'calendar_card.v1',
        data: CalendarCardData(
          id: 'evt_001',
          title: 'Meeting',
          startAt: '2026-03-01T10:00:00Z',
          location: 'Room 101',
        ).toJson(),
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('Room 101'), findsOneWidget);
    });

    testWidgets('calendar_card.v1 renders description', (tester) async {
      final card = UiCard(
        cardType: 'calendar_card.v1',
        data: CalendarCardData(
          id: 'evt_001',
          title: 'Meeting',
          startAt: '2026-03-01T10:00:00Z',
          description: 'Quarterly review',
        ).toJson(),
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('Quarterly review'), findsOneWidget);
    });

    testWidgets('calendar_card.v1 renders AI generated tag', (tester) async {
      final card = UiCard(
        cardType: 'calendar_card.v1',
        data: CalendarCardData(
          id: 'evt_001',
          title: 'Meeting',
          startAt: '2026-03-01T10:00:00Z',
          sourceType: 'ai_generated',
        ).toJson(),
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('AI生成'), findsOneWidget);
    });

    testWidgets('error_card.v1 renders error message', (tester) async {
      final card = UiCard(
        cardType: 'error_card.v1',
        data: {'message': 'Something went wrong'},
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('Something went wrong'), findsOneWidget);
    });

    testWidgets('error_card.v1 renders default message when missing', (
      tester,
    ) async {
      final card = UiCard(cardType: 'error_card.v1', data: {});

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('发生错误'), findsOneWidget);
    });

    testWidgets('unknown card type renders fallback', (tester) async {
      final card = UiCard(cardType: 'unknown_type', data: {'foo': 'bar'});

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.textContaining('未知卡片类型'), findsOneWidget);
      expect(find.textContaining('unknown_type'), findsOneWidget);
    });

    testWidgets('calendar_card.v1 renders actions', (tester) async {
      final card = UiCard(
        cardType: 'calendar_card.v1',
        data: CalendarCardData(
          id: 'evt_001',
          title: 'Meeting',
          startAt: '2026-03-01T10:00:00Z',
        ).toJson(),
        actions: [
          CardAction(type: 'link', label: '查看详情', target: '/calendar/evt_001'),
        ],
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('查看详情'), findsOneWidget);
    });

    testWidgets('calendar_card.v1 renders custom color', (tester) async {
      final card = UiCard(
        cardType: 'calendar_card.v1',
        data: CalendarCardData(
          id: 'evt_001',
          title: 'Meeting',
          startAt: '2026-03-01T10:00:00Z',
          color: '#FF0000',
        ).toJson(),
      );

      await tester.pumpWidget(
        MaterialApp(home: Scaffold(body: UiSchemaRenderer.render(card))),
      );

      expect(find.text('Meeting'), findsOneWidget);
    });
  });
}
