import { knex } from "../lib/ormish.js";
import { Media } from "../lib/models.js";
import { base_host, site_name } from "../client/config.js";
import fs from "fs";
import _ from "lodash";

export const description = "Generates the bare player.html files for the Twitter cards.";

export const options = [
  ["--target <dirwithslash>", "directory to write files (end with /)", "./public/video/"],
  ["--template <filename>", "file to use for template", "rendered/player.html"]
];

export const main = async (opts) => {
  const player = _.template(fs.readFileSync(opts.template));

  if(!opts.target.endsWith("/")) {
    console.error(`--target path must end in slash / but you have: ${ opts.target }`);
    process.exit(1);
  }

  const all_media = await Media.all(knex.raw("title is not null"));

  for(let media of all_media) {
    const html = player({ base_host, site_name, media });
    const outname = `${ opts.target }${ media.id }-${ media.slug }/player.html`;

    fs.writeFileSync(outname, html);
  }

  process.exit(0);
}

export default { main };
