import 'package:equatable/equatable.dart';

/// Type de média dans une story.
enum StoryMediaType {
  image,
  video,
}

/// Entité de domaine représentant une story.
///
/// Cette entité est pure et indépendante de la couche de données.
/// Elle représente une story éphémère (24h) dans le domaine métier.
class Story extends Equatable {
  const Story({
    required this.id,
    required this.userId,
    required this.userFirstName,
    required this.userLastName,
    required this.userProfileImageUrl,
    required this.userIsVerified,
    required this.mediaType,
    required this.mediaUrl,
    required this.createdAt,
    required this.expiresAt,
    this.thumbnailUrl,
    this.durationSeconds,
    this.isActive = true,
    this.viewsCount = 0,
    this.hasViewed = false,
  });

  final String id;
  final String userId;
  final String userFirstName;
  final String userLastName;
  final String userProfileImageUrl;
  final bool userIsVerified;
  final StoryMediaType mediaType;
  final String mediaUrl;
  final String? thumbnailUrl;
  final int? durationSeconds;
  final DateTime createdAt;
  final DateTime expiresAt;
  final bool isActive;
  final int viewsCount;
  final bool hasViewed;

  /// Retourne le nom complet de l'auteur.
  String get authorFullName => '$userFirstName $userLastName';

  /// Vérifie si la story est expirée.
  bool get isExpired => DateTime.now().isAfter(expiresAt);

  /// Retourne la durée formatée (MM:SS) pour les vidéos.
  String? get formattedDuration {
    if (durationSeconds == null) return null;
    final minutes = durationSeconds! ~/ 60;
    final seconds = durationSeconds! % 60;
    return '$minutes:${seconds.toString().padLeft(2, '0')}';
  }

  @override
  List<Object?> get props => [
        id,
        userId,
        userFirstName,
        userLastName,
        userProfileImageUrl,
        userIsVerified,
        mediaType,
        mediaUrl,
        thumbnailUrl,
        durationSeconds,
        createdAt,
        expiresAt,
        isActive,
        viewsCount,
        hasViewed,
      ];

  Story copyWith({
    String? id,
    String? userId,
    String? userFirstName,
    String? userLastName,
    String? userProfileImageUrl,
    bool? userIsVerified,
    StoryMediaType? mediaType,
    String? mediaUrl,
    String? thumbnailUrl,
    int? durationSeconds,
    DateTime? createdAt,
    DateTime? expiresAt,
    bool? isActive,
    int? viewsCount,
    bool? hasViewed,
  }) {
    return Story(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      userFirstName: userFirstName ?? this.userFirstName,
      userLastName: userLastName ?? this.userLastName,
      userProfileImageUrl: userProfileImageUrl ?? this.userProfileImageUrl,
      userIsVerified: userIsVerified ?? this.userIsVerified,
      mediaType: mediaType ?? this.mediaType,
      mediaUrl: mediaUrl ?? this.mediaUrl,
      thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl,
      durationSeconds: durationSeconds ?? this.durationSeconds,
      createdAt: createdAt ?? this.createdAt,
      expiresAt: expiresAt ?? this.expiresAt,
      isActive: isActive ?? this.isActive,
      viewsCount: viewsCount ?? this.viewsCount,
      hasViewed: hasViewed ?? this.hasViewed,
    );
  }
}
