// Copyright 2025 SkyReels SDK Contributors
//
// 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.

//! Custom client configuration examples for SkyReels SDK
//!
//! This example demonstrates how to configure and customize
//! the SkyReels client for different use cases.

use skyreels::SkyReelsClient;
use tracing::info;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load environment variables from .env file
    dotenv::dotenv().ok();

    tracing_subscriber::fmt::init();

    // Example 1: Default client
    info!("=== Example 1: Default Client ===");
    let default_client = SkyReelsClient::new("your_api_key");
    info!(
        "Default client created with base URL: {}",
        default_client.base_url().await
    );

    // Example 2: Client with custom base URL
    info!("=== Example 2: Client with Custom Base URL ===");
    let custom_client = SkyReelsClient::with_base_url(
        "your_api_key",
        "https://custom-gateway.example.com",
    );
    info!(
        "Custom client created with base URL: {}",
        custom_client.base_url().await
    );

    // Example 3: Update API key dynamically
    info!("=== Example 3: Dynamic API Key Update ===");
    let client = SkyReelsClient::new("initial_api_key");
    info!("Initial API key set");

    // Later, you might need to update the API key
    client.set_api_key("new_api_key").await;
    info!("API key updated");

    // Example 4: Update base URL dynamically
    info!("=== Example 4: Dynamic Base URL Update ===");
    client.set_base_url("https://new-gateway.example.com").await;
    info!("Base URL updated to: {}", client.base_url().await);

    // Example 5: Client for testing with mock server
    info!("=== Example 5: Testing Configuration ===");
    #[cfg(debug_assertions)]
    {
        let _test_client = SkyReelsClient::with_base_url(
            "test_key",
            "http://localhost:8080",
        );
        info!("Test client created for local development");

        // You could use this for local testing with a mock server
        // or integration tests
    }

    // Example 6: Client with environment-based configuration
    info!("=== Example 6: Environment-based Configuration ===");
    let api_key = std::env::var("SKYREELS_API_KEY")
        .unwrap_or_else(|_| "default_api_key".to_string());

    let base_url = std::env::var("SKYREELS_BASE_URL")
        .unwrap_or_else(|_| "https://api-gateway.skyreels.ai/api/v1".to_string());

    let env_client = SkyReelsClient::with_base_url(&api_key, &base_url);
    info!(
        "Environment-configured client created with URL: {}",
        env_client.base_url().await
    );

    // Example 7: Multiple clients for different environments
    info!("=== Example 7: Multiple Clients for Different Environments ===");

    let prod_client = SkyReelsClient::with_base_url(
        "prod_api_key",
        "https://api-gateway.skyreels.ai/api/v1",
    );

    let staging_client = SkyReelsClient::with_base_url(
        "staging_api_key",
        "https://staging-gateway.skyreels.ai/api/v1",
    );

    let dev_client = SkyReelsClient::with_base_url(
        "dev_api_key",
        "http://localhost:8080",
    );

    info!("Production client: {}", prod_client.base_url().await);
    info!("Staging client: {}", staging_client.base_url().await);
    info!("Development client: {}", dev_client.base_url().await);

    // Example 8: Client for proxy configuration
    info!("=== Example 8: Proxy Configuration ===");

    // If you need to use a proxy, you can set the base URL accordingly
    let _proxy_client = SkyReelsClient::with_base_url(
        "your_api_key",
        "https://proxy.example.com/skyreels/api/v1",
    );
    info!("Proxy client created");

    // Example 9: Shared client across async tasks
    info!("=== Example 9: Shared Client Across Tasks ===");

    let shared_client = SkyReelsClient::new("shared_api_key");

    // Clone the client for use in different tasks
    let client_clone1 = shared_client.clone();
    let client_clone2 = shared_client.clone();

    // Use in different async tasks (the client is safe to share across tasks)
    info!("Shared client can be safely used across multiple async tasks");

    // Example: You could spawn multiple tasks using the same client
    // This is safe because the client uses Arc internally

    info!("Both tasks completed successfully");

    Ok(())
}
