import { type Correlation, type CorrelationSpec } from '@grafana/api-clients/rtkq/correlations/v0alpha1';

export const generateCorrMetadata = (uid: string, correlation: CorrelationSpec) => {
  let labels: Record<string, string> = {
    'correlations.grafana.app/sourceDS-ref': `${correlation.source.group}.${correlation.source.name}`,
  };

  if (correlation.target?.group !== undefined && correlation.target?.name !== undefined) {
    labels['correlations.grafana.app/targetDS-ref'] = `${correlation.target?.group}.${correlation.target?.name}`;
  }

  return {
    kind: 'Correlation',
    apiVersion: 'correlations.grafana.app/v0alpha1',
    metadata: {
      uid: uid,
      name: uid,
      namespace: 'default',
      labels: labels,
    },
    spec: correlation,
  };
};

export const fakeCorrelations: CorrelationSpec[] = [
  {
    source: { group: 'loki', name: 'lokiUID' },
    target: { group: 'loki', name: 'lokiUID' },
    label: 'Loki to Loki',
    type: 'query',
    config: {
      field: 'line',
      target: {},
      transformations: [{ type: 'regex', expression: 'url=http[s]?://(S*)', mapValue: 'path' }],
    },
  },
  {
    source: { group: 'loki', name: 'lokiUID' },
    target: { group: 'prometheus', name: 'prometheusUID' },
    label: 'Loki to Prometheus',
    type: 'query',
    config: {
      field: 'line',
      target: {},
      transformations: [{ type: 'regex', expression: 'url=http[s]?://(S*)', mapValue: 'path' }],
    },
  },
  {
    source: { group: 'prometheus', name: 'prometheusUID' },
    target: { group: 'loki', name: 'lokiUID' },
    label: 'Prometheus to Loki',
    type: 'query',
    config: { field: 'label', target: {} },
  },
  {
    source: { group: 'prometheus', name: 'prometheusUID' },
    target: { group: 'prometheus', name: 'prometheusUID' },
    label: 'Prometheus to Prometheus',
    type: 'query',
    config: { field: 'label', target: {} },
  },
];

export const setupMockCorrelations = () => {
  mockCorrelationsMap.clear();
};

export let mockCorrelationsMap = new Map<string, Correlation>();

export const resetFixtures = () => {
  setupMockCorrelations();
};

export const prePopulateCorrelations = () => {
  fakeCorrelations.forEach((fakeCorr, i) => {
    mockCorrelationsMap.set(i.toString(), generateCorrMetadata(i.toString(), fakeCorr));
  });
};
