"use strict";

const express = require("express"),
  fs = require("fs"),
  url = require("url"),
  config = require("config"),
  moment = require("moment");

let photoDB;

require("../db/photos").then(function(db) {
  photoDB = db;
});

const router = express.Router();

/* Each photos has:

* locations
* people
* date
* tags
* photo info

*/

router.get("/*", function(req, res/*, next*/) {
  const path = decodeURI(req.url).replace(/\?.*$/, "").replace(/^\//, "");
    
  console.log("Looking for daily photo counts in '" + path + "'");
  return photoDB.sequelize.query(
    "SELECT DATE(photos.taken) AS date,COUNT(photos.id) AS count FROM albums " +
    "JOIN photos ON photos.deleted=0 AND photos.duplicate=0 AND photos.taken IS NOT NULL AND photos.albumId=albums.id " +
    "WHERE albums.path LIKE :path GROUP BY DATE(photos.taken) ORDER BY date DESC", {
    replacements: {
      path: path + "%"
    },
    type: photoDB.Sequelize.QueryTypes.SELECT,
    raw: true
  }).then(function(days) {
    return res.status(200).json(days);
  }).catch(function(error) {
    return Promise.reject(error);
  });
});

module.exports = router;
