import 'package:flutter/foundation.dart';

@immutable
class LivePhotoData {
  const LivePhotoData({
    required this.id,
    required this.imageUrl,
    required this.imagePath,
    required this.status,
    required this.timestamp,
  });

  factory LivePhotoData.fromJson(Map<String, dynamic> json) {
    // TODO: For now live_photo_obj returns a placeholder from the backend.
    final livePhotoObj = json['live_photo_obj'] as Map<String, dynamic>;

    return LivePhotoData(
      id: json['id'] as String? ?? '',
      imageUrl: json['live_photo'] as String? ?? '',
      imagePath: null,
      status: LivePhotoStatus.fromString(
        livePhotoObj['status'] as String? ?? '',
      ),
      timestamp: DateTime.tryParse(
            livePhotoObj['uploaded_at'] as String? ?? '',
          ) ??
          DateTime.now(),
    );
  }

  final String? id;
  final String? imageUrl;
  final String? imagePath;
  final LivePhotoStatus status;
  final DateTime timestamp;

  LivePhotoData copyWith({
    String? id,
    String? imageUrl,
    String? imagePath,
    LivePhotoStatus? status,
    DateTime? timestamp,
  }) {
    return LivePhotoData(
      id: id ?? this.id,
      imageUrl: imageUrl ?? this.imageUrl,
      imagePath: imagePath ?? this.imagePath,
      status: status ?? this.status,
      timestamp: timestamp ?? this.timestamp,
    );
  }
}

enum LivePhotoStatus {
  pending,
  verified,
  declined;

  static fromString(String value) {
    return switch (value) {
      'pending' => pending,
      'verified' => verified,
      _ => declined,
    };
  }
}
