import grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const protoPath = path.resolve(__dirname, '../../../shared/proto/security.proto');
const grpcUrl = process.env.SSO_CORE_GRPC_URL ?? 'localhost:50051';
const target = process.argv[2] ?? '+79591913202';

const packageDefinition = protoLoader.loadSync(protoPath, {
  keepCase: false,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
  includeDirs: [path.resolve(__dirname, '../../../shared/proto')]
});

const proto = grpc.loadPackageDefinition(packageDefinition);
const client = new proto.security.SettingsService(
  grpcUrl,
  grpc.credentials.createInsecure()
);

client.TestMessagingDelivery(
  { channel: 'sms', target },
  (error, response) => {
    if (error) {
      console.error('gRPC error:', error.message);
      process.exit(1);
    }
    console.log('Response:', response);
    process.exit(0);
  }
);
