require "./base.cr"

module Invidious::Database::Videos
  extend self

  def insert(video : Video)
    request = <<-SQL
      INSERT INTO videos
      VALUES ($1, $2, $3)
      ON CONFLICT (id) DO NOTHING
    SQL

    REDIS_DB.set(video.id, video.info.to_json, ex: 14400)
    REDIS_DB.set(video.id + ":time", video.updated, ex: 14400)
  end

  def delete(id)
    request = <<-SQL
      DELETE FROM videos *
      WHERE id = $1
    SQL

    REDIS_DB.del(id)
    REDIS_DB.del(id + ":time")
  end

  def delete_expired
    request = <<-SQL
      DELETE FROM videos *
      WHERE updated < (now() - interval '6 hours')
    SQL

    PG_DB.exec(request)
  end

  def update(video : Video)
    request = <<-SQL
      UPDATE videos
      SET (id, info, updated) = ($1, $2, $3)
      WHERE id = $1
    SQL

    PG_DB.exec(request, video.id, video.info.to_json, video.updated)
  end

  def select(id : String) : Video?
    request = <<-SQL
      SELECT * FROM videos
      WHERE id = $1
    SQL

    if ((info = REDIS_DB.get(id)) && (time = REDIS_DB.get(id + ":time")))
      return Video.new({
        id:      id,
        info:    JSON.parse(info).as_h,
        updated: Time.parse(time, "%Y-%m-%d %H:%M:%S %z", Time::Location::UTC),
      })
    else
      return nil
    end
  end
end
