import 'dart:convert';

import '../../domain/entities/client_notification.dart';

class ClientNotificationDto {
  final int id;
  final String content;
  final int clientId;
  final String type;
  final String category;
  final String createdAt;
  final String? canceledAt;
  final String? readAt;

  ClientNotificationDto({
    required this.id,
    required this.content,
    required this.clientId,
    required this.type,
    required this.category,
    required this.createdAt,
    this.canceledAt,
    this.readAt,
  });

  factory ClientNotificationDto.fromJson(Map<String, dynamic> json) {
    return ClientNotificationDto(
      id: json['id'] as int,
      content: json['content'] as String,
      clientId: json['clientId'] as int,
      type: json['type'] as String,
      category: json['category'] as String,
      createdAt: json['createdAt'] as String,
      canceledAt: json['canceledAt'] as String?,
      readAt: json['readAt'] as String?,
    );
  }

  ClientNotification toEntity() {
    return ClientNotification(
      id: id,
      content: content,
      clientId: clientId,
      type: _parseType(type),
      category: _parseCategory(category),
      createdAt: DateTime.parse(createdAt),
      canceledAt: canceledAt != null ? DateTime.parse(canceledAt!) : null,
      readAt: readAt != null ? DateTime.parse(readAt!) : null,
    );
  }

  NotificationType _parseType(String type) {
    switch (type.toLowerCase()) {
      case 'info':
        return NotificationType.info;
      case 'attention':
        return NotificationType.attention;
      case 'warning':
        return NotificationType.warning;
      default:
        return NotificationType.info;
    }
  }

  NotificationCategory _parseCategory(String category) {
    switch (category.toLowerCase()) {
      case 'auth':
        return NotificationCategory.auth;
      case 'zone':
        return NotificationCategory.zone;
      case 'payment':
        return NotificationCategory.payment;
      case 'companyinfo':
        return NotificationCategory.companyInfo;
      case 'admininfo':
        return NotificationCategory.adminInfo;
      case 'scooter':
        return NotificationCategory.scooter;
      default:
        return NotificationCategory.companyInfo;
    }
  }
}
