'use strict';

const { get, geta } = require('../utils/q');

function scrapeLatest(scenes, site) {
    return scenes.map(({ q, qa, qu, qd }) => {
        const release = {};

        release.title = q('.title a', true);

        const pathname = qu('.title a');
        release.entryId = pathname.split('/')[3];
        release.url = `${site.url}${pathname}`;

        release.date = qd('.date', 'MMM DD, YYYY');
        release.actors = qa('.models a.model', true);

        release.poster = q('img').dataset.original;

        release.stars = Number(q('.rating', true));
        release.likes = Number(q('.likes', true));

        console.log(release);
        return release;
    });
}

function scrapeScene({ q }, _site) {
    const release = {};

    console.log(release);
    return release;
}

async function fetchLatest(site, page = 1) {
    const url = `${site.url}/${page}`;
    const qLatest = await geta(url, '.selector');

    return qLatest && scrapeLatest(qLatest, site);
}

async function fetchScene(url, site) {
    const qScene = await get(url);

    return qScene && scrapeScene(qScene, site);
}

module.exports = {
    fetchLatest,
    fetchScene,
};
