module Api
  module V1
    class TeamsController < BaseController
      def index
        teams = current_user.streamable_teams
        render json: teams.map { |t| team_json(t) }
      end

      def show
        team = current_user.manageable_teams.find(params[:id])
        render json: team_json(team, detail: true)
      end

      def create
        if current_user.owned_clubs.exists?
          return render json: {
            error: "Registra o gestisci la società dal sito web per aggiungere squadre."
          }, status: :unprocessable_entity
        end

        club = Club.create!(name: team_params[:name], sport: team_params[:sport] || "volleyball",
                            primary_color: "#e53935", secondary_color: "#ffffff")
        ClubMembership.create!(user: current_user, club: club, role: "owner")
        team = club.teams.create!(name: team_params[:name], sport: club.sport)
        Billing::AssignPlan.call(club: club, plan_slug: "free")
        render json: team_json(team), status: :created
      end

      def recordings
        team = current_user.streamable_teams.find { |t| t.id.to_s == params[:id].to_s }
        raise ActiveRecord::RecordNotFound unless team
        unless team.entitlements.can_access_recordings?
          return render json: {
            error: "Archivio gare disponibile con Premium Light o Full",
            error_code: "premium_required",
            billing_url: team.entitlements.billing_url
          }, status: :forbidden
        end

        recs = team.recordings.ready.includes(stream_session: :match).order(created_at: :desc).limit(50)
        render json: recs.map { |r| recording_json(r) }
      end

      def update
        team = current_user.manageable_teams.find(params[:id])
        team.update!(team_params)
        render json: team_json(team)
      end

      def add_member
        team = current_user.manageable_teams.find(params[:id])
        team.entitlements.assert_can_invite!
        member = User.find(params[:user_id])
        UserTeam.find_or_create_by!(user: member, team: team) { |ut| ut.role = "member" }
        head :no_content
      end

      def remove_member
        team = current_user.manageable_teams.find(params[:id])
        ut = team.user_teams.find_by!(user_id: params[:user_id], role: "member")
        ut.destroy!
        head :no_content
      end

      private

      def team_params
        params.require(:team).permit(:name, :sport, :logo_url)
      end

      def team_json(team, detail: false)
        ent = team.entitlements
        data = {
          id: team.id,
          name: team.name,
          sport: team.sport,
          logo_url: team.effective_logo_url,
          primary_color: team.effective_primary_color,
          secondary_color: team.effective_secondary_color,
          club_id: team.club_id,
          club_name: team.club.name,
          youtube_connected: team.youtube_credential.present?,
          plan_slug: ent.plan.slug,
          plan_name: ent.plan.name,
          premium_active: ent.premium_active?,
          premium_full: ent.premium_full?,
          subscription_status: ent.subscription.status,
          features: ent.plan.features,
          billing_url: ent.billing_url,
          staff_manage_url: ent.staff_manage_url,
          max_staff: ent.max_staff,
          max_staff_transmission: ent.max_staff_transmission,
          staff_used: ent.staff_count,
          staff_transmission_used: ent.staff_count_for("transmission"),
          concurrent_streams_used: ent.concurrent_streams_used,
          concurrent_streams_limit: ent.concurrent_streams_limit,
          recordings_enabled: ent.can_access_recordings?,
          phone_download_enabled: ent.phone_download_enabled?,
          youtube_enabled: ent.youtube_enabled?,
          youtube_mode: ent.plan.youtube_mode,
          staff_role: current_user.staff_role_for(team),
          can_stream: current_user.can_stream_for?(team)
        }
        if detail
          data[:members] = team.user_teams.includes(:user).where.not(role: "owner").map do |ut|
            { id: ut.user.id, name: ut.user.name, email: ut.user.email, role: ut.role }
          end
        end
        data
      end

      def recording_json(recording)
        session = recording.stream_session
        match = session.match
        {
          id: recording.id,
          session_id: session.id,
          match_id: match.id,
          opponent_name: match.opponent_name,
          team_name: match.team.name,
          ended_at: session.ended_at,
          replay_url: recording.replay_url,
          download_url: recording.replay_url,
          expires_at: recording.expires_at
        }
      end
    end
  end
end
