#![doc = include_str!("../README.md")]
#![doc = include_str!("../docs/cli-reference.md")]
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]

// Copyright (C) 2026 Ordinary Labs, LLC.
//
// SPDX-License-Identifier: AGPL-3.0-only

mod cmds;
mod permission;
mod units;

use clap::{Parser, Subcommand};
use clap_verbosity_flag::{Verbosity, WarnLevel};
use clio::ClioPath;
use std::path::Path;
use tracing::{Level, instrument};
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

use crate::cmds::accounts::get_current_account;
use crate::cmds::secrets::Secrets;

use crate::cmds::root::Root;
use crate::cmds::ssg::Ssg;
use crate::cmds::utils::Utils;
pub use cmds::{
    accounts::Accounts, actions::Actions, app::App, assets::Assets, content::Content,
    integrations::Integrations, models::Models, templates::Templates,
};
use ordinary_api::client::OrdinaryApiClient;
use ordinary_config::OrdinaryConfig;
use ordinaryd::{AppApi, GlobalArgs};
pub use permission::Permission;

pub(crate) static USER_AGENT: &str =
    concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

pub(crate) static GENERATOR: &str = concat!("Ordinary CLI ", env!("CARGO_PKG_VERSION"));

pub(crate) fn add_http(domain: &str, insecure: bool) -> String {
    if insecure {
        format!("http://{domain}")
    } else {
        format!("https://{domain}")
    }
}

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
    #[command(subcommand)]
    pub commands: Commands,

    /// project path
    #[arg(short, long, global = true, value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = ".")]
    pub project: ClioPath,

    /// should only be necessary with localhost or when addressing by IP
    #[arg(long, global = true)]
    pub api_domain: Option<String>,

    /// use HTTP instead of HTTPS
    #[arg(long, global = true, default_value_t = false)]
    pub insecure: bool,

    /// DANGER: only use when working with self-signed localhost certs
    #[arg(long, global = true, default_value_t = false)]
    pub danger_accept_invalid_certs: bool,

    #[command(flatten)]
    pub verbosity: Verbosity<WarnLevel>,

    /// whether to pretty print events to stdio
    #[arg(long, global = true, default_value_t = false)]
    pub pretty: bool,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// create a new Ordinary project
    New {
        /// project domain
        domain: String,
        /// project path
        #[arg(long, default_value = ".")]
        path: String,
    },
    /// manage static site configuration
    Ssg {
        #[command(subcommand)]
        ssg: Ssg,
    },
    /// build your Ordinary project
    ///
    /// Note: will load environment variables from `.env`
    Build {
        /// build project without checking the cache
        #[arg(short, long, default_value_t = false)]
        ignore_cache: bool,
    },
    /// start the app, locally
    Start {
        #[command(flatten)]
        app_api: AppApi,

        #[command(flatten)]
        global_args: GlobalArgs,

        /// disables these defaults set for development:
        /// `--stdio-logs`
        /// `--stdio-logs-fmt concise`
        /// `--insecure`
        /// `--insecure-cookies`
        #[arg(short, long, default_value_t = false)]
        disable_defaults: bool,
    },
    /// combines `build`, `content update`, `assets write`,
    /// `templates upload`, `actions install`
    Publish {
        /// overrides if mode=production. does nothing if mode is localhost or staging
        ///
        /// useful for when you're validating that your DNS is configured properly, and you
        /// want to avoid the Let's Encrypt rate limit for production mode <https://letsencrypt.org/docs/rate-limits/>
        #[arg(short, long, default_value_t = false)]
        tls_staging: bool,
    },

    /// manage templates in your Ordinary project
    Templates {
        #[command(subcommand)]
        templates: Templates,
    },
    /// manage content in your Ordinary project
    Content {
        #[command(subcommand)]
        content: Content,
    },
    /// manage assets in your Ordinary project
    Assets {
        #[command(subcommand)]
        assets: Assets,
    },

    /// manage models in your Ordinary project
    Models {
        #[command(subcommand)]
        models: Models,
    },
    /// manage actions in your Ordinary project
    Actions {
        #[command(subcommand)]
        actions: Actions,
    },
    /// manage integrations in your Ordinary project
    Integrations {
        #[command(subcommand)]
        integrations: Integrations,
    },

    /// manage accounts connected to `ordinaryd`
    Accounts {
        #[command(subcommand)]
        accounts: Accounts,
    },
    /// manage applications running on `ordinaryd`
    App {
        #[command(subcommand)]
        app: App,
    },
    /// manage secrets in your Ordinary application
    Secrets {
        #[command(subcommand)]
        secrets: Secrets,
    },
    Root {
        #[command(subcommand)]
        root: Root,
    },
    /// ensure that all the correct system components are installed
    Doctor {
        /// auto fix installs
        #[arg(short, long, value_delimiter = ',', num_args = 1..)]
        fix: Option<Vec<ordinary_doctor::Fix>>,
    },
    /// utility functions for aiding project development
    Utils {
        #[command(subcommand)]
        utils: Utils,
    },
}

