import 'package:flutter_test/flutter_test.dart';
import 'package:tdc114plus/src/features/auth/domain/auth_models.dart';

void main() {
  test('PhoneLoginRequest serializes phone login payload', () {
    const request = PhoneLoginRequest(
      phoneNumber: '01012345678',
      device: LoginDeviceInfo(
        platform: 'android',
        appVersion: '0.1.0',
        deviceName: 'Pixel 8',
      ),
    );

    expect(request.toJson(), {
      'phoneNumber': '01012345678',
      'device': {
        'platform': 'android',
        'appVersion': '0.1.0',
        'deviceName': 'Pixel 8',
      },
    });
  });

  test('PhoneLoginResponse parses session token response', () {
    final response = PhoneLoginResponse.fromJson({
      'status': 'ok',
      'token': 'baron-sso-session-token',
      'expiresAt': '2026-07-02T12:00:00Z',
      'user': {
        'id': 'user-uuid',
        'name': 'User One',
        'phoneNumber': '+821012345678',
        'tenantId': 'tenant-uuid',
        'tenantName': 'Hanmac',
        'tenantSlug': 'hanmac',
        'department': 'Engineering',
        'grade': 'Lead',
        'position': 'Manager',
        'jobTitle': 'Developer',
      },
    });

    expect(response.status, 'ok');
    expect(response.token, 'baron-sso-session-token');
    expect(
      response.expiresAt.toUtc().toIso8601String(),
      '2026-07-02T12:00:00.000Z',
    );
    expect(response.user.tenantSlug, 'hanmac');
  });

  test('PhoneLoginLinkInitResponse parses pending auth response', () {
    final response = PhoneLoginLinkInitResponse.fromJson({
      'status': 'pending',
      'pendingRef': 'pending-ref',
      'expiresIn': 180,
      'interval': 3,
      'resendAfter': 30,
      'provider': 'Ory (Kratos/Hydra)',
    });

    expect(response.pendingRef, 'pending-ref');
    expect(response.interval, 3);
    expect(response.provider, 'Ory (Kratos/Hydra)');
  });

  test('PhoneLoginLinkPollResponse parses completed session', () {
    final response = PhoneLoginLinkPollResponse.fromJson({
      'status': 'ok',
      'session': {
        'status': 'ok',
        'token': 'baron-sso-session-token',
        'expiresAt': '2026-07-02T12:00:00Z',
        'user': {
          'id': 'user-uuid',
          'name': 'User One',
          'phoneNumber': '+821012345678',
          'tenantId': 'tenant-uuid',
          'tenantName': 'Hanmac',
          'tenantSlug': 'hanmac',
        },
      },
    });

    expect(response.session?.token, 'baron-sso-session-token');
    expect(response.isPending, isFalse);
  });

  test('PhoneLoginLinkPollResponse detects pending response', () {
    final response = PhoneLoginLinkPollResponse.fromJson({
      'status': 'pending',
      'code': 'authorization_pending',
      'interval': 5,
    });

    expect(response.isPending, isTrue);
    expect(response.interval, 5);
  });

  test('CurrentUser parses permissions and can map to employee', () {
    final user = CurrentUser.fromJson({
      'id': 'user-uuid',
      'name': 'User One',
      'phoneNumber': '+821012345678',
      'email': 'user@example.com',
      'tenantId': 'tenant-uuid',
      'tenantName': 'Hanmac',
      'tenantSlug': 'hanmac',
      'department': 'Engineering',
      'permissions': {
        'directory': true,
        'organization': true,
        'favoritesSync': false,
      },
    });

    expect(user.permissions.directory, isTrue);
    expect(user.permissions.favoritesSync, isFalse);
    expect(user.toEmployee().department, 'Engineering');
  });
}
