import { AvatarGenerationRequest } from '@core/media/domain/entities/AvatarGenerationRequest';
import { AvatarGenerationRequestOrmEntity } from '../entities/AvatarGenerationRequestOrmEntity';
import { TypeOrmMediaSchemaError } from '../errors/TypeOrmMediaSchemaError';
import {
  assertNonEmptyString,
  assertDate,
  assertStringArray,
  assertOptionalIntegerOrNull,
  assertOptionalStringOrNull,
  assertRacingSuitColor,
  assertAvatarStyle,
  assertAvatarGenerationStatus,
} from '../schema/TypeOrmMediaSchemaGuards';

export class AvatarGenerationRequestOrmMapper {
  toDomain(entity: AvatarGenerationRequestOrmEntity): AvatarGenerationRequest {
    const entityName = 'AvatarGenerationRequest';

    try {
      assertNonEmptyString(entityName, 'id', entity.id);
      assertNonEmptyString(entityName, 'userId', entity.userId);
      assertNonEmptyString(entityName, 'facePhotoUrl', entity.facePhotoUrl);
      assertRacingSuitColor(entityName, 'suitColor', entity.suitColor);
      assertAvatarStyle(entityName, 'style', entity.style);
      assertAvatarGenerationStatus(entityName, 'status', entity.status);
      assertStringArray(entityName, 'generatedAvatarUrls', entity.generatedAvatarUrls);
      assertOptionalIntegerOrNull(entityName, 'selectedAvatarIndex', entity.selectedAvatarIndex);
      assertOptionalStringOrNull(entityName, 'errorMessage', entity.errorMessage);
      assertDate(entityName, 'createdAt', entity.createdAt);
      assertDate(entityName, 'updatedAt', entity.updatedAt);
    } catch (error) {
      if (error instanceof TypeOrmMediaSchemaError) {
        throw error;
      }
      const message = error instanceof Error ? error.message : 'Invalid persisted AvatarGenerationRequest';
      throw new TypeOrmMediaSchemaError({ entityName, fieldName: 'unknown', reason: 'invalid_shape', message });
    }

    try {
      const props: any = {
        id: entity.id,
        userId: entity.userId,
        facePhotoUrl: entity.facePhotoUrl,
        suitColor: entity.suitColor,
        style: entity.style,
        status: entity.status,
        generatedAvatarUrls: entity.generatedAvatarUrls,
        createdAt: entity.createdAt,
        updatedAt: entity.updatedAt,
      };

      if (entity.selectedAvatarIndex !== null && entity.selectedAvatarIndex !== undefined) {
        props.selectedAvatarIndex = entity.selectedAvatarIndex;
      }

      if (entity.errorMessage !== null && entity.errorMessage !== undefined) {
        props.errorMessage = entity.errorMessage;
      }

      return AvatarGenerationRequest.reconstitute(props);
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Invalid persisted AvatarGenerationRequest';
      throw new TypeOrmMediaSchemaError({ entityName, fieldName: 'unknown', reason: 'invalid_shape', message });
    }
  }

  toOrmEntity(request: AvatarGenerationRequest): AvatarGenerationRequestOrmEntity {
    const entity = new AvatarGenerationRequestOrmEntity();
    const props = request.toProps();

    entity.id = props.id;
    entity.userId = props.userId;
    entity.facePhotoUrl = props.facePhotoUrl;
    entity.suitColor = props.suitColor;
    entity.style = props.style;
    entity.status = props.status;
    entity.generatedAvatarUrls = props.generatedAvatarUrls;
    entity.selectedAvatarIndex = props.selectedAvatarIndex ?? null;
    entity.errorMessage = props.errorMessage ?? null;
    entity.createdAt = props.createdAt;
    entity.updatedAt = props.updatedAt;

    return entity;
  }

  toStored(entity: AvatarGenerationRequestOrmEntity): AvatarGenerationRequest {
    return this.toDomain(entity);
  }
}
