<% content_for :title, 'Account' %>

<div class="hero min-h-content bg-base-200">
  <div class="hero-content flex-col lg:flex-row-reverse w-full my-5">
    <div class="text-center lg:text-left">
      <h1 class="text-5xl font-bold mb-5">Edit your account!</h1>
      <%= render 'devise/registrations/api_key' %>
      <% if current_user.trial? %>
        <p>Your trial period ends at <%= human_datetime current_user.active_until %>.</p>
        <p>
          <%= link_to 'Subscribe', "#{MANAGER_URL}/auth/dawarich?token=#{current_user.generate_subscription_token}", class: 'btn btn-sm btn-success glass' %> to continue using Dawarich after your trial ends.
        </p>
      <% end %>
      <% if !DawarichSettings.self_hosted? %>
        <%= render 'devise/registrations/points_usage' %>
      <% end %>
    </div>
    <div class="card flex-shrink-0 w-full max-w-sm shadow-2xl bg-base-100 px-5 py-5">
      <%= form_for(resource, as: resource_name, url: registration_path(resource_name), class: 'form-body', method: :put, data: { turbo_method: :put, turbo: false }) do |f| %>
        <%= render "devise/shared/error_messages", resource: resource %>

        <div class="form-control">
          <%= f.label :email, class: 'label' do %>
            <span class="label-text">Email</span>
          <% end %>
          <%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'input input-bordered' %>
        </div>

        <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
          <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
        <% end %>

        <div class="form-control mt-5">
          <%= f.label :password, class: 'label' do %>
            <span class="label-text">Password <i>(leave blank if you don't want to change it)</i></span>
          <% end %>
          <% if @minimum_password_length %>
            <em class='text-xs'>(<%= @minimum_password_length %> characters minimum)</em>
          <% end %>
          <%= f.password_field :password, autocomplete: "new-password", class: 'input input-bordered' %>
        </div>

        <div class="form-control mt-5">
          <%= f.label :password_confirmation, class: 'label' do %>
            <span class="label-text">Password confirmation</span>
          <% end %>
          <% if @minimum_password_length %>
            <em class='text-xs'>(<%= @minimum_password_length %> characters minimum)</em>
          <% end %>
          <%= f.password_field :password_confirmation, autocomplete: "new-password", class: 'input input-bordered' %>
        </div>

        <div class="form-control mt-5">
          <%= f.label :current_password, class: 'label' do %>
            <span class="label-text">Current password</span>
          <% end %>
          <% if @minimum_password_length %>
            <em class='text-xs'>(<%= @minimum_password_length %> characters minimum)</em>
          <% end %><br />
          <i class='text-xs'>(we need your current password to confirm your changes)</i>
          <%= f.password_field :current_password, autocomplete: "current-password", class: 'input input-bordered' %>
        </div>

        <div class="form-control mt-6">
          <%= f.submit "Update", class: 'btn btn-primary' %>
        </div>
      <% end %>

      <%= render "devise/shared/links" %>

      <p class='mt-3'>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), data: { turbo_confirm: "Are you sure?", turbo_method: :delete }, method: :delete, class: 'btn' %></p>
      <div class="divider"></div>
      <p class='mt-3 flex flex-col gap-2'>
        <%= link_to "Export my data", export_settings_users_path, class: 'btn btn-primary', data: {
            turbo_confirm: "Are you sure you want to export your data?",
            turbo_method: :get
          } %>
        <button class='btn btn-primary' onclick="import_modal.showModal()">Import my data</button>
      </p>

      <!-- Import Data Modal -->
      <dialog id="import_modal" class="modal">
        <div class="modal-box">
          <h3 class="font-bold text-lg mb-4">Import your data</h3>
          <p class="mb-4 text-sm text-gray-600">Upload a ZIP file containing your exported Dawarich data to restore your points, trips, and settings.</p>

          <%= form_with url: import_settings_users_path, method: :post, multipart: true, class: 'space-y-4', data: {
            turbo: false,
            controller: "user-data-archive-direct-upload",
            user_data_archive_direct_upload_url_value: rails_direct_uploads_url,
            user_data_archive_direct_upload_user_trial_value: current_user.trial?,
            user_data_archive_direct_upload_target: "form"
          } do |f| %>
            <div class="form-control">
              <%= f.label :archive, class: 'label' do %>
                <span class="label-text">Select ZIP archive</span>
              <% end %>
              <%= f.file_field :archive,
                  accept: '.zip',
                  required: true,
                  direct_upload: true,
                  class: 'file-input file-input-bordered w-full',
                  data: { user_data_archive_direct_upload_target: "input" } %>
              <div class="text-sm text-gray-500 mt-2">
                File will be uploaded directly to storage. Please be patient during upload.
              </div>
            </div>

            <div class="modal-action">
              <%= f.submit "Import Data",
                  class: 'btn btn-primary',
                  data: {
                    disable_with: 'Importing...',
                    user_data_archive_direct_upload_target: "submit"
                  } %>
              <button type="button" class="btn" onclick="import_modal.close()">Cancel</button>
            </div>
          <% end %>
        </div>
        <form method="dialog" class="modal-backdrop">
          <button>close</button>
        </form>
      </dialog>
    </div>
  </div>
</div>
