/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.cassandra.tcm.serialization;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.cassandra.tcm.ClusterMetadata;
import org.apache.cassandra.tcm.membership.NodeVersion;

public enum Version
{
    /**
     * Used for nodes on older, pre-CEP-21 release versions
     */
    OLD(-1),
    /**
     * Initial version
     */
    V0(0),
    /**
     * - Moved Partitioner in ClusterMetadata serializer to be the first field
     * - Added a counter to Directory serializer to keep track of NodeIds
     */
    V1(1),
    /**
     * - Added version to PlacementForRange serializer
     * - Serialize MemtableParams when serializing TableParams
     */
    V2(2),
    /**
     *  - down nodes serialized in PrepareCMSReconfiguration
     */
    V3(3),
    /**
     *  - Serialize allowAutoSnapshot and incrementalBackups when serializing TableParams
     */
    V4(4),
    /**
     *  - AlterSchema includes execution timestamp
     *  - PreInitialize includes datacenter (affects local serialization on first CMS node only)
     */
    V5(5),
    /**
     *  - CEP-42 - Constraints framework. New version due to modifications in table metadata serialization.
     */
    V6(6),
    /**
     *  - Track nodes removed
     *  - Column Metadata now stores a unique id
     *  - Added AccordFastPath
     *  - Added ConsensusMigrationState
     *  - Added AccordStaleReplicas
     *  - TableParam now has pendingDrop (accord table drop is multistep)
     */
    V7(7),

    /**
     *  - Comments and security labels for schema elements (keyspaces, tables, columns, UDTs, and UDT fields)
     */
    V8(8),
    /**
     * - DataPlacements don't include MetaStrategy, replaced by ClusterMetadata.CMSMembership
     * - Size of DataPlacements is encoded as vint
     */
    V9(9),

    UNKNOWN(Integer.MAX_VALUE);

    /**
     * The version that Accord was added to TCM.
     */
    public static final Version MIN_ACCORD_VERSION = V7;

    private static Map<Integer, Version> values = new HashMap<>();

    static
    {
        for (Version v : values())
            values.put(v.version, v);
    }

    private final int version;

    Version(int version)
    {
        this.version = version;
    }

    /**
     * Minimum serialization version known to all nodes in the cluster.
     */
    public static Version minCommonSerializationVersion()
    {
        ClusterMetadata metadata = ClusterMetadata.currentNullable();
        return metadata != null
               ? metadata.directory.commonSerializationVersion
               : NodeVersion.CURRENT.serializationVersion();
    }

    public int asInt()
    {
        return version;
    }

    public boolean equals(Version other)
    {
        return version == other.version;
    }

    public boolean isAtLeast(Version other)
    {
        return version >= other.version;
    }

    public boolean isEqualOrBefore(Version version)
    {
        return this.version <= version.version;
    }

    public boolean isBefore(Version other)
    {
        return version < other.version;
    }

    public boolean isAfter(Version other)
    {
        return version > other.version;
    }

    public static Version fromInt(int i)
    {
        Version v = values.get(i);
        if (v != null)
            return v;

        throw new IllegalArgumentException("Unsupported metadata version (" + i + ")");
    }

    public List<Version> greaterThanOrEqual()
    {
        Version[] all = Version.values();
        if (ordinal() == all.length - 1)
            return Collections.singletonList(this);
        List<Version> values = new ArrayList<>(all.length - ordinal());
        for (int i = ordinal(); i < all.length; i++)
            values.add(all[i]);
        return values;
    }
}
