<%
  application = Program.find_by(uid: @pre_auth.client.uid)
  scenario_class = application&.onboarding_scenario_class
  scenario = scenario_class&.new(current_identity)
  custom_logo_path = scenario&.logo_path
%>
<%= render "shared/scenario_background_style", scenario: %>
<div class="auth-container<%= ' has-background' if scenario&.background_path || scenario&.dark_mode_background_path %>">
  <div class="auth-brand">
    <%= vite_image_tag "images/hc-square.png", alt: "Hack Club logo", class: "brand-logo" %>
    <% if custom_logo_path %>
      <span class="brand-plus">×</span>
      <%= vite_image_tag custom_logo_path, alt: "Logo", class: "brand-logo brand-logo--custom" %>
    <% end %>
    <span class="brand-text"><%= t("brand") %></span>
  </div>
  <div class="auth-card oauth-consent">
    <header>
      <h1><%= session.dig(:stashed_data, "splash_message") || "Continue to #{@pre_auth.client.name}?" %></h1>
      <small>
        <%= raw t('.prompt', client_name: content_tag(:strong) { @pre_auth.client.name }) %>
      </small>
    </header>

    <% if @pre_auth.scopes.count > 0 %>
      <%
        scopes = @pre_auth.scopes.map(&:to_s)
        not_set = t('.data.not_set')
        scope_data = OAuthScope.consent_data_by_scope(scopes, current_identity)
        unknown_scopes = scopes.reject { |s| OAuthScope.known?(s) }
      %>

      <% if scope_data.any? || unknown_scopes.any? %>
        <div class="oauth-permissions">
          <fieldset>
            <legend><%= t('.able_to') %></legend>
            <ul class="permissions-list">
              <% scope_data.each do |scope_name, data| %>
                <li class="permission-item<%= ' permission-item--action' unless data[:fields].any? %>">
                  <div class="permission-header">
                    <% if data[:icon] %><%= inline_icon(data[:icon], size: 16) %><% end %>
                    <span class="permission-title"><%= t("#{scope_name}.title", scope: '.scopes', default: data[:description]) %></span>
                  </div>
                  <% if data[:fields].any? %>
                    <ul class="permission-details">
                      <% data[:fields].each do |field| %>
                        <li>
                          <span><%= t("#{scope_name}.#{field[:key]}_label", scope: '.scopes', default: t(".data.#{field[:key]}") + ":") %></span>
                          <%= field[:value] || not_set %>
                        </li>
                      <% end %>
                    </ul>
                  <% end %>
                </li>
              <% end %>
              <% unknown_scopes.each do |scope| %>
                <li class="permission-item permission-item--action">
                  <span class="permission-title"><%= t(scope, scope: [:doorkeeper, :scopes], default: scope.humanize) %></span>
                </li>
              <% end %>
            </ul>
          </fieldset>
        </div>
      <% end %>
    <% end %>

    <% auth_label = t('.authorize') %>
    <div class="oauth-actions" x-data="{ countdown: <%= application&.hq_official? ? 0 : 3 %>, ready: <%= application&.hq_official? ? true : false %>, authLabel: '<%= auth_label.gsub("'", "\\'") %>' }" x-init="if (countdown > 0) { let interval = setInterval(() => { countdown--; if (countdown <= 0) { ready = true; clearInterval(interval); } }, 1000); }">
      <%= form_tag oauth_authorization_path, method: :delete do %>
        <%= hidden_field_tag :client_id, @pre_auth.client.uid, id: nil %>
        <%= hidden_field_tag :redirect_uri, @pre_auth.redirect_uri, id: nil %>
        <%= hidden_field_tag :state, @pre_auth.state, id: nil %>
        <%= hidden_field_tag :response_type, @pre_auth.response_type, id: nil %>
        <%= hidden_field_tag :response_mode, @pre_auth.response_mode, id: nil %>
        <%= hidden_field_tag :scope, @pre_auth.scope, id: nil %>
        <%= hidden_field_tag :nonce, @pre_auth.nonce, id: nil %>
        <%= hidden_field_tag :code_challenge, @pre_auth.code_challenge, id: nil %>
        <%= hidden_field_tag :code_challenge_method, @pre_auth.code_challenge_method, id: nil %>
        <%= submit_tag t('.deny'), class: "secondary delete" %>
      <% end %>

      <%= form_tag oauth_authorization_path, method: :post do %>
        <%= hidden_field_tag :client_id, @pre_auth.client.uid, id: nil %>
        <%= hidden_field_tag :redirect_uri, @pre_auth.redirect_uri, id: nil %>
        <%= hidden_field_tag :state, @pre_auth.state, id: nil %>
        <%= hidden_field_tag :response_type, @pre_auth.response_type, id: nil %>
        <%= hidden_field_tag :response_mode, @pre_auth.response_mode, id: nil %>
        <%= hidden_field_tag :scope, @pre_auth.scope, id: nil %>
        <%= hidden_field_tag :nonce, @pre_auth.nonce, id: nil %>
        <%= hidden_field_tag :code_challenge, @pre_auth.code_challenge, id: nil %>
        <%= hidden_field_tag :code_challenge_method, @pre_auth.code_challenge_method, id: nil %>
        <%= submit_tag auth_label, "x-bind:disabled": "!ready", "x-bind:value": "ready ? authLabel : `Wait ${countdown}s...`", class: "approve" %>
      <% end %>
    </div>

    <% case application&.trust_level %>
    <% when "community_untrusted" %>
      <div class="auth-notice">
        <%= render Components::Banner.new(kind: :warning) do %>
          <strong><%= t('.banners.community_untrusted.title') %></strong><br>
          <%= raw t('.banners.community_untrusted.message_html') %>
        <% end %>
      </div>
    <% when "community_trusted" %>
      <div class="auth-notice">
        <%= render Components::Banner.new(kind: :info) do %>
          <strong><%= t('.banners.community_trusted.title') %></strong> <br>
          <%= raw t('.banners.community_trusted.message_html') %>
        <% end %>
      </div>
    <% end %>
  </div>
</div>
