// Copyright 2026 The Drasi Authors.
//
// 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.

//! Table service operations for Azure Storage reaction.

use azure_data_tables::clients::TableServiceClientBuilder;
use azure_data_tables::prelude::TableServiceClient;
use azure_storage::{CloudLocation, StorageCredentials};
use log::debug;
use serde_json::{Map, Value};

use crate::util::normalize_custom_uri;

#[derive(Clone)]
pub(crate) struct TableService {
    client: TableServiceClient,
}

impl TableService {
    pub fn new(
        account_name: &str,
        credentials: StorageCredentials,
        endpoint: Option<&str>,
    ) -> Self {
        let mut builder = TableServiceClientBuilder::new(account_name, credentials);
        if let Some(uri) = endpoint {
            builder = builder.cloud_location(CloudLocation::Custom {
                account: account_name.to_string(),
                uri: normalize_custom_uri(account_name, uri),
            });
        }
        Self {
            client: builder.build(),
        }
    }

    pub async fn upsert_entity(
        &self,
        table_name: &str,
        partition_key: &str,
        row_key: &str,
        mut properties: Map<String, Value>,
    ) -> anyhow::Result<()> {
        if properties.contains_key("PartitionKey") {
            debug!(
                "upsert_entity: template included 'PartitionKey'; overwriting with computed value"
            );
        }
        properties.insert(
            "PartitionKey".to_string(),
            Value::String(partition_key.to_string()),
        );
        if properties.contains_key("RowKey") {
            debug!("upsert_entity: template included 'RowKey'; overwriting with computed value");
        }
        properties.insert("RowKey".to_string(), Value::String(row_key.to_string()));

        self.client
            .table_client(table_name)
            .partition_key_client(partition_key)
            .entity_client(row_key)
            .insert_or_replace(Value::Object(properties))?
            .await?;
        Ok(())
    }

    pub async fn delete_entity(
        &self,
        table_name: &str,
        partition_key: &str,
        row_key: &str,
    ) -> anyhow::Result<()> {
        self.client
            .table_client(table_name)
            .partition_key_client(partition_key)
            .entity_client(row_key)
            .delete()
            .await?;
        Ok(())
    }
}
