import { type DataSourceSettings, LocalStorageValueProvider } from '@grafana/data';
import { Trans, t } from '@grafana/i18n';
import { Alert, TextLink } from '@grafana/ui';
import { isOpenSourceBuildOrUnlicenced } from 'app/features/admin/EnterpriseAuthFeaturesCard';

const LOCAL_STORAGE_KEY = 'datasources.settings.cloudInfoBox.isDismissed';

export interface Props {
  dataSource: DataSourceSettings;
}

export function CloudInfoBox({ dataSource }: Props) {
  let dsName = '';

  // don't show for already configured data sources or provisioned data sources
  if (dataSource.readOnly || (dataSource.version ?? 0) > 2) {
    return null;
  }

  // Skip showing this info box in some editions
  if (!isOpenSourceBuildOrUnlicenced()) {
    return null;
  }

  switch (dataSource.type) {
    case 'prometheus':
      dsName = 'Prometheus';
      break;
    case 'loki':
      dsName = 'Loki';
      break;
    case 'tempo':
      dsName = 'Tempo';
      break;
    case 'grafana-pyroscope-datasource':
      dsName = 'Pyroscope';
      break;
    default:
      return null;
  }

  return (
    <LocalStorageValueProvider<boolean> storageKey={LOCAL_STORAGE_KEY} defaultValue={false}>
      {(isDismissed, onDismiss) => {
        if (isDismissed) {
          return null;
        }
        return (
          <Alert
            title={t('datasources.cloud-info-box.title-alert', 'Configure your {{dsName}} data source below', {
              dsName,
            })}
            severity="info"
            bottomSpacing={4}
            onRemove={() => {
              onDismiss(true);
            }}
          >
            <Trans i18nKey="datasources.cloud-info-box.body-alert">
              Or skip the effort and get {{ dsName }} as fully-managed, scalable, and hosted data source from Grafana
              Labs with the{' '}
              <TextLink
                href={`https://grafana.com/signup/cloud/connect-account?src=grafana-oss&cnt=${dataSource.type}-settings`}
                external
              >
                free-forever Grafana Cloud plan
              </TextLink>
              .
            </Trans>
          </Alert>
        );
      }}
    </LocalStorageValueProvider>
  );
}
