use serde::{Deserialize, Serialize};

use crate::{api_model::Platforms, plugin_run::PluginState};

pub type Rowid = i64;
pub type DbTime = i64;

/// Fields common to all items
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ItemBase {
    pub rowid: Rowid,
    pub id: String,

    #[serde(rename = "type")]
    pub _type: String,
    pub date_created: DbTime,
    pub date_modified: DbTime,
    pub date_server_modified: DbTime,
    pub deleted: bool,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ItemWithBase<T> {
    #[serde(flatten)]
    pub base: ItemBase,
    #[serde(flatten)]
    pub item : T
}

/// Property name of PluginRun object, containing port on which plugin starts to
/// listen it's webserver.
pub const WEBSERVER_PORT: &str = "webserverPort";
pub const WEBSERVER_URL: &str = "webserverUrl";
pub const CONTAINER_ID: &str = "containerId";
pub const CONTAINER_IMAGE: &str = "containerImage";
pub const CONTAINER_STATE: &str = "containerState";
#[derive(Deserialize, Debug, Hash, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PluginRunItem {
    pub container_image: String,
    pub target_item_id: String,
    pub webserver_port: u32,
    pub webserver_url: String,
    pub container_id: String,
    pub status: Option<String>,
    pub container_state: PluginState
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Trigger {
    pub action: String,
    pub filter_created_after: i64,
    pub filter_created_after_property_name: String,
    // TODO: use base item
    pub id: String,
    pub plugin_run_id: String,
    pub trigger_on: String,
}

pub const OAUTH2_FLOW: &str = "Oauth2Flow";
pub const ACCESS_TOKEN: &str = "accessToken";
pub const REFRESH_TOKEN: &str = "refreshToken";
pub const TOKEN_TYPE: &str = "tokenType";
pub const REDIRECT_URI: &str = "redirectUri";
pub const PLATFORM: &str = "platform";
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Oauth2Flow {
    pub access_token: String,
    pub refresh_token: String,
    pub token_type: String,
    pub redirect_uri: String,
    pub platform: Platforms,

    #[serde(flatten)]
    pub base: ItemBase,
}