import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:health_app/pages/home/widgets/chat_messages_view.dart';
import 'package:health_app/providers/chat_provider.dart';

void main() {
  test('chat messages keep their natural display order', () {
    final messages = [
      ChatMessage(
        id: 'oldest',
        role: 'user',
        content: '第一条',
        createdAt: DateTime(2026, 1, 1),
      ),
      ChatMessage(
        id: 'newest',
        role: 'assistant',
        content: '最新一条',
        createdAt: DateTime(2026, 1, 2),
      ),
    ];

    expect(messageAtDisplayIndex(messages, 0).id, 'oldest');
    expect(messageAtDisplayIndex(messages, 1).id, 'newest');
  });

  test('today health card stays in the normal message stream', () {
    final taskCard = ChatMessage(
      id: 'task_card',
      role: 'assistant',
      content: '',
      createdAt: DateTime(2026, 1, 1),
      type: MessageType.taskCard,
    );
    final userMessage = ChatMessage(
      id: 'user',
      role: 'user',
      content: '你好',
      createdAt: DateTime(2026, 1, 2),
    );

    final messages = [taskCard, userMessage];

    expect(messageAtDisplayIndex(messages, 0).id, 'task_card');
    expect(messageAtDisplayIndex(messages, 1).id, 'user');
  });

  test('today health keeps one cached snapshot during background refresh', () {
    final providers = File(
      'lib/providers/data_providers.dart',
    ).readAsStringSync();
    final card = File(
      'lib/pages/home/widgets/chat_messages_view.dart',
    ).readAsStringSync();
    final home = File('lib/pages/home/home_page.dart').readAsStringSync();

    expect(providers, contains('todayHealthSnapshotProvider'));
    expect(
      providers,
      isNot(
        contains(
          'notificationUnreadCountProvider = FutureProvider.autoDispose',
        ),
      ),
    );
    expect(
      providers,
      isNot(
        contains(
          'medicationReminderProvider =\n    FutureProvider.autoDispose',
        ),
      ),
    );
    expect(
      providers,
      isNot(
        contains(
          'currentExercisePlanProvider =\n    FutureProvider.autoDispose',
        ),
      ),
    );
    expect(card, contains('ref.watch(todayHealthSnapshotProvider)'));
    expect(card, contains('final snapshot = snapshotAsync.value'));
    expect(card, contains('正在获取今日健康'));
    expect(home, contains('_refreshTodayHealthIfCached()'));
  });

  test('drawer keeps seven recent conversations and expands in place', () {
    final source = File('lib/widgets/health_drawer.dart').readAsStringSync();

    expect(source, contains('static const int _previewCount = 7;'));
    expect(source, contains('bool _expanded = false;'));
    expect(source, isNot(contains("pushRoute(ref, 'conversationHistory')")));
  });

  test('history actions stay inside the section hit-test area', () {
    final source = File('lib/widgets/health_drawer.dart').readAsStringSync();

    expect(source, isNot(contains('top: -46')));
    expect(source, contains('trailing:'));
  });

  test('drawer sections use spacing without horizontal divider lines', () {
    final source = File('lib/widgets/health_drawer.dart').readAsStringSync();

    expect(source, isNot(contains('Divider(')));
    expect(source, contains('const SizedBox(height: 22)'));
  });

  test('home drawer gesture is limited to the conversation stream', () {
    final source = File('lib/pages/home/home_page.dart').readAsStringSync();

    expect(source, contains('drawerEnableOpenDragGesture: false'));
    expect(
      source,
      contains('onHorizontalDragUpdate: _updateConversationDrawerDrag'),
    );
    expect(source, isNot(contains('_drawerDragStartX')));
  });

  test('agent capsules use slightly larger icons and labels', () {
    final source = File('lib/pages/home/home_page.dart').readAsStringSync();
    final cards = File(
      'lib/pages/home/widgets/chat_messages_view.dart',
    ).readAsStringSync();
    final drawer = File('lib/widgets/health_drawer.dart').readAsStringSync();

    expect(source, contains('width: 18'));
    expect(source, contains('height: 18'));
    expect(source, contains('visual.icon,'));
    expect(source, contains('size: 13'));
    expect(source, contains('fontSize: 15'));
    expect(source, contains('gradient: selected'));
    expect(source, contains('? AppColors.actionOutlineGradient'));
    expect(source, contains('selected ? null : Colors.transparent'));
    expect(source, contains('color: Colors.white'));
    expect(cards, isNot(contains('LucideIcons.footprints')));
    expect(cards, contains('AppModuleVisuals.exercise.icon'));
    expect(drawer, contains('AppModuleVisuals.exercise.icon'));
  });

  test('upload and send actions stay inside one input capsule', () {
    final source = File('lib/pages/home/home_page.dart').readAsStringSync();
    final input = source.substring(
      source.indexOf('Widget _buildInputBar()'),
      source.indexOf('void _showAttachmentPicker'),
    );

    expect(input, contains('borderRadius: AppRadius.pillBorder'));
    expect(input, contains('LucideIcons.plus'));
    expect(input, contains('LucideIcons.send'));
    expect(input, contains('border: InputBorder.none'));
    expect(input, contains('crossAxisAlignment: CrossAxisAlignment.center'));
    expect(
      RegExp('gradient: AppColors.primaryGradient').allMatches(input).length,
      2,
    );
    expect(input, isNot(contains('Border.all(color: AppColors.borderLight)')));
    expect(source, isNot(contains('class _RoundToolButton')));
  });
}
