import 'package:flutter_test/flutter_test.dart';
import 'package:showen_v2_flutter/models/ai_model.dart';
import 'package:showen_v2_flutter/models/character_pack.dart';
import 'package:showen_v2_flutter/models/chat_models.dart';

void main() {
  group('CharacterPack / AvailableConfigs', () {
    test('parses M2.1 config available payload', () {
      final snapshot = AvailableConfigs.fromJson({
        'active': 'dog_state_machine.json',
        'configs': [
          {
            'filename': 'dog_state_machine.json',
            'character': {
              'name': '小狗',
              'character_type': 'pet',
              'render_type': 'video',
            },
          },
          {
            'filename': 'live2d_demo.json',
            'character': {
              'name': 'Live2D',
              'character_type': 'live2d',
              'render_type': 'live2d',
              'live2d_model': 'Hiyori',
            },
          },
        ],
      });

      expect(snapshot.active, 'dog_state_machine.json');
      expect(snapshot.configs, hasLength(2));
      expect(snapshot.configs.first.name, '小狗');
      expect(snapshot.configs.first.typeLabel, '宠物');
      expect(snapshot.configs.last.isLive2d, isTrue);
    });
  });

  group('ChatTurnResponse', () {
    test('parses success and error responses', () {
      final ok = ChatTurnResponse.fromJson({
        'session_id': 'app_1',
        'transcription': '你好',
        'reply_text': '汪！',
        'reply_audio_path': '/tmp/tts.wav',
        'error': null,
      });
      expect(ok.isOk, isTrue);
      expect(ok.replyText, '汪！');
      expect(ok.replyAudioPath, '/tmp/tts.wav');

      final err = ChatTurnResponse.fromJson({
        'session_id': '',
        'reply_text': '',
        'error': 'AI 插件未就绪',
      });
      expect(err.isOk, isFalse);
      expect(err.error, 'AI 插件未就绪');
    });
  });

  group('ModelsSnapshot', () {
    test('parses model list and active map', () {
      final snapshot = ModelsSnapshot.fromJson({
        'models': [
          {
            'id': 'qwen2.5-0.5b-q4_k_m',
            'kind': 'llm',
            'name': 'Qwen2.5 0.5B',
            'version': '1',
            'size': 400000000,
            'memory_required': 1000000000,
            'recommended_tier': '4G',
            'downloaded': true,
            'download_progress': 100,
          },
        ],
        'active': {'llm': 'qwen2.5-0.5b-q4_k_m', 'asr': 'whisper-tiny'},
        'used_space': 500000000,
        'quota': 4000000000,
      });

      expect(snapshot.models, hasLength(1));
      expect(snapshot.isActive(snapshot.models.first), isTrue);
      expect(snapshot.usageRatio, closeTo(0.125, 0.001));
      expect(snapshot.models.first.kindLabel, 'LLM');
    });
  });
}
