const fs = require('fs');
const { faker } = require('@faker-js/faker');

const sc_styles = [
	"facebook",
	"instagram",
	"linkedin",
	"pinterest",
	"snapchat",
	"spotify",
	"telegram",
	"tiktok",
	"twitter",
	"youtube",
	"other",
];

const template = JSON.parse(fs.readFileSync('./templates/dark/index.json', 'utf8'));
const head = JSON.parse(fs.readFileSync('./templates/base/index.json', 'utf8'));

// const data = {
// 	userimage: faker.image.urlLoremFlickr(),
// 	username: faker.name.fullName,
// 	jobtitle: faker.name.jobTitle(),
// 	socialmedia: Array.from({ length: Math.floor(Math.random() * 10) }, () => ({
// 		[sc_styles[Math.floor(Math.random() * sc_styles.length)]]: faker.internet.url()
// 	})).reduce((acc, curr) => {
// 		const [key, value] = Object.entries(curr)[0]; 
// 		acc[key] = value; 
// 		return acc;
// 	}, {})
// 	,
// 	links: Array.from({ length: Math.floor(Math.random() * 10) }, () => ({
// 		title: faker.lorem.words(),
// 		url: faker.internet.url()
// 	}))
// };

const data = {
	userimage: "/templates/assets/img/aymvne.jpeg",
	username: "Aymen Mahfoudi",
	jobtitle: "CEO of Rythmely",
	socialmedia: {
		instagram : "https://www.instagram.com/aymvne/",
		linkedin : "https://www.linkedin.com/in/aymenmahfoudi/",
		phone: "tel:212628676226",
		email: "mailto:aymenmahfoudi1@gmail.com",
	},
	links: [
		{
			title: "Social Media - example",
			url: "https://www.instagram.com/crepe_ti/",
		},
	],
};

console.log(data.socialmedia);

let header = head.base.replaceAll("%STYLE%", template.styles.map((style) => head.style.replace("%CSS%", style)).join("")).replaceAll("%NAME%", data.username);
let body = {};

body.header = template.body.header.replaceAll("%IMG%", data.userimage).replaceAll("%NAME%", data.username).replaceAll("%JOB%", data.jobtitle);
const socialmedia = Object.entries(data.socialmedia).map(([key, value]) => template.body.link.replaceAll("%IMG%", template.socialmedia[key]).replaceAll("%KEY%", key).replaceAll("%INAME%", value)).join("");
body.social = template.body.social.replaceAll("%LINK%", socialmedia);
body.href = template.body.href.replace("%LINKS%", data.links.map((link) => template.body.a.replace("%TITLE%", link.title).replace("%URL%", link.url)).join(""));
body.footer = template.footer;

const html = header + body.header + body.social + body.href + body.footer;

fs.writeFileSync(`./build/${data.username}.html`, html);
//todo : add phone, email, fax, location icon