<% content_for :title do %>
  Edit <%= @application.name %> - Admin
<% end %>

<div class="max-w-3xl mx-auto p-6 space-y-6">
  <header class="text-center mb-8">
    <h1 class="text-4xl font-bold text-surface-content mb-2">Edit Application</h1>
    <p class="text-secondary text-lg">
      Update settings for <%= @application.name %>
      <% if @application.verified? %>
        <span class="inline-flex items-center gap-1 px-2 py-0.5 bg-green/20 text-green border border-green/30 rounded text-sm ml-1">
          <svg class="w-3 h-3" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
          </svg>
          Verified
        </span>
      <% end %>
    </p>
  </header>

  <div class="bg-blue/10 border border-blue/30 rounded-lg p-4 mb-6">
    <div class="flex items-start gap-3">
      <svg class="w-5 h-5 text-blue shrink-0 mt-0.5" fill="currentColor" viewBox="0 0 20 20">
        <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />
      </svg>
      <div>
        <p class="text-blue font-medium">Super Admin Edit</p>
        <p class="text-blue/80 text-sm mt-1">As a super admin, you can edit all fields including the name of verified applications.</p>
      </div>
    </div>
  </div>

  <%= form_for @application, url: admin_oauth_application_path(@application), method: :patch, as: :oauth_application, html: { class: 'space-y-6' } do |f| %>
    <% if @application.errors.any? %>
      <div class="p-4 bg-red/10 border border-red/20 rounded-lg">
        <div class="flex items-center gap-2 text-red mb-2">
          <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
          </svg>
          <span class="font-medium">Error</span>
        </div>
        <ul class="list-disc list-inside text-red/80 text-sm">
          <% @application.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
        </ul>
      </div>
    <% end %>

    <div class="border border-primary rounded-xl p-6 bg-dark">
      <div class="flex items-center gap-3 mb-6">
        <div class="p-2 bg-primary/10 rounded">
          <svg class="w-6 h-6 text-primary" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
          </svg>
        </div>
        <h2 class="text-xl font-semibold text-surface-content">Application Details</h2>
      </div>

      <div class="space-y-5">
        <div>
          <%= f.label :name, class: 'block text-sm font-medium text-surface-content mb-2' %>
          <%= f.text_field :name, class: 'w-full px-3 py-2 bg-darkless border border-darkless rounded text-surface-content focus:border-primary focus:ring-1 focus:ring-primary placeholder-secondary', placeholder: 'My Awesome App', required: true %>
          <% if @application.errors[:name].present? %>
            <p class="mt-1 text-xs text-red"><%= @application.errors[:name].to_sentence %></p>
          <% end %>
        </div>

        <div>
          <%= f.label :redirect_uri, 'Redirect URIs', class: 'block text-sm font-medium text-surface-content mb-2' %>
          <%= f.text_area :redirect_uri, class: 'w-full px-3 py-2 bg-darkless border border-darkless rounded text-surface-content focus:border-primary focus:ring-1 focus:ring-primary placeholder-secondary font-mono text-sm', placeholder: 'https://example.com/auth/callback', rows: 3 %>
          <% if @application.errors[:redirect_uri].present? %>
            <p class="mt-1 text-xs text-red"><%= @application.errors[:redirect_uri].to_sentence %></p>
          <% end %>
        </div>

        <div>
          <%= f.label :scopes, class: 'block text-sm font-medium text-surface-content mb-2' %>
          <%= f.text_field :scopes, class: 'w-full px-3 py-2 bg-darkless border border-darkless rounded text-surface-content focus:border-primary focus:ring-1 focus:ring-primary placeholder-secondary font-mono text-sm', placeholder: 'profile read' %>
          <% if @application.errors[:scopes].present? %>
            <p class="mt-1 text-xs text-red"><%= @application.errors[:scopes].to_sentence %></p>
          <% end %>
        </div>

        <div class="flex items-start gap-3 p-4 bg-darkless border border-darkless rounded">
          <%= f.check_box :confidential, class: 'mt-0.5 w-4 h-4 text-primary border-darkless rounded focus:ring-primary bg-darker' %>
          <div>
            <%= f.label :confidential, 'Confidential Application', class: 'text-sm font-medium text-surface-content' %>
            <p class="mt-1 text-xs text-secondary">Confidential clients can keep secrets. Native apps and SPAs are not confidential.</p>
          </div>
        </div>
      </div>
    </div>

    <div class="flex items-center gap-3">
      <%= f.submit 'Save Changes', class: 'px-6 py-2 bg-primary text-on-primary font-medium rounded transition-colors duration-200 hover:opacity-90 cursor-pointer' %>
      <%= link_to 'Cancel', admin_oauth_application_path(@application), class: 'px-6 py-2 border border-darkless text-surface-content font-medium rounded transition-colors duration-200 hover:bg-darkless' %>
    </div>
  <% end %>
</div>
