import {
  expirationPeriodToDate,
  OrganizationAccessTokens,
} from '../../providers/organization-access-tokens';
import type { MutationResolvers } from './../../../../__generated__/types';

export const createOrganizationAccessToken: NonNullable<
  MutationResolvers['createOrganizationAccessToken']
> = async (_, args, { injector }) => {
  const result = await injector.get(OrganizationAccessTokens).createForOrganization({
    organization: args.input.organization,
    title: args.input.title,
    description: args.input.description ?? null,
    permissions: [...args.input.permissions],
    assignedResources: args.input.resources,
    expiresAt: expirationPeriodToDate(args.input.expirationPeriod ?? 'NEVER'),
  });

  if (result.type === 'success') {
    return {
      ok: {
        __typename: 'CreateOrganizationAccessTokenResultOk',
        createdOrganizationAccessToken: result.accessToken,
        privateAccessKey: result.privateAccessKey,
      },
    };
  }

  return {
    error: {
      __typename: 'CreateOrganizationAccessTokenResultError',
      message: result.message,
      details: result.details,
    },
  };
};
