import { v4 as uuidv4 } from 'uuid';
import { Network } from '../../../domain/entities/network';
import { INetworkRepository } from '../../../domain/repositories/network-repository';
import { IImageStorage } from '../../../application/ports/image-storage';
import { CreateNetworkInput } from '../../dto/admin-network.dto';

export class CreateNetworkUseCase {
  constructor(
    private readonly networkRepository: INetworkRepository,
    private readonly imageStorage: IImageStorage,
  ) {}

  async execute(input: CreateNetworkInput): Promise<Network> {
    const id = uuidv4();
    const thumbnail = input.thumbnail
      ? await this.imageStorage.downloadAndStore('network', id, input.thumbnail, {
          name: input.name,
          symbol: input.code,
        })
      : null;

    const network = Network.create(
      id,
      input.name,
      input.code,
      input.addressType,
      thumbnail,
      input.explorer,
      input.chainId ?? null,
      input.status,
      input.mcap ?? null,
    );

    return this.networkRepository.save(network);
  }
}
