import { createCustomApiCallAction } from '@activepieces/pieces-common';
import {
  createPiece,
  OAuth2PropertyValue,
} from '@activepieces/pieces-framework';
import { PieceCategory } from '@activepieces/pieces-framework';
import { createRecordAction } from './lib/actions/create-record.action';
import { deleteRecordAction } from './lib/actions/delete-record.action';
import { getRecordAction } from './lib/actions/get-record.action';
import { searchRecordsAction } from './lib/actions/search-records.action';
import { updateRecordAction } from './lib/actions/update-record.action';
import { businessCentralAuth } from './lib/auth';
import { newOrUpdatedRecordTrigger } from './lib/triggers/new-or-updated-record.trigger';

export const microsoftDynamics365BusinessCentral = createPiece({
  displayName: 'Microsoft Dynamics 365 Business Central',
  auth: businessCentralAuth,
  description: 'All-in-one business management solution by Microsoft.',
  categories: [PieceCategory.SALES_AND_CRM],
  minimumSupportedRelease: '0.27.1',
  logoUrl:
    'https://cdn.activepieces.com/pieces/microsoft-dynamics-365-business-central.png',
  authors: ['kishanprmr'],
  actions: [
    createRecordAction,
    deleteRecordAction,
    getRecordAction,
    updateRecordAction,
    searchRecordsAction,
    createCustomApiCallAction({
      auth: businessCentralAuth,
      baseUrl: (auth) => {
        const authValue = auth as OAuth2PropertyValue;
        const cloud = authValue.props?.['cloud'] as string | undefined;
        const host = cloud === 'login.microsoftonline.us'
          ? 'api.businesscentral.dynamics.us'
          : 'api.businesscentral.dynamics.com';
        return `https://${host}/v2.0/${authValue.props?.['environment']}/api/v2.0`;
      },
      authMapping: async (auth) => ({
        Authorization: `Bearer  ${(auth as OAuth2PropertyValue).access_token}`,
      }),
    }),
  ],
  triggers: [newOrUpdatedRecordTrigger],
});
