// TODO : Setup ssh (config + authorized_keys)
// TODO : Setup virtualization
// TODO : Setup docker
// TODO : Autojoin docker swarm
// TODO : Autojoin teleport

// DRIVE SELECTION

use boot::setup_bootloader;
use drives::{format_drives, mount_drives};
use first_boot::{first_boot_values, genfstab};
use kernel::setup_mkinitcpio;
use navos::setup_navos;
use ollama::setup_ollama;
use security::{setup_secure_boot, setup_tpm_unlock};
use skel::setup_skel;
use ssh::setup_ssh;
use user::setup_users;
use yansi::{Color, Paint};
use zram::setup_zram;

pub mod boot;
pub mod drives;
pub mod first_boot;
pub mod kernel;
pub mod navos;
pub mod ollama;
pub mod security;
pub mod skel;
pub mod ssh;
pub mod user;
pub mod zram;

use crate::{
    config::InstallConfig,
    pkg::{self, install_pkgs, pacstrap},
    print_status,
};

pub fn uncomment_first_value_of(value: &str, file: &str) {
    // read in the file
    let content = std::fs::read_to_string(file).unwrap();
    let mut new = String::new();
    let mut found = false;

    // search for the first instance of `value` in the file
    // uncomment the '#' symbol if there is one
    for line in content.lines() {
        if line.contains(value) && !found {
            new.push_str(&format!("{}\n", line.replace('#', "")));
            found = true;
        } else {
            new.push_str(&format!("{line}\n"));
        }
    }
    // write back
    std::fs::write(file, new).unwrap();
}

pub fn uncomment_tag(tag: &str, file: &str) {
    // read in the file
    let content = std::fs::read_to_string(file).unwrap();
    let mut new = String::new();

    // search for the first instance of `value` in the file
    // uncomment the '#' symbol if there is one
    for line in content.lines() {
        if line.starts_with(tag) {
            new.push_str(&format!("{}\n", line.trim_start_matches(tag)));
        } else {
            new.push_str(&format!("{line}\n"));
        }
    }

    // write back
    std::fs::write(file, new).unwrap();
}

pub fn install(conf: InstallConfig) {
    // Drive Setup
    format_drives(&conf.drive);
    mount_drives(&conf.drive);

    // Base Install
    pacstrap(&conf.pkg);
    genfstab();

    // System Setup
    first_boot_values(&conf.general);
    setup_skel(&conf.general);
    setup_users(&conf.user);

    setup_ssh(conf.ssh);

    setup_bootloader();

    match conf.general.mode {
        crate::config::InstallMode::Base => {}
        crate::config::InstallMode::Desktop => {
            setup_navos();
            install_pkgs(&pkg::DESKTOP_PKG);
            print_status("Enable SDDM");

            // TODO : Setup KDE Keyboard Layout

            std::os::unix::fs::symlink(
                "/usr/lib/systemd/system/sddm.service",
                "/mnt/etc/systemd/system/display-manager.service",
            )
            .unwrap();
        }
        crate::config::InstallMode::Server => {
            setup_navos();
            install_pkgs(&pkg::SERVER_PKG);
        }
        crate::config::InstallMode::Kiosk => {
            // TODO
        }
    }

    if conf.pkg.virtualization {
        // TODO : Enable virtualization
    }

    if conf.pkg.docker {
        // TODO : Enable docker
    }

    if let Some(ai) = conf.ai {
        setup_ollama(&ai);
    }

    setup_zram();
    setup_mkinitcpio(&conf.drive);
    setup_secure_boot();

    if conf.drive.encryption.is_some() {
        setup_tpm_unlock(&conf.drive);
    }

    println!("{}", "System install complete".paint(Color::Green));
}