import 'package:flutter_test/flutter_test.dart';
import 'package:agenda_tasks/features/auth/data/models/user_model.dart';
import 'package:agenda_tasks/features/auth/domain/entities/user_entity.dart';

void main() {
  group('UserModel', () {
    final testCreatedAt = DateTime(2026, 1, 1, 10, 30, 0);

    group('fromJson', () {
      test('should parse complete JSON correctly', () {
        final json = {
          'id': 'user-123',
          'email': 'test@example.com',
          'name': 'Test User',
          'created_at': '2026-01-01T10:30:00.000',
        };

        final user = UserModel.fromJson(json);

        expect(user.id, 'user-123');
        expect(user.email, 'test@example.com');
        expect(user.name, 'Test User');
        expect(user.createdAt, testCreatedAt);
      });

      test('should parse JSON without created_at', () {
        final json = {
          'id': 'user-123',
          'email': 'test@example.com',
          'name': 'Test User',
        };

        final user = UserModel.fromJson(json);

        expect(user.id, 'user-123');
        expect(user.email, 'test@example.com');
        expect(user.name, 'Test User');
        expect(user.createdAt, isNull);
      });

      test('should parse JSON with null created_at', () {
        final json = {
          'id': 'user-123',
          'email': 'test@example.com',
          'name': 'Test User',
          'created_at': null,
        };

        final user = UserModel.fromJson(json);

        expect(user.createdAt, isNull);
      });
    });

    group('toJson', () {
      test('should serialize to JSON correctly with all fields', () {
        final user = UserModel(
          id: 'user-123',
          email: 'test@example.com',
          name: 'Test User',
          createdAt: testCreatedAt,
        );

        final json = user.toJson();

        expect(json['id'], 'user-123');
        expect(json['email'], 'test@example.com');
        expect(json['name'], 'Test User');
        expect(json['created_at'], isNotNull);
      });

      test('should serialize to JSON with null created_at', () {
        const user = UserModel(
          id: 'user-123',
          email: 'test@example.com',
          name: 'Test User',
        );

        final json = user.toJson();

        expect(json['id'], 'user-123');
        expect(json['email'], 'test@example.com');
        expect(json['name'], 'Test User');
        expect(json['created_at'], isNull);
      });
    });

    group('fromEntity', () {
      test('should create UserModel from UserEntity with all fields', () {
        final entity = UserEntity(
          id: 'entity-123',
          email: 'entity@example.com',
          name: 'Entity User',
          createdAt: testCreatedAt,
        );

        final model = UserModel.fromEntity(entity);

        expect(model.id, entity.id);
        expect(model.email, entity.email);
        expect(model.name, entity.name);
        expect(model.createdAt, entity.createdAt);
      });

      test('should create UserModel from UserEntity without createdAt', () {
        const entity = UserEntity(
          id: 'entity-123',
          email: 'entity@example.com',
          name: 'Entity User',
        );

        final model = UserModel.fromEntity(entity);

        expect(model.id, entity.id);
        expect(model.email, entity.email);
        expect(model.name, entity.name);
        expect(model.createdAt, isNull);
      });
    });

    group('round-trip serialization', () {
      test('should maintain data after fromJson -> toJson', () {
        final originalJson = {
          'id': 'user-123',
          'email': 'test@example.com',
          'name': 'Test User',
        };

        final user = UserModel.fromJson(originalJson);
        final resultJson = user.toJson();

        expect(resultJson['id'], originalJson['id']);
        expect(resultJson['email'], originalJson['email']);
        expect(resultJson['name'], originalJson['name']);
      });
    });
  });
}
