import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hub/src/features/hub/domain/entities/address_entity.dart';
import 'package:hub/src/features/hub/domain/entities/network_entity.dart';
import 'package:hub/src/features/hub/domain/entities/position_entity.dart';

part 'latest_positions_response_model.freezed.dart';
part 'latest_positions_response_model.g.dart';

@freezed
abstract class LatestPositionsResponseModel with _$LatestPositionsResponseModel {
  const factory LatestPositionsResponseModel({
    required List<LatestPositionsItemResponseModel> items,
  }) = _LatestPositionsResponseModel;

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

@freezed
abstract class LatestPositionsItemResponseModel
    with _$LatestPositionsItemResponseModel {
  const factory LatestPositionsItemResponseModel({
    required String id,
    required String deviceIdentificator,
    required double latitude,
    required double longitude,
    required int hpe,
    int? ncell,
    required int type,
    int? steps,
    LatestPositionsAddressResponseModel? address,
    required int createdAt,
    required int positionDate,
    int? positionDateOriginal,
    String? frequentPlaceName,
    required String message,
    required List<LatestPositionsNetworkResponseModel> networks,
    required bool ignore,
    required bool suspect,
    required bool frequentPlace,
  }) = _LatestPositionsItemResponseModel;

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

extension LatestPositionsResponseModelMapper on LatestPositionsResponseModel {
  List<PositionEntity> toEntity() {
  return items.map((LatestPositionsItemResponseModel item) => PositionEntity(
    id: item.id,
    deviceIdentificator: item.deviceIdentificator,
    latitude: item.latitude,
    longitude: item.longitude,
    hpe: item.hpe,
    ncell: item.ncell,
    type: item.type,
    steps: item.steps,
    address: item.address?.toEntity(),
    createdAt: item.createdAt,
    positionDate: item.positionDate,
    positionDateOriginal: item.positionDateOriginal,
    frequentPlaceName: item.frequentPlaceName,
    message: item.message,
    networks: item.networks.map((n)=>n.toEntity()).toList(),
    ignore: item.ignore,
    suspect: item.suspect,
    frequentPlace: item.frequentPlace,
    )).toList();
  }
}

@freezed
abstract class LatestPositionsAddressResponseModel with _$LatestPositionsAddressResponseModel {
  const factory LatestPositionsAddressResponseModel({
    required String street,
    required String city,
    required String province,
    required String state,
    required String country,
  }) = _LatestPositionsAddressResponseModel;

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

extension LatestPositionsAddressResponseModelMapper on LatestPositionsAddressResponseModel {
  AddressEntity toEntity() {
    return AddressEntity(
      street: street,
      city: city,
      province: province,
      state: state,
      country: country,
    );
  }
}

@freezed
abstract class LatestPositionsNetworkResponseModel with _$LatestPositionsNetworkResponseModel {
  const factory LatestPositionsNetworkResponseModel({
    required String SSID,
    required String BSSID,
    required String signal,
  }) = _LatestPositionsNetworkResponseModel;

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

extension LatestPositionsNetworkResponseModelMapper on LatestPositionsNetworkResponseModel {
  NetworkEntity toEntity() {
    return NetworkEntity(
      SSID: SSID,
      BSSID: BSSID,
      signal: signal,
    );
  }
}