import { AdminUser } from '@core/admin/domain/entities/AdminUser';
import { AdminUserOrmEntity } from '../entities/AdminUserOrmEntity';
import { TypeOrmAdminSchemaError } from '../errors/TypeOrmAdminSchemaError';
import {
    assertDate,
    assertNonEmptyString,
    assertOptionalDate,
    assertOptionalString,
    assertStringArray,
} from '../schema/TypeOrmAdminSchemaGuards';

export class AdminUserOrmMapper {
  toDomain(entity: AdminUserOrmEntity): AdminUser {
    const entityName = 'AdminUser';

    try {
      assertNonEmptyString(entityName, 'id', entity.id);
      assertNonEmptyString(entityName, 'email', entity.email);
      assertNonEmptyString(entityName, 'displayName', entity.displayName);
      assertStringArray(entityName, 'roles', entity.roles);
      assertNonEmptyString(entityName, 'status', entity.status);
      assertOptionalString(entityName, 'primaryDriverId', entity.primaryDriverId);
      assertOptionalDate(entityName, 'lastLoginAt', entity.lastLoginAt);
      assertDate(entityName, 'createdAt', entity.createdAt);
      assertDate(entityName, 'updatedAt', entity.updatedAt);
    } catch (error) {
      if (error instanceof TypeOrmAdminSchemaError) {
        throw error;
      }
      const message = error instanceof Error ? error.message : 'Invalid persisted AdminUser';
      throw new TypeOrmAdminSchemaError({ entityName, fieldName: 'unknown', reason: 'invalid_shape', message });
    }

    try {
      const domainProps: {
        id: string;
        email: string;
        displayName: string;
        roles: string[];
        status: string;
        createdAt: Date;
        updatedAt: Date;
        primaryDriverId?: string;
        lastLoginAt?: Date;
      } = {
        id: entity.id,
        email: entity.email,
        displayName: entity.displayName,
        roles: entity.roles,
        status: entity.status,
        createdAt: entity.createdAt,
        updatedAt: entity.updatedAt,
      };

      if (entity.primaryDriverId !== null && entity.primaryDriverId !== undefined) {
        domainProps.primaryDriverId = entity.primaryDriverId;
      }

      if (entity.lastLoginAt !== null && entity.lastLoginAt !== undefined) {
        domainProps.lastLoginAt = entity.lastLoginAt;
      }

      return AdminUser.create(domainProps);
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Invalid persisted AdminUser';
      throw new TypeOrmAdminSchemaError({ entityName, fieldName: 'unknown', reason: 'invalid_shape', message });
    }
  }

  toOrmEntity(adminUser: AdminUser): AdminUserOrmEntity {
    const entity = new AdminUserOrmEntity();

    entity.id = adminUser.id.value;
    entity.email = adminUser.email.value;
    entity.displayName = adminUser.displayName;
    entity.roles = adminUser.roles.map(r => r.value);
    entity.status = adminUser.status.value;
    entity.createdAt = adminUser.createdAt;
    entity.updatedAt = adminUser.updatedAt;

    if (adminUser.primaryDriverId !== undefined) {
      entity.primaryDriverId = adminUser.primaryDriverId;
    }

    if (adminUser.lastLoginAt !== undefined) {
      entity.lastLoginAt = adminUser.lastLoginAt;
    }

    return entity;
  }

  toStored(entity: AdminUserOrmEntity): AdminUser {
    return this.toDomain(entity);
  }
}