import "dotenv/config";
import esClient from "../src/elasticsearch";
import minioClient from "../src/minio";

const DEFAULT_BUCKET = process.env.MINIO_BUCKET;
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;

if (!DEFAULT_BUCKET) throw Error("Default MinIO bucket must be specified.");
if (!DEFAULT_INDEX) throw Error("Default ElasticSearch index must be specified.");

esClient.ingest.putPipeline({
  id: "attachment",
  body: {
    description: "Extract attachment information",
    processors: [
      {
        attachment: {
          field: "data",
        },
      },
      {
        remove: {
          field: "data",
        },
      },
    ],
  },
});

esClient.indices.create({
  index: DEFAULT_INDEX,
  settings: {
    index: {
      analysis: {
        analyzer: {
          analyzer_shingle: {
            type: "custom",
            tokenizer: "icu_tokenizer",
            filter: ["filter_shingle"],
          },
        },
        filter: {
          filter_shingle: {
            type: "shingle",
            max_shingle_size: 3,
            min_shingle_size: 2,
            output_unigrams: true,
          },
        },
      },
    },
  },
  mappings: {
    properties: {
      path: { type: "keyword" },
      pathname: { type: "keyword" },

      fileName: { type: "text", analyzer: "analyzer_shingle" },
      title: { type: "text", analyzer: "analyzer_shingle" },
      description: { type: "text", analyzer: "analyzer_shingle" },

      "attachment.content": { type: "text", analyzer: "analyzer_shingle" },
    },
  },
});

minioClient.makeBucket(DEFAULT_BUCKET!, (e) => {
  if (!e) {
    console.log("Success.");
    console.log("Configuration is required for Bucket Notification to AMQP.");
    console.log("Configuration is required for Keycloak.");
  } else {
    console.error(e);
  }
});
