class <%= class_name %>ItemsController < ApplicationController
  before_action :set_<%= file_name %>_item, only: [:show, :edit, :update, :destroy, :sync, :setup_accounts, :complete_account_setup]

  def index
    @<%= table_name %> = Current.family.<%= table_name %>.ordered
  end

  def show
  end

  def new
    @<%= file_name %>_item = Current.family.<%= table_name %>.build
  end

  def edit
  end

  def create
    @<%= file_name %>_item = Current.family.<%= table_name %>.build(<%= file_name %>_item_params)
    @<%= file_name %>_item.name ||= "<%= class_name %> Connection"

    if @<%= file_name %>_item.save
      if turbo_frame_request?
        flash.now[:notice] = t(".success", default: "Successfully configured <%= class_name %>.")
        @<%= table_name %> = Current.family.<%= table_name %>.ordered
        render turbo_stream: [
          turbo_stream.replace(
            "<%= file_name %>-providers-panel",
            partial: "settings/providers/<%= file_name %>_panel",
            locals: { <%= file_name %>_items: @<%= table_name %> }
          ),
          *flash_notification_stream_items
        ]
      else
        redirect_to settings_providers_path, notice: t(".success"), status: :see_other
      end
    else
      @error_message = @<%= file_name %>_item.errors.full_messages.join(", ")

      if turbo_frame_request?
        render turbo_stream: turbo_stream.replace(
          "<%= file_name %>-providers-panel",
          partial: "settings/providers/<%= file_name %>_panel",
          locals: { error_message: @error_message }
        ), status: :unprocessable_entity
      else
        redirect_to settings_providers_path, alert: @error_message, status: :unprocessable_entity
      end
    end
  end

  def update
    if @<%= file_name %>_item.update(<%= file_name %>_item_params)
      if turbo_frame_request?
        flash.now[:notice] = t(".success", default: "Successfully updated <%= class_name %> configuration.")
        @<%= table_name %> = Current.family.<%= table_name %>.ordered
        render turbo_stream: [
          turbo_stream.replace(
            "<%= file_name %>-providers-panel",
            partial: "settings/providers/<%= file_name %>_panel",
            locals: { <%= file_name %>_items: @<%= table_name %> }
          ),
          *flash_notification_stream_items
        ]
      else
        redirect_to settings_providers_path, notice: t(".success"), status: :see_other
      end
    else
      @error_message = @<%= file_name %>_item.errors.full_messages.join(", ")

      if turbo_frame_request?
        render turbo_stream: turbo_stream.replace(
          "<%= file_name %>-providers-panel",
          partial: "settings/providers/<%= file_name %>_panel",
          locals: { error_message: @error_message }
        ), status: :unprocessable_entity
      else
        redirect_to settings_providers_path, alert: @error_message, status: :unprocessable_entity
      end
    end
  end

  def destroy
    @<%= file_name %>_item.destroy_later
    redirect_to settings_providers_path, notice: t(".success", default: "Scheduled <%= class_name %> connection for deletion.")
  end

  def sync
    unless @<%= file_name %>_item.syncing?
      @<%= file_name %>_item.sync_later
    end

    respond_to do |format|
      format.html { redirect_back_or_to accounts_path }
      format.json { head :ok }
    end
  end

  # Collection actions for account linking flow
  # TODO: Implement these when you have the provider SDK ready

  def preload_accounts
    # TODO: Fetch accounts from provider API and cache them
    redirect_to settings_providers_path, alert: "Not implemented yet"
  end

  def select_accounts
    # TODO: Show UI to select which accounts to link
    @accountable_type = params[:accountable_type]
    @return_to = params[:return_to]
    redirect_to settings_providers_path, alert: "Not implemented yet"
  end

  def link_accounts
    # TODO: Link selected accounts
    redirect_to settings_providers_path, alert: "Not implemented yet"
  end

  def select_existing_account
    # TODO: Show UI to link an existing account to provider
    @account_id = params[:account_id]
    redirect_to settings_providers_path, alert: "Not implemented yet"
  end

  def link_existing_account
    # TODO: Link an existing account to a provider account
    redirect_to settings_providers_path, alert: "Not implemented yet"
  end

  def setup_accounts
    # TODO: Show account setup UI
    redirect_to settings_providers_path, alert: "Not implemented yet"
  end

  def complete_account_setup
    # TODO: Complete the account setup process
    redirect_to settings_providers_path, alert: "Not implemented yet"
  end

  private

  def set_<%= file_name %>_item
    @<%= file_name %>_item = Current.family.<%= table_name %>.find(params[:id])
  end

  def <%= file_name %>_item_params
    params.require(:<%= file_name %>_item).permit(
      :name,
      :sync_start_date<% parsed_fields.each do |field| %>,
      :<%= field[:name] %><% end %>
    )
  end
end
