/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   prompt.c                                           :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/04 16:13:08 by whaffman      #+#    #+#                 */
/*   Updated: 2025/03/02 22:13:52 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

char	*get_user(t_minishell *msh)
{
	int				len;
	t_environment	*user;
	char			*username;
	char			*result;
	const char		*prompt = RESET "🐚" GREEN "minishell" RESET ": ";

	user = environment_get(msh, "USER");
	if (user == NULL)
		username = "guest";
	else
		username = user->value;
	len = ft_strlen(username) + ft_strlen(prompt) + ft_strlen(GREEN) + 1;
	result = (char *)malloc_safe(msh, len);
	ft_strlcpy(result, GREEN, ft_strlen(GREEN) + 1);
	ft_strlcat(result, username, ft_strlen(GREEN) + ft_strlen(username) + 1);
	ft_strlcat(result, prompt, len);
	return (result);
}

int	get_home_len(t_minishell *msh, char *cwd)
{
	t_environment	*home;
	int				home_len;

	home = environment_get(msh, "HOME");
	home_len = 0;
	if (home && !ft_strncmp(cwd, home->value, ft_strlen(home->value)))
		home_len = ft_strlen(home->value) - 1;
	return (home_len);
}

char	*get_path_with_home(t_minishell *msh)
{
	char			*result;
	char			*cwd;
	int				len;
	int				home_len;

	cwd = getcwd(NULL, 0);
	if (cwd == NULL)
		return (perror("getcwd"), NULL);
	home_len = get_home_len(msh, cwd);
	len = ft_strlen(cwd) - home_len + 1;
	result = malloc_safe(msh, len);
	if (home_len)
	{
		ft_strlcpy(result, "~", 2);
		ft_strlcat(result, cwd + home_len + 1, len);
	}
	else
		ft_strlcpy(result, cwd, len);
	free(cwd);
	return (result);
}

char	*get_path(t_minishell *msh)
{
	char	*result;
	char	*cwd;
	int		len;

	cwd = get_path_with_home(msh);
	if (cwd == NULL)
		return (NULL);
	len = ft_strlen(cwd) + ft_strlen(BLUE RESET "> ") + 1;
	result = malloc_safe(msh, len);
	ft_strlcpy(result, BLUE, len);
	ft_strlcat(result, cwd, len);
	ft_strlcat(result, RESET "> ", len);
	free_safe(msh, (void **) &cwd);
	return (result);
}

char	*ft_prompt(t_minishell *msh)
{
	char	*line;
	char	*cwd;
	char	*prompt;
	char	*user;

	if (PROMPT)
		line = readline(PROMPT);
	else
	{
		cwd = get_path(msh);
		if (cwd == NULL)
			return (NULL);
		user = get_user(msh);
		if (user == NULL)
			return (free(cwd), NULL);
		prompt = ft_strjoin_safe(msh, user, cwd);
		free_safe(msh, (void **)&user);
		free_safe(msh, (void **)&cwd);
		rl_on_new_line();
		line = readline(prompt);
		free_safe(msh, (void **)&prompt);
	}
	if (line != NULL && *line != '\0')
		history_write(line);
	return (line);
}
