/*
 * Copyright (c) nosqlbench
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.nosqlbench.nb.mql.testdata;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Utility for loading test databases from src/test/resources/testdata/.
 * Provides easy access to pre-generated test databases for unit and integration tests.
 */
public class TestDatabaseLoader {

    private static final String TEST_DATA_DIR = "testdata";

    /**
     * Available test databases with their purposes.
     */
    public enum TestDatabase {
        SIMPLE_COUNTER("simple_counter.db", "Basic counters for instant/rate queries"),
        MULTI_DIMENSIONAL("multi_dimensional.db", "Complex 4D labels for aggregations"),
        LATENCY_TIMERS("latency_timers.db", "Timer metrics with quantiles"),
        RATE_CALCULATIONS("rate_calculations.db", "Predictable growth patterns for rate testing");

        private final String filename;
        private final String description;

        TestDatabase(String filename, String description) {
            this.filename = filename;
            this.description = description;
        }

        public String getFilename() {
            return filename;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * Get the path to a test database by enum.
     *
     * @param database The test database enum
     * @return Path to the database file
     * @throws IOException If the database file cannot be found
     */
    public static Path getDatabase(TestDatabase database) throws IOException {
        return getDatabase(database.getFilename());
    }

    /**
     * Get the path to a test database by filename.
     *
     * @param filename Name of the database file (e.g., "simple_counter.db")
     * @return Path to the database file
     * @throws IOException If the database file cannot be found
     */
    public static Path getDatabase(String filename) throws IOException {
        String resourcePath = TEST_DATA_DIR + "/" + filename;
        URL resource = TestDatabaseLoader.class.getClassLoader().getResource(resourcePath);

        if (resource == null) {
            throw new IOException(
                "Test database not found: " + resourcePath + "\n" +
                "Make sure test databases have been generated by running TestDatabaseGenerator.main()"
            );
        }

        try {
            return Paths.get(resource.toURI());
        } catch (URISyntaxException e) {
            throw new IOException("Invalid URI for test database: " + resourcePath, e);
        }
    }

    /**
     * Check if a test database exists.
     *
     * @param database The test database enum
     * @return true if the database file exists
     */
    public static boolean exists(TestDatabase database) {
        try {
            getDatabase(database);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    /**
     * Get a descriptive message about available test databases.
     *
     * @return Multi-line string describing all test databases
     */
    public static String getAvailableDatabases() {
        StringBuilder sb = new StringBuilder("Available test databases:\n");
        for (TestDatabase db : TestDatabase.values()) {
            sb.append(String.format("  - %s: %s\n",
                db.getFilename(),
                db.getDescription()));
        }
        return sb.toString();
    }

    private TestDatabaseLoader() {
        // Utility class - prevent instantiation
    }
}
