import { Provider } from '@nestjs/common';
import { MediaService } from './MediaService';

// Import core interfaces
import { IAvatarGenerationRepository } from '@core/media/domain/repositories/IAvatarGenerationRepository';
import { FaceValidationPort } from '@core/media/application/ports/FaceValidationPort';
import { AvatarGenerationPort } from '@core/media/application/ports/AvatarGenerationPort';
import type { Logger } from '@core/shared/application';

// Import use cases
import { RequestAvatarGenerationUseCase } from '@core/media/application/use-cases/RequestAvatarGenerationUseCase';

// Define injection tokens
export const AVATAR_GENERATION_REPOSITORY_TOKEN = 'IAvatarGenerationRepository';
export const FACE_VALIDATION_PORT_TOKEN = 'FaceValidationPort';
export const AVATAR_GENERATION_PORT_TOKEN = 'AvatarGenerationPort';
export const LOGGER_TOKEN = 'Logger';

// Use case tokens
export const REQUEST_AVATAR_GENERATION_USE_CASE_TOKEN = 'RequestAvatarGenerationUseCase';

// Mock implementations
class MockAvatarGenerationRepository implements IAvatarGenerationRepository {
  async save(_request: any): Promise<void> {}
  async findById(_id: string): Promise<any | null> { return null; }
  async findByUserId(_userId: string): Promise<any[]> { return []; }
  async findLatestByUserId(_userId: string): Promise<any | null> { return null; }
  async delete(_id: string): Promise<void> {}
}

class MockFaceValidationAdapter implements FaceValidationPort {
  async validateFacePhoto(data: string): Promise<any> {
    return { isValid: true, hasFace: true, faceCount: 1 };
  }
}

class MockAvatarGenerationAdapter implements AvatarGenerationPort {
  async generateAvatars(options: any): Promise<any> {
    return {
      success: true,
      avatars: [
        { url: 'https://cdn.example.com/avatars/mock-avatar-1.png' },
        { url: 'https://cdn.example.com/avatars/mock-avatar-2.png' },
        { url: 'https://cdn.example.com/avatars/mock-avatar-3.png' },
      ],
    };
  }
}

class MockLogger implements Logger {
  debug(message: string, meta?: any): void {}
  info(message: string, meta?: any): void {}
  warn(message: string, meta?: any): void {}
  error(message: string, error?: Error): void {}
}

export const MediaProviders: Provider[] = [
  MediaService, // Provide the service itself
  {
    provide: AVATAR_GENERATION_REPOSITORY_TOKEN,
    useClass: MockAvatarGenerationRepository,
  },
  {
    provide: FACE_VALIDATION_PORT_TOKEN,
    useClass: MockFaceValidationAdapter,
  },
  {
    provide: AVATAR_GENERATION_PORT_TOKEN,
    useClass: MockAvatarGenerationAdapter,
  },
  {
    provide: LOGGER_TOKEN,
    useClass: MockLogger,
  },
  // Use cases
  {
    provide: REQUEST_AVATAR_GENERATION_USE_CASE_TOKEN,
    useFactory: (avatarRepo: IAvatarGenerationRepository, faceValidation: FaceValidationPort, avatarGeneration: AvatarGenerationPort, logger: Logger) =>
      new RequestAvatarGenerationUseCase(avatarRepo, faceValidation, avatarGeneration, logger),
    inject: [AVATAR_GENERATION_REPOSITORY_TOKEN, FACE_VALIDATION_PORT_TOKEN, AVATAR_GENERATION_PORT_TOKEN, LOGGER_TOKEN],
  },
];
