// Copyright 2024 RustFS Team
//
// 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.

use std::collections::HashMap;

use crate::error::{Error, Result, StorageError, is_err_object_not_found, is_err_version_not_found};
use crate::object_api::{ObjectInfo, ObjectOptions};
use rustfs_utils::http::metadata_compat::{
    SUFFIX_REPLICATION_DELETE_MARKER_VERSION_ARN_PREFIX, SUFFIX_REPLICATION_RESET_ARN_PREFIX,
    strip_internal_prefix_preserving_case,
};
use rustfs_utils::path::decode_dir_object;
use time::OffsetDateTime;

#[derive(Debug, Default)]
pub(in crate::store) struct PoolErr {
    pub(in crate::store) index: Option<usize>,
    pub(in crate::store) err: Option<Error>,
}

#[derive(Debug, Default)]
pub(in crate::store) struct PoolObjInfo {
    pub(in crate::store) index: usize,
    pub(in crate::store) object_info: ObjectInfo,
    pub(in crate::store) err: Option<Error>,
}

impl Clone for PoolObjInfo {
    fn clone(&self) -> Self {
        Self {
            index: self.index,
            object_info: self.object_info.clone(),
            err: self.err.clone(),
        }
    }
}

pub(super) struct LatestObjectInfoCandidate {
    pub(super) info: Option<ObjectInfo>,
    pub(super) idx: usize,
    pub(super) err: Option<Error>,
}

pub(super) struct RebalanceDeletePoolResult {
    pub(super) pool_idx: usize,
    pub(super) result: Result<ObjectInfo>,
}

pub(super) fn pool_lookup_not_found_error(bucket: &str, object: &str, opts: &ObjectOptions) -> Error {
    let object = decode_dir_object(object);

    if let Some(version_id) = &opts.version_id {
        StorageError::VersionNotFound(bucket.to_owned(), object, version_id.clone())
    } else {
        StorageError::ObjectNotFound(bucket.to_owned(), object)
    }
}

pub(super) fn resolve_store_rebalance_pool_meta_reload_result<T>(result: Result<T>, stage: &str) -> Result<T> {
    result.map_err(|err| Error::other(format!("store rebalance pool meta reload failed during {stage}: {err}")))
}

pub(super) fn resolve_rebalance_delete_from_all_pools_result(
    result: Result<ObjectInfo>,
    bucket: &str,
    object: &str,
) -> Result<ObjectInfo> {
    result.map_err(|err| {
        if matches!(&err, Error::PreconditionFailed | Error::PrefixAccessDenied(_, _)) {
            err
        } else {
            Error::other(format!("failed to delete rebalance source object {bucket}/{object}: {err}"))
        }
    })
}

fn is_ignorable_rebalance_delete_error(err: &Error) -> bool {
    is_err_object_not_found(err) || is_err_version_not_found(err)
}

fn rebalance_delete_pool_error(pool_idx: usize, bucket: &str, object: &str, err: Error) -> Error {
    if matches!(&err, Error::PreconditionFailed | Error::PrefixAccessDenied(_, _)) {
        err
    } else {
        Error::other(format!("pool {pool_idx} delete failed for {bucket}/{object}: {err}"))
    }
}

pub(super) fn resolve_rebalance_delete_from_all_pools_results(
    results: Vec<RebalanceDeletePoolResult>,
    bucket: &str,
    object: &str,
) -> Result<ObjectInfo> {
    let mut deleted = None;
    let mut ignored_error = None;

    for pool_result in results {
        let pool_idx = pool_result.pool_idx;
        match pool_result.result {
            Ok(info) => {
                if deleted.is_none() {
                    deleted = Some(info);
                }
            }
            Err(err) if is_ignorable_rebalance_delete_error(&err) => {
                ignored_error = Some((pool_idx, err));
            }
            Err(err) => {
                return Err(rebalance_delete_pool_error(pool_idx, bucket, object, err));
            }
        }
    }

    if let Some(info) = deleted {
        return Ok(info);
    }

    if let Some((pool_idx, err)) = ignored_error {
        return Err(rebalance_delete_pool_error(pool_idx, bucket, object, err));
    }

    Err(Error::other(format!(
        "failed to delete rebalance source object {bucket}/{object}: no pools were attempted"
    )))
}

pub(super) fn rebalance_disk_set_lookup_error(pool_idx: usize, set_idx: usize, pool_count: usize) -> Error {
    Error::other(format!(
        "failed to resolve rebalance disk set: pool index {pool_idx}, set index {set_idx}, pool count {pool_count}",
    ))
}

fn latest_candidate_mod_time(candidate: &LatestObjectInfoCandidate) -> Option<OffsetDateTime> {
    candidate
        .info
        .as_ref()
        .map(|info| info.mod_time.unwrap_or(OffsetDateTime::UNIX_EPOCH))
}

