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

class DutySchedule {
  DutySchedule({
    required this.id,
    required this.userId,
    required this.shiftType,
    required this.startTime,
    required this.endTime,
    required this.status,
    required this.createdAt,
    required this.checkInAt,
    required this.checkInLocation,
    required this.relieverIds,
  });

  final String id;
  final String userId;
  final String shiftType;
  final DateTime startTime;
  final DateTime endTime;
  final String status;
  final DateTime createdAt;
  final DateTime? checkInAt;
  final Object? checkInLocation;
  final List<String> relieverIds;

  factory DutySchedule.fromMap(Map<String, dynamic> map) {
    final relieversRaw = map['reliever_ids'];
    final relievers = relieversRaw is List
        ? relieversRaw
              .where((e) => e != null)
              .map((entry) => entry.toString())
              .where((s) => s.isNotEmpty)
              .toList()
        : <String>[];
    return DutySchedule(
      id: map['id'] as String,
      userId: map['user_id'] as String,
      shiftType: map['shift_type'] as String? ?? 'normal',
      startTime: AppTime.parse(map['start_time'] as String),
      endTime: AppTime.parse(map['end_time'] as String),
      status: map['status'] as String? ?? 'scheduled',
      createdAt: AppTime.parse(map['created_at'] as String),
      checkInAt: map['check_in_at'] == null
          ? null
          : AppTime.parse(map['check_in_at'] as String),
      checkInLocation: map['check_in_location'],
      relieverIds: relievers,
    );
  }
}
