import 'package:pshared/data/dto/recipient/recipient.dart';
import 'package:pshared/models/describable.dart';
import 'package:pshared/models/organization/bound.dart';
import 'package:pshared/models/permissions/bound.dart';
import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/models/recipient/status.dart';
import 'package:pshared/models/recipient/type.dart';
import 'package:pshared/models/storable.dart';


extension RecipientModelMapper on Recipient {
  RecipientDTO toDTO() => RecipientDTO(
    id: storable.id,
    createdAt: storable.createdAt,
    updatedAt: storable.updatedAt,
    permissionRef: permissionBound.permissionRef,
    organizationRef: permissionBound.organizationRef,
    name: name,
    description: description,
    email: email,
    avatarUrl: avatarUrl,
    status: _recipientStatusToValue(status),
    type: _recipientTypeToValue(type),
    isArchived: isArchived,
  );
}

extension RecipientDTOMapper on RecipientDTO {
  Recipient toDomain() => Recipient(
    storable: newStorable(id: id, createdAt: createdAt, updatedAt: updatedAt),
    permissionBound: newPermissionBound(
      organizationBound: newOrganizationBound(organizationRef: organizationRef),
      permissionRef: permissionRef,
    ),
    describable: newDescribable(name: name, description: description),
    email: email,
    avatarUrl: avatarUrl,
    status: _recipientStatusFromValue(status),
    type: _recipientTypeFromValue(type),
    isArchived: isArchived,
  );
}

RecipientStatus _recipientStatusFromValue(String value) {
  switch (value) {
    case 'ready':
      return RecipientStatus.ready;
    case 'registered':
      return RecipientStatus.registered;
    case 'notRegistered':
      return RecipientStatus.notRegistered;
    default:
      return RecipientStatus.ready;
  }
}

String _recipientStatusToValue(RecipientStatus status) {
  switch (status) {
    case RecipientStatus.ready:
      return 'ready';
    case RecipientStatus.registered:
      return 'registered';
    case RecipientStatus.notRegistered:
      return 'notRegistered';
  }
}

RecipientType _recipientTypeFromValue(String value) {
  switch (value) {
    case 'external':
      return RecipientType.external;
    case 'internal':
    default:
      return RecipientType.internal;
  }
}

String _recipientTypeToValue(RecipientType type) {
  switch (type) {
    case RecipientType.internal:
      return 'internal';
    case RecipientType.external:
      return 'external';
  }
}
