import AuditSource from '../auditsource.js';
import BoxError from '../boxerror.js';
import branding from '../branding.js';
import constants from '../constants.js';
import dashboard from '../dashboard.js';
import ejs from 'ejs';
import fs from 'node:fs';
import { HttpError } from '@cloudron/connect-lastmile';
import { HttpSuccess } from '@cloudron/connect-lastmile';
import path from 'node:path';
import paths from '../paths.js';
import safe from 'safetydance';
import settings from '../settings.js';


async function getConfig(req, res, next) {
    const [error, cloudronConfig] = await safe(dashboard.getConfig());
    if (error) return next(BoxError.toHttpError(error));

    next(new HttpSuccess(200, cloudronConfig));
}

async function startPrepareLocation(req, res, next) {
    if (!req.body.domain || typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));

    const [error, taskId] = await safe(dashboard.startPrepareLocation(req.body.domain, AuditSource.fromRequest(req)));
    if (error) return next(BoxError.toHttpError(error));

    next(new HttpSuccess(202, { taskId }));
}

async function changeLocation(req, res, next) {
    if (!req.body.domain || typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));

    const [error] = await safe(dashboard.changeLocation(constants.DASHBOARD_SUBDOMAIN, req.body.domain, AuditSource.fromRequest(req)));
    if (error) return next(BoxError.toHttpError(error));

    next(new HttpSuccess(204, {}));
}

const TEMPLATE_INDEX = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'index.html'), 'utf-8');
const TEMPLATE_FILEMANAGER = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'filemanager.html'), 'utf-8');
const TEMPLATE_LOGS = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'logs.html'), 'utf-8');
const TEMPLATE_TERMINAL = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'terminal.html'), 'utf-8');
const TEMPLATE_PASSWORDRESET = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'passwordreset.html'), 'utf-8');
const TEMPLATE_SETUPACCOUNT = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'setupaccount.html'), 'utf-8');
const TEMPLATE_SETUP = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'setup.html'), 'utf-8');
const TEMPLATE_RESTORE = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'restore.html'), 'utf-8');
const TEMPLATE_ACTIVATION = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'activation.html'), 'utf-8');

async function renderIndex(req, res) {
    const { fqdn:dashboardFqdn } = await dashboard.getLocation();
    const name = await branding.getCloudronName();
    const html = ejs.render(TEMPLATE_INDEX, { dashboardFqdn, name });

    res.send(html);
}

async function renderFilemanager(req, res) {
    res.send(ejs.render(TEMPLATE_FILEMANAGER, {}));
}

async function renderLogs(req, res) {
    res.send(ejs.render(TEMPLATE_LOGS, {}));
}

async function renderTerminal(req, res) {
    res.send(ejs.render(TEMPLATE_TERMINAL, {}));
}

async function renderPasswordreset(req, res) {
    const name = await branding.getCloudronName();
    const footer = await branding.renderFooter();
    const language = await settings.get(settings.LANGUAGE_KEY);

    const html = ejs.render(TEMPLATE_PASSWORDRESET, { name, footer, language });

    res.send(html);
}

async function renderSetupaccount(req, res) {
    const name = await branding.getCloudronName();
    const footer = await branding.renderFooter();
    const language = await settings.get(settings.LANGUAGE_KEY);

    const html = ejs.render(TEMPLATE_SETUPACCOUNT, { name, footer, language });

    res.send(html);
}

async function renderSetup(req, res) {
    res.send(ejs.render(TEMPLATE_SETUP, {}));
}

async function renderRestore(req, res) {
    res.send(ejs.render(TEMPLATE_RESTORE, {}));
}

async function renderActivation(req, res) {
    res.send(ejs.render(TEMPLATE_ACTIVATION, {}));
}

export default {
    getConfig,

    startPrepareLocation,
    changeLocation,

    renderIndex,
    renderFilemanager,
    renderLogs,
    renderTerminal,
    renderPasswordreset,
    renderSetupaccount,
    renderSetup,
    renderRestore,
    renderActivation,
};
