use super::{Job, UiSender};

/// /etc/hostname ve /etc/hosts dosyalarını yazar.
pub struct ConfigureHostnameJob {
    pub mount:    String,
    pub hostname: String,
}

#[async_trait::async_trait]
impl Job for ConfigureHostnameJob {
    fn name(&self) -> &str { "job_hostname" }

    async fn run(&self, ui: &UiSender) -> Result<(), String> {
        let etc_dir = format!("{}/etc", self.mount);
        let _ = tokio::fs::create_dir_all(&etc_dir).await;

        // /etc/hostname
        let hostname_path = format!("{}/etc/hostname", self.mount);
        ui.log(format!("Yazılıyor: {}", hostname_path));
        tokio::fs::write(&hostname_path, format!("{}\n", self.hostname))
            .await
            .map_err(|e| format!("/etc/hostname yazılamadı: {}", e))?;

        // /etc/hosts
        let hosts_path = format!("{}/etc/hosts", self.mount);
        let hosts_content = format!(
            "127.0.0.1\tlocalhost\n\
             ::1\t\tlocalhost\n\
             127.0.1.1\t{hostname}\n",
            hostname = self.hostname
        );
        ui.log(format!("Yazılıyor: {}", hosts_path));
        tokio::fs::write(&hosts_path, hosts_content)
            .await
            .map_err(|e| format!("/etc/hosts yazılamadı: {}", e))?;

        Ok(())
    }
}
