import 'task_stage.dart';

class Task {
  final String id;
  final String documentId;
  final String taskType;
  final String name;
  final String status;
  final int progress;
  final String? currentStep;
  final String? errorMessage;
  final DateTime? startedAt;
  final DateTime? completedAt;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<TaskStage> stages;

  Task({
    required this.id,
    required this.documentId,
    required this.taskType,
    required this.name,
    required this.status,
    required this.progress,
    this.currentStep,
    this.errorMessage,
    this.startedAt,
    this.completedAt,
    this.createdAt,
    this.updatedAt,
    required this.stages,
  });

  factory Task.fromJson(Map<String, dynamic> data) {
    return Task(
      id: data['id'] != null ? data['id'].toString() : '',
      documentId:
          data['documentId'] != null ? data['documentId'].toString() : '',
      taskType: data['taskType'] != null ? data['taskType'].toString() : 'parse',
      name: data['name'] != null ? data['name'].toString() : '',
      status: data['status'] != null ? data['status'].toString() : 'pending',
      progress: (data['progress'] ?? 0) as int,
      currentStep:
          data['currentStep'] != null ? data['currentStep'].toString() : null,
      errorMessage:
          data['errorMessage'] != null ? data['errorMessage'].toString() : null,
      startedAt: data['startedAt'] != null
          ? DateTime.parse(data['startedAt'].toString())
          : null,
      completedAt: data['completedAt'] != null
          ? DateTime.parse(data['completedAt'].toString())
          : null,
      createdAt: data['createdAt'] != null
          ? DateTime.parse(data['createdAt'].toString())
          : null,
      updatedAt: data['updatedAt'] != null
          ? DateTime.parse(data['updatedAt'].toString())
          : null,
      stages: (data['stages'] as List<dynamic>? ?? [])
          .map((e) => TaskStage.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }
}


