'use strict';

const Promise = require('bluebird');
const knex = require('../knex');

async function listSites() {
    const [networks, allSites] = await Promise.all([
        knex('networks').orderBy('name'),
        knex('sites').orderBy('name'),
    ]);

    await Promise.each(networks, async (network) => {
        console.log(`* **${network.name}**`);

        const sites = await knex('sites')
            .where({ network_id: network.id })
            .orderBy('name');

        if (sites.length === 1 && sites[0].name === network.name) {
            return;
        }

        sites.forEach((site) => {
            const rkSpecial = network.id === 'realitykings'
                && (new URL(site.url).hostname === 'www.realitykings.com'
                || (site.parameters?.altLayout))
                ? '\\*' : ''; // Reality Kings alt layout sites do not support scene fetch by URL

            console.log(`    * ${site.name}${rkSpecial}`);
        });
    });

    console.log(`${networks.length} networks with ${allSites.length} sites total`);
}

listSites();
