/**
 * Use Case: RequestAvatarGenerationUseCase
 *
 * Handles the business logic for requesting avatar generation from a face photo.
 */

import { v4 as uuidv4 } from 'uuid';
import type { IAvatarGenerationRepository } from '../../domain/repositories/IAvatarGenerationRepository';
import type { FaceValidationPort } from '../ports/FaceValidationPort';
import type { AvatarGenerationPort } from '../ports/AvatarGenerationPort';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { AvatarGenerationRequest } from '../../domain/entities/AvatarGenerationRequest';
import type { RacingSuitColor } from '../../domain/types/AvatarGenerationRequest';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

export interface RequestAvatarGenerationInput {
  userId: string;
  facePhotoData: string;
  suitColor: RacingSuitColor;
  style?: 'realistic' | 'cartoon' | 'pixel-art';
}

export interface RequestAvatarGenerationResult {
  requestId: string;
  status: 'validating' | 'generating' | 'completed';
  avatarUrls?: string[];
}

export type RequestAvatarGenerationErrorCode =
  | 'FACE_VALIDATION_FAILED'
  | 'GENERATION_FAILED'
  | 'REPOSITORY_ERROR';

export type RequestAvatarGenerationApplicationError = ApplicationErrorCode<
  RequestAvatarGenerationErrorCode,
  { message: string }
>;

export class RequestAvatarGenerationUseCase {
  constructor(
    private readonly avatarRepo: IAvatarGenerationRepository,
    private readonly faceValidation: FaceValidationPort,
    private readonly avatarGeneration: AvatarGenerationPort,
    private readonly output: UseCaseOutputPort<RequestAvatarGenerationResult>,
    private readonly logger: Logger,
  ) {}

  async execute(
    input: RequestAvatarGenerationInput,
  ): Promise<Result<void, RequestAvatarGenerationApplicationError>> {
    this.logger.info('[RequestAvatarGenerationUseCase] Starting avatar generation request', {
      userId: input.userId,
      suitColor: input.suitColor,
    });

    try {
      const requestId = uuidv4();
      const requestProps: Parameters<typeof AvatarGenerationRequest.create>[0] = {
        id: requestId,
        userId: input.userId,
        facePhotoUrl: input.facePhotoData,
        suitColor: input.suitColor,
      };
      
      if (input.style !== undefined) {
        requestProps.style = input.style;
      }
      
      const request = AvatarGenerationRequest.create(requestProps);

      await this.avatarRepo.save(request);

      request.markAsValidating();
      await this.avatarRepo.save(request);

      const validationResult = await this.faceValidation.validateFacePhoto(input.facePhotoData);

      if (!validationResult.isValid || !validationResult.hasFace || validationResult.faceCount !== 1) {
        const errorMessage =
          validationResult.errorMessage || 'Invalid face photo: must contain exactly one face';
        request.fail(errorMessage);
        await this.avatarRepo.save(request);

        return Result.err<void, RequestAvatarGenerationApplicationError>({
          code: 'FACE_VALIDATION_FAILED',
          details: { message: errorMessage },
        });
      }

      request.markAsGenerating();
      await this.avatarRepo.save(request);

      const generationOptions = {
        facePhotoUrl: input.facePhotoData,
        prompt: request.buildPrompt(),
        suitColor: input.suitColor,
        style: input.style || 'realistic',
        count: 3,
      };

      const generationResult = await this.avatarGeneration.generateAvatars(generationOptions);

      if (!generationResult.success) {
        const errorMessage = generationResult.errorMessage || 'Failed to generate avatars';
        request.fail(errorMessage);
        await this.avatarRepo.save(request);

        return Result.err<void, RequestAvatarGenerationApplicationError>({
          code: 'GENERATION_FAILED',
          details: { message: errorMessage },
        });
      }

      const avatarUrls = generationResult.avatars.map(avatar => avatar.url);
      request.completeWithAvatars(avatarUrls);
      await this.avatarRepo.save(request);

      this.output.present({
        requestId,
        status: 'completed',
        avatarUrls,
      });

      this.logger.info('[RequestAvatarGenerationUseCase] Avatar generation completed', {
        requestId,
        userId: input.userId,
        avatarCount: avatarUrls.length,
      });

      return Result.ok(undefined);
    } catch (error) {
      const err = error instanceof Error ? error : new Error(String(error));

      this.logger.error('[RequestAvatarGenerationUseCase] Error during avatar generation', err, {
        userId: input.userId,
      });

      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: { message: err.message ?? 'Internal error occurred during avatar generation' },
      });
    }
  }
}