/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the "Elastic License
 * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
 * Public License v 1"; you may not use this file except in compliance with, at
 * your election, the "Elastic License 2.0", the "GNU Affero General Public
 * License v3.0 only", or the "Server Side Public License, v 1".
 */

package org.elasticsearch.geoip;

import joptsimple.OptionException;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;

@LuceneTestCase.SuppressFileSystems(value = "ExtrasFS") // Don't randomly add 'extra' files to directory.
public class GeoIpCliTests extends CommandTestCase {

    private Path source;
    private Path target;

    public void setUp() throws Exception {
        super.setUp();
        source = createTempDir();
        target = createTempDir();
    }

    public void testNoSource() throws Exception {
        var e = expectThrows(OptionException.class, () -> execute());
        assertThat(e.getMessage(), containsString("Missing required option(s) [s/source]"));
    }

    public void testDifferentDirectories() throws Exception {
        Map<String, byte[]> data = createTestFiles(source);

        execute("-t", target.toAbsolutePath().toString(), "-s", source.toAbsolutePath().toString());

        try (Stream<Path> list = Files.list(source)) {
            List<String> files = list.map(p -> p.getFileName().toString()).collect(Collectors.toList());
            assertThat(files, containsInAnyOrder("a.mmdb", "b.mmdb", "c.tgz"));
        }

        try (Stream<Path> list = Files.list(target)) {
            List<String> files = list.map(p -> p.getFileName().toString()).collect(Collectors.toList());
            assertThat(files, containsInAnyOrder("a.tgz", "b.tgz", "c.tgz", "overview.json"));
        }

        verifyTarball(data);
        verifyOverview();
    }

    public void testSameDirectory() throws Exception {
        Map<String, byte[]> data = createTestFiles(target);
        execute("-s", target.toAbsolutePath().toString());

        try (Stream<Path> list = Files.list(target)) {
            List<String> files = list.map(p -> p.getFileName().toString()).collect(Collectors.toList());
            assertThat(files, containsInAnyOrder("a.mmdb", "b.mmdb", "a.tgz", "b.tgz", "c.tgz", "overview.json"));
        }

        Files.delete(target.resolve("a.mmdb"));
        Files.delete(target.resolve("b.mmdb"));

        verifyTarball(data);
        verifyOverview();
    }

    private void verifyOverview() throws Exception {
        byte[] data = Files.readAllBytes(target.resolve("overview.json"));
        try (XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, data)) {
            @SuppressWarnings({ "unchecked" })
            List<Map<String, String>> list = (List) parser.list();
            assertThat(list, containsInAnyOrder(hasEntry("name", "a.tgz"), hasEntry("name", "b.tgz"), hasEntry("name", "c.tgz")));
            assertThat(list, containsInAnyOrder(hasEntry("url", "a.tgz"), hasEntry("url", "b.tgz"), hasEntry("url", "c.tgz")));
            for (Map<String, String> map : list) {
                assertThat(map, hasKey("md5_hash"));
                assertThat(map, hasKey("updated"));
            }
        }
    }

    private void verifyTarball(Map<String, byte[]> data) throws Exception {
        for (String tgz : List.of("a.tgz", "b.tgz")) {
            try (
                TarArchiveInputStream tis = new TarArchiveInputStream(new GZIPInputStream(Files.newInputStream(target.resolve(tgz)), 8192))
            ) {
                TarArchiveEntry entry = tis.getNextTarEntry();
                assertNotNull(entry);
                assertTrue(entry.isFile());
                byte[] bytes = data.get(tgz);
                assertEquals(tgz.replace(".tgz", ".mmdb"), entry.getName());
                assertEquals(bytes.length, entry.getSize());
                assertArrayEquals(bytes, tis.readAllBytes());
                assertEquals(1000, entry.getLongUserId());
                assertEquals(1000, entry.getLongGroupId());
                assertEquals(420, entry.getMode()); // 644oct=420dec

                assertNull(tis.getNextTarEntry());
            }
        }
    }

    private Map<String, byte[]> createTestFiles(Path dir) throws IOException {
        Map<String, byte[]> data = new HashMap<>();

        byte[] a = new byte[514];
        Arrays.fill(a, (byte) 'a');
        Files.write(dir.resolve("a.mmdb"), a);
        data.put("a.tgz", a);

        byte[] b = new byte[100];
        Arrays.fill(b, (byte) 'b');
        Files.write(dir.resolve("b.mmdb"), b);
        data.put("b.tgz", b);

        Files.createFile(dir.resolve("c.tgz"));

        return data;
    }

    @Override
    protected Command newCommand() {
        return new GeoIpCli();
    }
}
