import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:syncrow_app/features/auth/model/project_model.dart'; import 'package:syncrow_app/features/auth/model/token.dart'; class UserModel { static String userUuidKey = 'userUuid'; final String? uuid; final String? email; final String? firstName; final String? lastName; final Uint8List? profilePicture; final String? phoneNumber; final bool? isEmailVerified; final String? regionName; final String? timeZone; final String? regionUuid; final bool? isAgreementAccepted; final bool? hasAcceptedWebAgreement; final DateTime? webAgreementAcceptedAt; final bool? hasAcceptedAppAgreement; final DateTime? appAgreementAcceptedAt; final Role? role; final Project? project; UserModel({ required this.uuid, required this.email, required this.firstName, required this.lastName, required this.profilePicture, required this.phoneNumber, required this.isEmailVerified, required this.regionUuid, required this.isAgreementAccepted, required this.regionName, required this.timeZone, required this.hasAcceptedWebAgreement, required this.webAgreementAcceptedAt, required this.hasAcceptedAppAgreement, required this.appAgreementAcceptedAt, required this.role, required this.project, }); factory UserModel.fromJson(Map json) { return UserModel( uuid: json['uuid'], email: json['email'], firstName: json['firstName'], lastName: json['lastName'], profilePicture: UserModel.decodeBase64Image(json['profilePicture']), phoneNumber: json['phoneNumber'], isEmailVerified: json['isEmailVerified'], isAgreementAccepted: json['isAgreementAccepted'], regionName: json['region']?['regionName'], timeZone: json['timeZone']?['timeZoneOffset'], regionUuid: json['region']?['uuid'], hasAcceptedWebAgreement: json['hasAcceptedWebAgreement'], webAgreementAcceptedAt: json['webAgreementAcceptedAt'] != null ? DateTime.parse(json['webAgreementAcceptedAt']) : null, hasAcceptedAppAgreement: json['hasAcceptedAppAgreement'], appAgreementAcceptedAt: json['appAgreementAcceptedAt'] != null ? DateTime.parse(json['appAgreementAcceptedAt']) : null, role: json['role'] != null ? Role.fromJson(json['role']) : null, project: json['project'] != null ? Project.fromJson(json['project']) : null, ); } factory UserModel.fromToken(Token token) { Map tempJson = Token.decodeToken(token.accessToken); return UserModel( uuid: tempJson['uuid'].toString(), email: tempJson['email'], lastName: tempJson['lastName'], firstName: tempJson['firstName'], profilePicture: UserModel.decodeBase64Image(tempJson['profilePicture']), phoneNumber: null, isEmailVerified: null, isAgreementAccepted: null, regionUuid: null, regionName: tempJson['region']?['regionName'], timeZone: tempJson['timezone']?['timeZoneOffset'], hasAcceptedWebAgreement: tempJson['hasAcceptedWebAgreement'], webAgreementAcceptedAt: tempJson['webAgreementAcceptedAt'] != null ? DateTime.parse(tempJson['webAgreementAcceptedAt']) : null, hasAcceptedAppAgreement: tempJson['hasAcceptedAppAgreement'], appAgreementAcceptedAt: tempJson['appAgreementAcceptedAt'] != null ? DateTime.parse(tempJson['appAgreementAcceptedAt']) : null, role: tempJson['role'] != null ? Role.fromJson(tempJson['role']) : null, project: null); } static Uint8List? decodeBase64Image(String? base64String) { if (base64String != null) { return base64.decode(base64String); } return null; } Map toJson() { return { 'uuid': uuid, 'email': email, 'firstName': firstName, 'lastName': lastName, 'profilePicture': profilePicture != null ? base64.encode(profilePicture!) : null, 'phoneNumber': phoneNumber, 'isEmailVerified': isEmailVerified, 'regionUuid': regionUuid, 'isAgreementAccepted': isAgreementAccepted, 'regionName': regionName, 'timeZone': timeZone, 'hasAcceptedWebAgreement': hasAcceptedWebAgreement, 'webAgreementAcceptedAt': webAgreementAcceptedAt?.toIso8601String(), 'hasAcceptedAppAgreement': hasAcceptedAppAgreement, 'appAgreementAcceptedAt': appAgreementAcceptedAt?.toIso8601String(), 'role': role?.toJson(), }; } } class Role { final String? uuid; final DateTime? createdAt; final DateTime? updatedAt; final String? type; Role({ required this.uuid, required this.createdAt, required this.updatedAt, required this.type, }); factory Role.fromJson(Map json) { return Role( uuid: json['uuid'], createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null, updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null, type: json['type'], ); } Map toJson() { return { 'uuid': uuid, 'createdAt': createdAt?.toIso8601String(), 'updatedAt': updatedAt?.toIso8601String(), 'type': type, }; } }