pub fn setup(cli: &Cli) -> anyhow::Result<()> {
    let pretty_layer = if cli.pretty {
        Some(
            tracing_subscriber::fmt::layer()
                .pretty()
                .with_span_events(FmtSpan::CLOSE)
                .with_writer(std::io::stderr),
        )
    } else {
        None
    };

    let ugly_layer = if cli.pretty {
        None
    } else {
        Some(
            tracing_subscriber::fmt::layer()
                .with_span_events(FmtSpan::CLOSE)
                .with_target(false)
                .with_writer(std::io::stderr),
        )
    };

    let log_level_str = cli
        .verbosity
        .tracing_level()
        .unwrap_or(Level::INFO)
        .as_str()
        .to_ascii_lowercase();

    let directives = [
        ("ordinaryd", &log_level_str),            // daemon
        ("ordinary_modify", &log_level_str),      // modify
        ("ordinary_build", &log_level_str),       // build
        ("ordinary_doctor", &log_level_str),      // doctor
        ("ordinary_studio", &log_level_str),      // studio
        ("ordinary_utils", &log_level_str),       // utils
        ("ordinary_auth", &log_level_str),        // auth
        ("ordinary_api", &log_level_str),         // api
        ("ordinary_app", &log_level_str),         // app
        ("ordinary_template", &log_level_str),    // templates
        ("ordinary_action", &log_level_str),      // actions
        ("ordinary_integration", &log_level_str), // integrations
        ("ordinary_storage", &log_level_str),     // storage
        ("ordinary_monitor", &log_level_str),     // monitor
        ("ordinary_config", &log_level_str),      // config
        ("tower_http", &log_level_str),           // http
    ];

    let mut directives_string = format!("ordinary={}", &log_level_str);

    for (lib, lvl) in directives {
        directives_string = format!("{directives_string},{lib}={lvl}");
    }

    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| directives_string.into()),
        )
        .with(pretty_layer)
        .with(ugly_layer)
        .init();

    std::panic::set_hook(Box::new(|info| {
        if let Some(msg) = info.payload_as_str()
            && let Some(loc) = info.location()
        {
            tracing::error!(%loc, msg, "panic");
        } else if let Some(loc) = info.location() {
            tracing::error!(%loc, "panic");
        }
    }));

    Ok(())
}

#[allow(clippy::too_many_lines, clippy::missing_panics_doc)]
#[instrument(name = "ordinary", skip_all, err)]
pub async fn run(cli: &Cli) -> anyhow::Result<()> {
    let api_domain = cli.api_domain.as_deref();

    let project = cli
        .project
        .to_str()
        .expect("failed to get string from path");

    match &cli.commands {
        Commands::New { path, domain } => ordinary_modify::project::new(path, domain)?,
        Commands::Ssg { ssg } => {
            ssg.handle(project)?;
        }
        Commands::Build { ignore_cache } => {
            let env_file = Path::new(project).join(".env");
            if env_file.exists() {
                dotenv::from_path(env_file)?;
            }

            ordinary_build::build(project, *ignore_cache, "ordinary")?;
        }
        Commands::Publish { tls_staging } => {
            let env_file = Path::new(project).join(".env");
            if env_file.exists() {
                dotenv::from_path(env_file)?;
            }

            ordinary_build::build(project, true, GENERATOR)?;

            let account = get_current_account(cli.insecure)?;
            let client = OrdinaryApiClient::new(
                &account.host,
                &account.name,
                api_domain,
                cli.danger_accept_invalid_certs,
                USER_AGENT,
                true,
            )?;

            let config = OrdinaryConfig::get(project)?;

            client.deploy(project, *tls_staging).await?;

            if config.content.is_some() {
                client.update(project).await?;
            }

            if config.assets.is_some() {
                client.write_all(project).await?;
            }

            if config.templates.is_some() {
                client.upload_all(project).await?;
            }

            if config.actions.is_some() {
                client.install_all(project).await?;
            }
        }
        Commands::Templates { templates } => {
            templates
                .handle(
                    api_domain,
                    cli.danger_accept_invalid_certs,
                    project,
                    cli.insecure,
                )
                .await?;
        }
        Commands::Content { content } => {
            content
                .handle(
                    api_domain,
                    cli.danger_accept_invalid_certs,
                    project,
                    cli.insecure,
                )
                .await?;
        }
        Commands::Assets { assets } => {
            assets
                .handle(
                    api_domain,
                    cli.danger_accept_invalid_certs,
                    project,
                    cli.insecure,
                )
                .await?;
        }
        Commands::Models { models } => {
            models
                .handle(
                    api_domain,
                    cli.danger_accept_invalid_certs,
                    project,
                    cli.insecure,
                )
                .await?;
        }
        Commands::Actions { actions } => {
            actions
                .handle(
                    api_domain,
                    cli.danger_accept_invalid_certs,
                    project,
                    cli.insecure,
                )
                .await?;
        }
        Commands::Integrations { integrations } => {
            integrations.handle(project)?;
        }
        Commands::Accounts { accounts } => {
            accounts
                .handle(api_domain, cli.danger_accept_invalid_certs, cli.insecure)
                .await?;
        }
        Commands::App { app } => {
            let env_file = Path::new(project).join(".env");
            if env_file.exists() {
                dotenv::from_path(env_file)?;
            }

            app.handle(
                api_domain,
                cli.danger_accept_invalid_certs,
                project,
                cli.insecure,
            )
            .await?;
        }
        Commands::Secrets { secrets } => {
            secrets
                .handle(
                    api_domain,
                    cli.danger_accept_invalid_certs,
                    project,
                    cli.insecure,
                )
                .await?;
        }
        Commands::Root { root } => {
            root.handle(api_domain, cli.danger_accept_invalid_certs, cli.insecure)
                .await?;
        }
        Commands::Doctor { fix } => {
            ordinary_doctor::doctor(&fix.clone().unwrap_or(vec![]))?;
        }
        Commands::Utils { utils } => {
            utils.handle()?;
        }
        Commands::Start { .. } => unreachable!("checked in main.rs"),
    }

    Ok(())
}
