import * as tempy from 'tempy';
import { modelPackageCloudClient } from './model-package-cloud-client';
import { ModelJson } from '@alwaysai/model-configuration-schemas';
import { ModelPackageJsonFile } from './model-package-json-file';
import { ModelId } from './model-id';
import { RandomString } from '../../util';
import { CliAuthenticationClient, CliRpcClient } from '../../infrastructure';

export async function MockModel() {
  const { username } = await CliAuthenticationClient().getInfo();

  const id = ModelId.serialize({
    publisher: username,
    name: RandomString()
  });

  const dir = tempy.directory();
  const modelJsonFile = ModelPackageJsonFile(dir);
  const json: ModelJson = {
    accuracy: '',
    dataset: '',
    description: RandomString(),
    id,
    inference_time: null,
    license: '',
    mean_average_precision_top_1: null,
    mean_average_precision_top_5: null,
    public: false,
    website_url: '',
    model_parameters: {
      framework_type: 'caffe',
      config_file: 'models/MobileNetSSD_deploy.prototxt.txt',
      size: [300, 300],
      model_file: 'models/MobileNetSSD_deploy.caffemodel',
      label_file: 'labels/pascal_voc.txt',
      scalefactor: 0.007843,
      mean: [127.5, 127.5, 127.5],
      crop: true,
      swaprb: false,
      purpose: 'ObjectDetection'
    }
  };

  modelJsonFile.write(json);

  const spy = jest.spyOn(process, 'cwd');
  spy.mockReturnValue(dir);
  const uuid = await modelPackageCloudClient.publish(undefined);
  const metadata = await CliRpcClient().getModelVersionByUuid(uuid);
  return {
    json,
    dir,
    metadata
  };
}
