require 'sinatra/base'
require 'sinatra/namespace'
require 'sinatra/activerecord'
require './mood'

class MoodApp < Sinatra::Base
  register Sinatra::ActiveRecordExtension
  register Sinatra::Namespace
  set :database, { adapter: 'sqlite3', database: "mood-#{ENV['RACK_ENV']}.sqlite3" }

  get '/' do
    @last_mood = Mood.last
    @mood_log = Mood.log
    erb :index
  end

  namespace '/api/v1' do
    before do
      content_type 'application/json'
    end

    post '/moods' do
      json = request.body.read
      puts json
      data = JSON.parse json
      Mood.create(mode: data['mode'], recorded_at: Time.now)
    end
  end

  run! if app_file == $0
end
