# frozen_string_literal: true

require 'teamsnap'
require 'net/http'
require 'uri'
require 'date'

class TeamController < ApplicationController
  include SessionsHelper
  before_action :logged_in_user

  def show
    team_id = params[:team_id]
    session[:team_id] = team_id
    team = TeamSnap::Team.find(@teamsnap_client, team_id)
    upcoming_events = TeamSnap::Event.search(@teamsnap_client, {
      team_id: team_id, started_after: Date.today.strftime("%Y-%m-%d"), page_size: 4
    })
    past_events = TeamSnap::Event.search(@teamsnap_client, {
      team_id: team_id, started_before: Date.today.strftime("%Y-%m-%d"), page_size: 4, sort_start_date: "desc"
    })
    
    items = TeamSnap::AvailabilitySummary.search(@teamsnap_client, {:event_id => upcoming_events.map{|e|e.id}.join(",")})
    
    render 'show', locals: { team: team, upcoming_events: upcoming_events, past_events: past_events, availability_summaries: items }
  end

  def list
    teams = TeamSnap::Team.search(@teamsnap_client, { user_id: @user_id })
    render 'list', locals: { teams: teams }
  end
end
