import 'package:legacy_shared/src/data/models/entities/device_entity.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'get_devices_response_model.freezed.dart';
part 'get_devices_response_model.g.dart';

@freezed
abstract class GetDevicesResponseModel with _$GetDevicesResponseModel {
  const factory GetDevicesResponseModel({
    required List<GetDevicesItemResponseModel> items,
  }) = _GetDevicesResponseModel;

  factory GetDevicesResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetDevicesResponseModelFromJson(json);
}

@freezed
abstract class GetDevicesItemResponseModel with _$GetDevicesItemResponseModel {
  const factory GetDevicesItemResponseModel({
    required String identificator,
    required String carrierName,
    required String? carrierGenre,
    required int? carrierBirthday,
    required double? carrierWeight,
    required double? carrierStepLength,
    required String phone,
    required String id,
    required GetDevicesSettingsResponseModel settings,
    required String protocol,
    required String type,
    required String connectionServer,
    required int createdAt,
    required GetDevicesFlagsResponseModel flags,
  }) =
  _GetDevicesItemResponseModel;

  factory GetDevicesItemResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetDevicesItemResponseModelFromJson(json);
}

extension GetDevicesResponseModelMapper on GetDevicesResponseModel {
  List<DeviceEntity> toEntity() {
    return items.map((GetDevicesItemResponseModel item) => DeviceEntity(
      identificator: item.identificator,
      carrierName: item.carrierName,
      carrierGenre: item.carrierGenre,
      carrierBirthday: item.carrierBirthday,
      carrierWeight: item.carrierWeight,
      carrierStepLength: item.carrierStepLength,
      phone: item.phone,
      id: item.id,
      settings: item.settings.toEntity(),
      protocol: item.protocol,
      type: item.type,
      connectionServer: item.connectionServer,
      createdAt: item.createdAt,
      flags: item.flags.toEntity(),
    )).toList();
  }
}

@freezed
abstract class GetDevicesSettingsResponseModel with _$GetDevicesSettingsResponseModel {
  const factory GetDevicesSettingsResponseModel({
    required int frequency,
    required int frequencyHeartRate,
    required int timezone,
    required bool pedometer,
    required String language,
    required List<String> alerts,
  }) = _GetDevicesSettingsResponseModel;

  factory GetDevicesSettingsResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetDevicesSettingsResponseModelFromJson(json);
}

extension GetDevicesSettingsResponseModelMapper on GetDevicesSettingsResponseModel {
  DeviceSettingsEntity toEntity() {
    return DeviceSettingsEntity(
      frequency: frequency,
      frequencyHeartRate: frequencyHeartRate,
      timezone: timezone,
      pedometer: pedometer,
      language: language,
      alerts: alerts,
    );
  }
}

@freezed
abstract class GetDevicesFlagsResponseModel with _$GetDevicesFlagsResponseModel {
  const factory GetDevicesFlagsResponseModel({
    required String isInOrOut,
    required String geofenceId,
    required bool isBatteryLow,
    required bool isDisconnect,
  }) = _GetDevicesFlagsResponseModel;

  factory GetDevicesFlagsResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetDevicesFlagsResponseModelFromJson(json);
}

extension GetDevicesFlagsResponseModelMapper on GetDevicesFlagsResponseModel {
  DeviceFlagsEntity toEntity() {
    return DeviceFlagsEntity(
      isInOrOut: isInOrOut,
      geofenceId: geofenceId,
      isBatteryLow: isBatteryLow,
      isDisconnect: isDisconnect,
    );
  }
}