import 'package:cross_file/cross_file.dart';

class TicketModel {
  List<Tickets> tickets = [];
  TicketModel();

  TicketModel.fromJson(Map<String, dynamic> json) {
    if (json['tickets'] != null) {
      Map<String, dynamic> t = json['tickets'];
      final keysIterable = t.keys;
      List<String> keysList = keysIterable.toList();
      for (var key in keysList) {
        Map<String, dynamic> map = {'tickets': t[key], 'date': key};
        tickets.add(Tickets.fromJson(map));
      }
    }
  }
}

class Tickets {
  String? date;
  List<Ticket> tickets = [];

  Tickets({List<Ticket>? tickets, this.date}) {
    if (tickets != null) {
      this.tickets.addAll(tickets);
    }
  }

  Tickets.fromJson(Map<String, dynamic> json) {
    if (json['tickets'] != null) {
      json['tickets'].forEach((v) {
        tickets.add(Ticket.fromJson(v));
      });
    }
    date = json['date'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (tickets.isNotEmpty) {
      data['tickets'] = tickets.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Ticket {
  int? id;
  String? text;
  String? role;
  String? file;
  XFile? localFile;
  String? createdAt;

  Ticket(
      {this.id,
      this.text,
      this.role,
      this.file,
      this.createdAt,
      this.localFile});

  Ticket.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    text = json['text'];
    role = json['role'];
    file = json['file'];
    createdAt = json['created_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['text'] = text;
    data['role'] = role;
    data['file'] = file;
    data['created_at'] = createdAt;
    return data;
  }
}
