use crate::dot::config::DotfileConfig;
use crate::dot::db::Database;
use crate::dot::repo::DotfileRepositoryManager;
use crate::ui::catppuccin::colors;
use crate::ui::nerd_font::NerdFont;
use crate::ui::preview::PreviewBuilder;

/// Build preview for a repository in the main menu
pub fn build_repo_preview(repo_name: &str, config: &DotfileConfig, db: &Database) -> String {
    let repo_manager = DotfileRepositoryManager::new(config, db);

    let repo_config = match config.repos.iter().find(|r| r.name == repo_name) {
        Some(rc) => rc,
        None => return format!("Repository '{}' not found in config", repo_name),
    };

    let mut builder = PreviewBuilder::new().title(colors::SKY, repo_name).blank();

    // Show external repo status if applicable
    if repo_config.metadata.is_some() {
        builder = builder.line(
            colors::YELLOW,
            Some(NerdFont::Info),
            "External (Yadm/Stow compatible - metadata in config)",
        );
    }

    builder = builder.line(
        colors::TEXT,
        Some(NerdFont::Link),
        &format!("URL: {}", repo_config.url),
    );

    // Branch
    if let Some(branch) = &repo_config.branch {
        builder = builder.line(
            colors::TEXT,
            Some(NerdFont::GitBranch),
            &format!("Branch: {}", branch),
        );
    }

    // Priority
    let priority = config
        .repos
        .iter()
        .position(|r| r.name == repo_name)
        .map(|i| i + 1)
        .unwrap_or(0);
    let total_repos = config.repos.len();

    if priority > 0 {
        let label = if priority == 1 && total_repos > 1 {
            " (highest priority)"
        } else if priority == total_repos && total_repos > 1 {
            " (lowest priority)"
        } else {
            ""
        };

        builder = builder.line(
            colors::PEACH,
            Some(NerdFont::ArrowUp),
            &format!("Priority: P{}{}", priority, label),
        );
    }

    // Status
    let status_color = if repo_config.enabled {
        colors::GREEN
    } else {
        colors::RED
    };
    let status_text = if repo_config.enabled {
        "Enabled"
    } else {
        "Disabled"
    };
    let status_icon = if repo_config.enabled {
        NerdFont::ToggleOn
    } else {
        NerdFont::ToggleOff
    };
    builder = builder.line(status_color, Some(status_icon), status_text);

    // Read-only
    if repo_config.read_only {
        builder = builder.line(colors::YELLOW, Some(NerdFont::Lock), "Read-only");
    }

    // Try to get more info from DotfileRepo
    if let Ok(dotfile_repo) = repo_manager.get_repository_info(repo_name) {
        let defaults_disabled = repo_config.active_subdirectories.is_none()
            && dotfile_repo
                .meta
                .default_active_subdirs
                .as_ref()
                .map(|dirs| dirs.is_empty())
                .unwrap_or(false);

        // Show description if present
        if let Some(desc) = &dotfile_repo.meta.description {
            builder = builder.blank().line(
                colors::TEXT,
                Some(NerdFont::FileText),
                &format!("Description: {}", desc),
            );
        }

        // Show author if present
        if let Some(author) = &dotfile_repo.meta.author {
            builder = builder.line(
                colors::BLUE,
                Some(NerdFont::User),
                &format!("Author: {}", author),
            );
        }

        builder = builder
            .blank()
            .line(colors::MAUVE, Some(NerdFont::Folder), "Subdirectories");

        if defaults_disabled {
            builder = builder.indented_line(
                colors::YELLOW,
                Some(NerdFont::Warning),
                "Defaults disabled - enable subdirs to activate this repo",
            );
        }

        if dotfile_repo.meta.dots_dirs.is_empty() {
            builder = builder.indented_line(colors::SUBTEXT0, None, "No subdirectories configured");
        } else {
            let available = dotfile_repo.meta.dots_dirs.join(", ");
            let active = if let Some(active_subdirs) = &repo_config.active_subdirectories {
                if active_subdirs.is_empty() {
                    "(none configured)".to_string()
                } else {
                    active_subdirs.join(", ")
                }
            } else if dotfile_repo.meta.dots_dirs.is_empty() {
                "(none configured)".to_string()
            } else {
                let repo_path = config.repos_path().join(&repo_config.name);
                let effective_active = config.resolve_active_subdirs(repo_config);
                if effective_active.is_empty() {
                    if defaults_disabled {
                        "(disabled by defaults)".to_string()
                    } else if repo_path.join("instantdots.toml").exists()
                        || repo_config.metadata.is_some()
                    {
                        "(none configured)".to_string()
                    } else {
                        "(none detected)".to_string()
                    }
                } else {
                    effective_active.join(", ")
                }
            };
            builder = builder
                .indented_line(colors::TEXT, None, &format!("Available: {}", available))
                .indented_line(colors::GREEN, None, &format!("Active: {}", active));
        }

        // Show encryption status if present
        let recipients = &dotfile_repo.meta.encryption_recipients;
        if !recipients.is_empty() {
            builder =
                builder
                    .blank()
                    .line(colors::PEACH, Some(NerdFont::ShieldLock), "Age Encryption");

            let local_pubkeys =
                crate::dot::operations::key::get_local_public_keys().unwrap_or_default();
            let has_matching_identity = recipients.iter().any(|r| local_pubkeys.contains(r));

            let status_line = if has_matching_identity {
                format!("Authorized ({} recipient(s))", recipients.len())
            } else {
                format!(
                    "Unauthorized ({} recipient(s) - key missing)",
                    recipients.len()
                )
            };
            let status_color = if has_matching_identity {
                colors::GREEN
            } else {
                colors::RED
            };
            let status_icon = if has_matching_identity {
                NerdFont::Check
            } else {
                NerdFont::Warning
            };

            builder = builder.indented_line(status_color, Some(status_icon), &status_line);
        }

        // Local path
        if let Ok(local_path) = dotfile_repo.local_path(config) {
            let tilde_path = local_path.display().to_string();
            builder = builder.blank().indented_line(
                colors::TEXT,
                Some(NerdFont::Folder),
                &format!("Local: {}", tilde_path),
            );
        }
    }

    builder.build_string()
}
