import '../utils/app_time.dart';

class SwapRequest {
  SwapRequest({
    required this.id,
    required this.requesterId,
    required this.recipientId,
    required this.requesterScheduleId,
    required this.targetScheduleId,
    required this.status,
    required this.createdAt,
    required this.updatedAt,
    this.chatThreadId,
    this.shiftType,
    this.shiftStartTime,
    this.relieverIds,
    this.approvedBy,
  });

  final String id;
  final String requesterId;
  final String recipientId;
  final String requesterScheduleId; // previously `shiftId`
  final String? targetScheduleId;
  final String status;
  final DateTime createdAt;
  final DateTime? updatedAt;
  final String? chatThreadId;
  final String? shiftType;
  final DateTime? shiftStartTime;
  final List<String>? relieverIds;
  final String? approvedBy;

  factory SwapRequest.fromMap(Map<String, dynamic> map) {
    return SwapRequest(
      id: map['id'] as String,
      requesterId: map['requester_id'] as String,
      recipientId: map['recipient_id'] as String,
      requesterScheduleId:
          (map['requester_schedule_id'] as String?) ??
          (map['shift_id'] as String),
      targetScheduleId: map['target_shift_id'] as String?,
      status: map['status'] as String? ?? 'pending',
      createdAt: AppTime.parse(map['created_at'] as String),
      updatedAt: map['updated_at'] == null
          ? null
          : AppTime.parse(map['updated_at'] as String),
      chatThreadId: map['chat_thread_id'] as String?,
      shiftType: map['shift_type'] as String?,
      shiftStartTime: map['shift_start_time'] == null
          ? null
          : AppTime.parse(map['shift_start_time'] as String),
      relieverIds: map['reliever_ids'] is List
          ? (map['reliever_ids'] as List)
                .where((e) => e != null)
                .map((e) => e.toString())
                .toList()
          : const <String>[],
      approvedBy: map['approved_by'] as String?,
    );
  }
}
