// Copyright 2023 Greptime 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.

//! Define a trait for flow engine, which is used by both streaming engine and batch engine

use std::collections::HashMap;

use common_meta::key::flow::flow_state::FlowStat;
use session::context::QueryContext;
use table::metadata::TableId;

use crate::Error;
// TODO(discord9): refactor common types for flow to a separate module
/// FlowId is a unique identifier for a flow task
pub type FlowId = u64;
pub type TableName = [String; 3];

/// The arguments to create a flow
#[derive(Debug, Clone)]
pub struct CreateFlowArgs {
    pub flow_id: FlowId,
    pub sink_table_name: TableName,
    pub source_table_ids: Vec<TableId>,
    pub create_if_not_exists: bool,
    pub or_replace: bool,
    /// Duration in seconds.
    pub expire_after: Option<i64>,
    pub eval_interval: Option<i64>,
    pub comment: Option<String>,
    pub sql: String,
    pub flow_options: HashMap<String, String>,
    pub query_ctx: Option<QueryContext>,
    /// Typed schedule configuration for `EVAL INTERVAL` flows.
    pub eval_schedule: Option<common_meta::key::flow::flow_info::FlowScheduleConfig>,
}

pub trait FlowEngine {
    /// Create a flow using the provided arguments, return previous flow id if exists and is replaced
    async fn create_flow(&self, args: CreateFlowArgs) -> Result<Option<FlowId>, Error>;
    /// Remove a flow by its ID
    async fn remove_flow(&self, flow_id: FlowId) -> Result<(), Error>;
    /// Flush the flow, return the number of rows flushed
    async fn flush_flow(&self, flow_id: FlowId) -> Result<usize, Error>;
    /// Check if the flow exists
    async fn flow_exist(&self, flow_id: FlowId) -> Result<bool, Error>;
    /// List all flows
    async fn list_flows(&self) -> Result<impl IntoIterator<Item = FlowId>, Error>;
    /// Handle the insert requests for the flow
    async fn handle_flow_inserts(
        &self,
        request: api::v1::region::InsertRequests,
    ) -> Result<(), Error>;

    async fn handle_mark_window_dirty(
        &self,
        req: api::v1::flow::DirtyWindowRequests,
    ) -> Result<(), Error>;
}

/// Provides flow runtime statistics for information schema and heartbeat reporting.
pub trait FlowStatProvider {
    /// Returns current runtime stats of an engine.
    async fn flow_stat(&self) -> FlowStat;
}
