// Copyright 2022 RisingWave Labs
//
// 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::HashSet;

use async_trait::async_trait;
use futures::stream::BoxStream;
use risingwave_hummock_sdk::change_log::TableChangeLogs;
use risingwave_hummock_sdk::version::HummockVersion;
use risingwave_hummock_sdk::{
    CompactionGroupId, HummockEpoch, HummockVersionId, ObjectIdRange, SyncResult,
};
use risingwave_pb::hummock::{
    PbHummockVersion, SubscribeCompactionEventRequest, SubscribeCompactionEventResponse,
};
use risingwave_pb::iceberg_compaction::{
    SubscribeIcebergCompactionEventRequest, SubscribeIcebergCompactionEventResponse,
};
use risingwave_pb::id::{HummockSstableId, JobId, TableId};
use tokio::sync::mpsc::UnboundedSender;

pub type CompactionEventItem = std::result::Result<SubscribeCompactionEventResponse, tonic::Status>;
pub type IcebergCompactionEventItem =
    std::result::Result<SubscribeIcebergCompactionEventResponse, tonic::Status>;

use crate::error::Result;

pub type HummockMetaClientChangeLogInfo = Vec<u64>;

#[async_trait]
pub trait HummockMetaClient: Send + Sync + 'static {
    async fn unpin_version_before(&self, unpin_version_before: HummockVersionId) -> Result<()>;
    async fn get_current_version(&self) -> Result<HummockVersion>;
    async fn get_new_object_ids(&self, number: u32) -> Result<ObjectIdRange>;
    // We keep `commit_epoch` only for test/benchmark.
    async fn commit_epoch_with_change_log(
        &self,
        epoch: HummockEpoch,
        sync_result: SyncResult,
        change_log_info: Option<HummockMetaClientChangeLogInfo>,
    ) -> Result<()>;
    async fn commit_epoch(&self, epoch: HummockEpoch, sync_result: SyncResult) -> Result<()> {
        self.commit_epoch_with_change_log(epoch, sync_result, None)
            .await
    }
    async fn trigger_manual_compaction(
        &self,
        compaction_group_id: CompactionGroupId,
        table_id: JobId,
        level: u32,
        target_level: Option<u32>,
        sst_ids: Vec<HummockSstableId>,
        exclusive: bool,
    ) -> Result<bool>;
    async fn trigger_full_gc(
        &self,
        sst_retention_time_sec: u64,
        prefix: Option<String>,
    ) -> Result<()>;

    async fn subscribe_compaction_event(
        &self,
    ) -> Result<(
        UnboundedSender<SubscribeCompactionEventRequest>,
        BoxStream<'static, CompactionEventItem>,
    )>;

    async fn get_version_by_epoch(
        &self,
        epoch: HummockEpoch,
        table_id: TableId,
    ) -> Result<PbHummockVersion>;

    async fn subscribe_iceberg_compaction_event(
        &self,
    ) -> Result<(
        UnboundedSender<SubscribeIcebergCompactionEventRequest>,
        BoxStream<'static, IcebergCompactionEventItem>,
    )>;

    async fn get_table_change_logs(
        &self,
        epoch_only: bool,
        start_epoch_inclusive: Option<u64>,
        end_epoch_inclusive: Option<u64>,
        table_ids: Option<HashSet<TableId>>,
        exclude_empty: bool,
        limit: Option<u32>,
    ) -> Result<TableChangeLogs>;
}
