import 'dart:convert'; import 'word_model.dart'; class SessionWord { final String word; final int sessionNumber; final String partOfSpeech; final String category; final String symbolicMeaning; final String? positionTitle; const SessionWord({ required this.word, required this.sessionNumber, required this.partOfSpeech, required this.category, required this.symbolicMeaning, this.positionTitle, }); String get formattedSessionNumber => sessionNumber.toString().padLeft(3, '0'); Map toMap() => { 'word': word, 'sessionNumber': sessionNumber, 'partOfSpeech': partOfSpeech, 'category': category, 'symbolicMeaning': symbolicMeaning, if (positionTitle != null) 'positionTitle': positionTitle, }; factory SessionWord.fromMap(Map map) => SessionWord( word: map['word'] as String? ?? '', sessionNumber: map['sessionNumber'] as int? ?? 0, partOfSpeech: map['partOfSpeech'] as String? ?? '', category: map['category'] as String? ?? '', symbolicMeaning: map['symbolicMeaning'] as String? ?? '', positionTitle: map['positionTitle'] as String?, ); factory SessionWord.fromWordItem(WordItem item, {String? positionTitle}) => SessionWord( word: item.word, sessionNumber: item.sessionNumber, partOfSpeech: item.partOfSpeech, category: item.category, symbolicMeaning: item.displayMeaning, positionTitle: positionTitle, ); } class SessionChatMessage { final String id; final String sender; // 'user' | 'oracle' | 'system' final String text; final DateTime timestamp; final SessionWord? attachedWord; const SessionChatMessage({ required this.id, required this.sender, required this.text, required this.timestamp, this.attachedWord, }); Map toMap() => { 'id': id, 'sender': sender, 'text': text, 'timestamp': timestamp.toIso8601String(), if (attachedWord != null) 'attachedWord': attachedWord!.toMap(), }; factory SessionChatMessage.fromMap(Map map) => SessionChatMessage( id: map['id'] as String? ?? '', sender: map['sender'] as String? ?? 'oracle', text: map['text'] as String? ?? '', timestamp: DateTime.tryParse(map['timestamp'] as String? ?? '') ?? DateTime.now(), attachedWord: map['attachedWord'] != null ? SessionWord.fromMap(map['attachedWord'] as Map) : null, ); } class OracleSession { final String id; final DateTime timestamp; final String? question; final int wordsCount; final List words; final String? aiInterpretation; final List messages; const OracleSession({ required this.id, required this.timestamp, this.question, required this.wordsCount, required this.words, this.aiInterpretation, this.messages = const [], }); OracleSession copyWith({ String? id, DateTime? timestamp, String? question, int? wordsCount, List? words, String? aiInterpretation, List? messages, }) { return OracleSession( id: id ?? this.id, timestamp: timestamp ?? this.timestamp, question: question ?? this.question, wordsCount: wordsCount ?? this.wordsCount, words: words ?? this.words, aiInterpretation: aiInterpretation ?? this.aiInterpretation, messages: messages ?? this.messages, ); } String get formattedDate { final day = timestamp.day.toString().padLeft(2, '0'); final month = timestamp.month.toString().padLeft(2, '0'); final year = timestamp.year.toString(); final hour = timestamp.hour.toString().padLeft(2, '0'); final min = timestamp.minute.toString().padLeft(2, '0'); final sec = timestamp.second.toString().padLeft(2, '0'); return '$day.$month.$year $hour:$min:$sec'; } Map toMap() => { 'id': id, 'timestamp': timestamp.toIso8601String(), if (question != null) 'question': question, 'wordsCount': wordsCount, 'words': words.map((w) => w.toMap()).toList(), if (aiInterpretation != null) 'aiInterpretation': aiInterpretation, 'messages': messages.map((m) => m.toMap()).toList(), }; factory OracleSession.fromMap(Map map) { return OracleSession( id: map['id'] as String? ?? '', timestamp: DateTime.tryParse(map['timestamp'] as String? ?? '') ?? DateTime.now(), question: map['question'] as String?, wordsCount: map['wordsCount'] as int? ?? (map['words'] as List?)?.length ?? 1, words: (map['words'] as List?) ?.map((item) => SessionWord.fromMap(item as Map)) .toList() ?? [], aiInterpretation: map['aiInterpretation'] as String?, messages: (map['messages'] as List?) ?.map((item) => SessionChatMessage.fromMap(item as Map)) .toList() ?? [], ); } String toJson() => jsonEncode(toMap()); factory OracleSession.fromJson(String source) => OracleSession.fromMap(jsonDecode(source)); } typedef HistoryEntry = OracleSession;