/**
 * setup domains
 * @type {{framework: string, domain: string}[]}
 */

import fs from 'node-fs'
import { makeHostFileForWordpress } from './model.wordpress.mjs'
import { makeHostFileForSymfony } from './model.symfony.mjs'
import { makeHostFileForPhpPages } from './model.php-website.mjs'
import { domainsConfig } from './domains.mjs'
import { makeHostFileForNextcloud } from './model.nextcloud.mjs'

// autres frameworks:
// nextcloud: cloud.tykayn.fr
// vaultwarden: pass.cipherbliss.com
// mastodon: mastodon.cipherbliss.com
// peertube: peertube.cipherbliss.com
//
// sites statiques
// meltingpot.cipherbliss.com
// joinfediverse
// coussinet
let domainsForHostFile = []
let createFoldersScript = ''

let domainsSorted = domainsConfig.sort((a, b) => {
	return a.domain - b.domain
})

for (let configDomain of domainsSorted) {

	console.log('domaine :', configDomain.name)
	domainsForHostFile.push(configDomain.domain)

	let hostFile
	if (configDomain.framework === 'wordpress') {
		hostFile = makeHostFileForWordpress(configDomain)
	}
	if (configDomain.framework === 'symfony') {
		hostFile = makeHostFileForSymfony(configDomain)
	}
	if (configDomain.framework === 'nextcloud') {
		hostFile = makeHostFileForNextcloud(configDomain)
	}
	if (configDomain.framework === 'static') {
		hostFile = makeHostFileForPhpPages(configDomain)

		createFoldersScript += '# --------- \n'
		createFoldersScript += 'mkdir -p /home/www/' + configDomain.domain + ' \n'
		createFoldersScript += 'touch /home/www/' + configDomain.domain + '/index.html \n'
		createFoldersScript += 'echo "coucou ' + configDomain.domain + '" > /home/www/' + configDomain.domain + '/index.html \n'
		createFoldersScript += ' \n'
		createFoldersScript += 'touch /etc/nginx/sites-available/' + configDomain.domain + '" \n'
		createFoldersScript += 'rm /etc/nginx/sites-available/' + configDomain.domain + '" /etc/nginx/sites-enabled/' + configDomain.domain + '" \n'
		createFoldersScript += 'ln -s /etc/nginx/sites-available/' + configDomain.domain + '" /etc/nginx/sites-enabled/ \n'
		createFoldersScript += ' \n'

		writeFile(configDomain.domain + '_direct.conf', hostFile.noContainerNginxConf)
	}else{

	writeFile(configDomain.domain + '_host.conf', hostFile.homeNginxConf)
	writeFile(configDomain.domain + '_container.conf', hostFile.containerNginxConf)
	}

	let renewCertbotScript = domainsForHostFile.map(domain => {
			if (domain.redirectToNoWWW || domain.includes('www.')) {

				return ' certbot certonly -a webroot --webroot-path=/tmp/letsencrypt-auto -d ' + domain + ' -d ' + domain.replace('www.', '') + ' \n'
			} else {
				return ' certbot certonly -a webroot --webroot-path=/tmp/letsencrypt-auto -d ' + domain + ' \n'
			}
		}
	)

	let hostfileDomains = domainsForHostFile.sort((a, b) => {
		return a - b
	}).map(domain => ' 127.0.0.1\t' + domain + ' \n')

	console.log('hostfileDomains', hostfileDomains.join('')
		.replace(',', ''))


	writeFile('certbot_renew.sh', '!#/bin/bash\n' + renewCertbotScript.join('').replace(',', '')+ '\n149.202.77.27 riseup\n' +
		'::1     localhost       ip6-localhost   ip6-loopback\n' +
		'# coussinet chatons\n' +
		'ff02::1 ip6-allnodes\n' +
		'ff02::2 ip6-allrouters\n' +
		'# ici c\'est 149.202.77.27\n')

	writeFile('folders_create.sh', createFoldersScript.replace(',', ''))
	writeFile('hosts', hostfileDomains.join('')
		.replace(',', ''))
}

function writeFile (fileName, fileContent) {
	console.log('write file', fileName)
	return fs.writeFile(
		`./output/${fileName}`,
		fileContent,
		'utf8',
		(err) => {
			if (err) {
				console.log(`Error writing file: ${err}`)
			}
		}
	)
}