fn same_transition_identity(left: &ObjectInfo, right: &ObjectInfo) -> bool {
    left.transition_version_state == right.transition_version_state
        && left.transitioned_object.name == right.transitioned_object.name
        && left.transitioned_object.version_id == right.transitioned_object.version_id
        && left.transitioned_object.tier == right.transitioned_object.tier
        && left.transitioned_object.free_version == right.transitioned_object.free_version
        && left.transitioned_object.status == right.transitioned_object.status
}

#[derive(PartialEq, Eq)]
struct LatestUserDefinedIdentity {
    internal: HashMap<String, String>,
    other: HashMap<String, String>,
}

fn normalize_internal_identity_suffix(key: &str) -> Option<String> {
    let suffix = strip_internal_prefix_preserving_case(key)?;

    for dynamic_prefix in [
        SUFFIX_REPLICATION_RESET_ARN_PREFIX,
        SUFFIX_REPLICATION_DELETE_MARKER_VERSION_ARN_PREFIX,
    ] {
        let prefix_len = dynamic_prefix.len();
        if let (Some(prefix), Some(remainder)) = (suffix.get(..prefix_len), suffix.get(prefix_len..))
            && prefix.eq_ignore_ascii_case(dynamic_prefix)
        {
            return Some(format!("{dynamic_prefix}{remainder}"));
        }
    }

    Some(suffix.to_lowercase())
}

fn normalize_user_defined_identity(user_defined: &HashMap<String, String>) -> Option<LatestUserDefinedIdentity> {
    let mut identity = LatestUserDefinedIdentity {
        internal: HashMap::with_capacity(user_defined.len()),
        other: HashMap::with_capacity(user_defined.len()),
    };

    for (key, value) in user_defined {
        if let Some(suffix) = normalize_internal_identity_suffix(key) {
            if identity
                .internal
                .insert(suffix, value.clone())
                .is_some_and(|previous| previous != *value)
            {
                return None;
            }
        } else {
            identity.other.insert(key.clone(), value.clone());
        }
    }

    Some(identity)
}

fn same_user_defined_identity(left: &ObjectInfo, right: &ObjectInfo) -> bool {
    match (
        normalize_user_defined_identity(&left.user_defined),
        normalize_user_defined_identity(&right.user_defined),
    ) {
        (Some(left), Some(right)) => left == right,
        _ => false,
    }
}

/// Pool-specific erasure geometry and data-movement rewrites are intentionally
/// excluded. The selected winner still carries the chosen pool's layout, while
/// the remaining read-visible fields must agree before the pool index can
/// provide a deterministic tie-break.
pub(super) fn same_latest_object_info_identity(left: &ObjectInfo, right: &ObjectInfo) -> bool {
    let same_read_surface = left.bucket == right.bucket
        && left.name == right.name
        && left.is_dir == right.is_dir
        && left.restore_ongoing == right.restore_ongoing
        && left.restore_expires == right.restore_expires
        && left.is_latest == right.is_latest
        && left.content_type == right.content_type
        && left.content_encoding == right.content_encoding
        && left.num_versions == right.num_versions
        && left.successor_mod_time == right.successor_mod_time
        && left.inlined == right.inlined
        && left.metadata_only == right.metadata_only
        && left.version_only == right.version_only
        && left.replication_decision == right.replication_decision;

    if !same_read_surface {
        return false;
    }

    let exact_identity = left.storage_class == right.storage_class
        && left.mod_time == right.mod_time
        && left.size == right.size
        && left.actual_size == right.actual_size
        && same_user_defined_identity(left, right)
        && left.user_tags == right.user_tags
        && left.version_id == right.version_id
        && left.data_dir == right.data_dir
        && left.delete_marker == right.delete_marker
        && same_transition_identity(left, right)
        && left.parts == right.parts
        && left.expires == right.expires
        && left.etag == right.etag
        && left.replication_status_internal == right.replication_status_internal
        && left.replication_status == right.replication_status
        && left.version_purge_status_internal == right.version_purge_status_internal
        && left.version_purge_status == right.version_purge_status
        && left.checksum == right.checksum;

    if exact_identity {
        return true;
    }

    let left_moved =
        rustfs_utils::http::get_consistent_str(&left.user_defined, rustfs_utils::http::SUFFIX_DATA_MOVED) == Some("true");
    let right_moved =
        rustfs_utils::http::get_consistent_str(&right.user_defined, rustfs_utils::http::SUFFIX_DATA_MOVED) == Some("true");

    match (left_moved, right_moved) {
        (false, false) => false,
        (false, true) => crate::data_movement::is_equivalent_data_movement_object_identity(left, right, true, true),
        (true, false) => crate::data_movement::is_equivalent_data_movement_object_identity(right, left, true, true),
        (true, true) => {
            crate::data_movement::is_equivalent_data_movement_object_identity(left, right, true, true)
                || crate::data_movement::is_equivalent_data_movement_object_identity(right, left, true, true)
        }
    }
}

