import {
  AttachmentClassification,
  CreateAttachmentInputData,
  CreateQrAttachmentInputData,
  DEFAULT_APP_ID,
  DeviceTypeEnum,
  GeolocationData,
  makeSampleGeolocation,
} from '../../common';
import { CreateAttachmentsPayload, CreateQrAttachmentRequest } from '../interfaces';

interface MakeCreateAttachmentRequestOpts {
  appId?: string;
  eventId: string;
  deviceId?: string;
  deviceName?: string;
  deviceType?: DeviceTypeEnum;
  geolocation?: GeolocationData;
  ipAddress?: string;
  docs?: CreateAttachmentInputData[];
  chainType?: string;
}

// This is a helper function that constructs the proper Asset Anchoring Attachment input
export function makeCreateAttachmentsRequest(opts: MakeCreateAttachmentRequestOpts): CreateAttachmentsPayload {
  return {
    eventId: opts.eventId,
    req: {
      application: { id: opts.appId ?? DEFAULT_APP_ID },
      docs: opts.docs ?? [],
      eventId: opts.eventId,
      device: {
        date: new Date().toISOString(),
        deviceId: opts?.deviceId ?? '',
        deviceName: opts?.deviceName ?? '',
        geolocation: opts?.geolocation ?? makeSampleGeolocation(),
        ipAddress: opts?.ipAddress ?? '',
        deviceType: opts?.deviceType ?? DeviceTypeEnum.PHONE,
      },
    },
    chainType: opts.chainType ?? '',
  };
}

interface MakeCreateQrAttachmentRequestOpts {
  qrImageBase64: string;
  appId?: string;
  eventId: string;
  deviceId?: string;
  deviceName?: string;
  deviceType?: DeviceTypeEnum;
  geolocation?: GeolocationData;
  ipAddress?: string;
  docs?: CreateQrAttachmentInputData[];
}

export function makeCreateQrAttachmentRequest(opts: MakeCreateQrAttachmentRequestOpts): CreateQrAttachmentRequest {
  return {
    application: { id: opts?.appId ?? DEFAULT_APP_ID },
    device: {
      date: new Date().toISOString(),
      deviceId: opts.deviceId ?? '',
      deviceName: opts.deviceName ?? '',
      deviceType: opts.deviceType ?? DeviceTypeEnum.PHONE,
      geolocation: opts.geolocation ?? makeSampleGeolocation(),
      ipAddress: opts?.ipAddress ?? '',
    },
    docs: [
      {
        docType: 'PHOTO',
        classification: AttachmentClassification.QrCode,
        data: opts.qrImageBase64,
      },
    ],
  };
}