pub(super) fn validate_prepared_pool_refetch_identity(expected: &ObjectInfo, refetched: &ObjectInfo) -> Result<()> {
    if same_latest_object_info_identity(expected, refetched) {
        Ok(())
    } else {
        Err(Error::ErasureReadQuorum)
    }
}

#[cfg(test)]
pub(super) fn resolve_latest_object_info_candidates(
    candidates: Vec<LatestObjectInfoCandidate>,
    bucket: &str,
    object: &str,
    opts: &ObjectOptions,
) -> Result<(ObjectInfo, usize)> {
    resolve_latest_object_info_candidates_with_pool_state(candidates, &[], bucket, object, opts)
}

pub(super) fn resolve_latest_object_info_candidates_with_pool_state(
    candidates: Vec<LatestObjectInfoCandidate>,
    suspended_pools: &[bool],
    bucket: &str,
    object: &str,
    opts: &ObjectOptions,
) -> Result<(ObjectInfo, usize)> {
    let latest_mod_time = candidates.iter().filter_map(latest_candidate_mod_time).max();

    if let Some(latest_mod_time) = latest_mod_time {
        let mut latest_candidates = candidates
            .into_iter()
            .filter(|candidate| latest_candidate_mod_time(candidate) == Some(latest_mod_time))
            .collect::<Vec<_>>();

        // A decommission source remains readable until its target commits. Once
        // equivalent copies exist, prefer the active target without hiding a
        // same-time identity conflict behind pool state.
        latest_candidates.sort_by_key(|candidate| {
            (
                suspended_pools.get(candidate.idx).copied().unwrap_or(false),
                std::cmp::Reverse(candidate.idx),
            )
        });

        let Some(winner) = latest_candidates.first() else {
            return Err(Error::ErasureReadQuorum);
        };
        let Some(winner_info) = winner.info.as_ref() else {
            return Err(Error::ErasureReadQuorum);
        };

        if latest_candidates.iter().skip(1).any(|candidate| {
            candidate
                .info
                .as_ref()
                .is_none_or(|info| !same_latest_object_info_identity(winner_info, info))
        }) {
            return Err(Error::ErasureReadQuorum);
        }

        let winner = latest_candidates.swap_remove(0);
        let Some(winner_info) = winner.info else {
            return Err(Error::ErasureReadQuorum);
        };
        return Ok((winner_info, winner.idx));
    }

    for candidate in candidates {
        if let Some(err) = candidate.err
            && !is_err_object_not_found(&err)
            && !is_err_version_not_found(&err)
        {
            return Err(err);
        }
    }

    Err(pool_lookup_not_found_error(bucket, object, opts))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn prepared_pool_refetch_identity_fails_closed_on_generation_change() {
        let expected = ObjectInfo {
            mod_time: Some(OffsetDateTime::from_unix_timestamp(10).expect("test timestamp should be valid")),
            version_id: Some(uuid::Uuid::from_u128(1)),
            etag: Some("etag-a".to_string()),
            ..Default::default()
        };
        let mut refetched = expected.clone();
        refetched.etag = Some("etag-b".to_string());

        let error = validate_prepared_pool_refetch_identity(&expected, &refetched)
            .expect_err("refetched metadata from a changed generation must fail closed");

        assert_eq!(error, Error::ErasureReadQuorum);
    }

    #[test]
    fn rebalance_delete_result_preserves_precondition_failed() {
        let err = resolve_rebalance_delete_from_all_pools_result(Err(Error::PreconditionFailed), "bucket", "object")
            .expect_err("precondition failure should remain structured");

        assert_eq!(err, Error::PreconditionFailed);
    }

    #[test]
    fn rebalance_delete_result_preserves_prefix_access_denied() {
        let err = resolve_rebalance_delete_from_all_pools_result(
            Err(Error::PrefixAccessDenied("bucket".to_owned(), "object".to_owned())),
            "bucket",
            "object",
        )
        .expect_err("prefix access denial should remain structured");

        assert_eq!(err, Error::PrefixAccessDenied("bucket".to_owned(), "object".to_owned()));
    }

    #[test]
    fn rebalance_delete_pool_result_preserves_precondition_failed() {
        let err = resolve_rebalance_delete_from_all_pools_results(
            vec![RebalanceDeletePoolResult {
                pool_idx: 0,
                result: Err(Error::PreconditionFailed),
            }],
            "bucket",
            "object",
        )
        .expect_err("precondition failure should remain structured");

        assert_eq!(err, Error::PreconditionFailed);
    }

    #[test]
    fn rebalance_delete_pool_result_preserves_prefix_access_denied() {
        let err = resolve_rebalance_delete_from_all_pools_results(
            vec![RebalanceDeletePoolResult {
                pool_idx: 0,
                result: Err(Error::PrefixAccessDenied("bucket".to_owned(), "object".to_owned())),
            }],
            "bucket",
            "object",
        )
        .expect_err("prefix access denial should remain structured");

        assert_eq!(err, Error::PrefixAccessDenied("bucket".to_owned(), "object".to_owned()));
    }
}
