# frozen_string_literal: true

class <%= class_name %>Item < ApplicationRecord
  include Syncable, Provided, Unlinking

  enum :status, { good: "good", requires_update: "requires_update" }, default: :good

  # Helper to detect if ActiveRecord Encryption is configured for this app
  def self.encryption_ready?
    creds_ready = Rails.application.credentials.active_record_encryption.present?
    env_ready = ENV["ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY"].present? &&
                ENV["ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY"].present? &&
                ENV["ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT"].present?
    creds_ready || env_ready
  end

  # Encrypt sensitive credentials if ActiveRecord encryption is configured
  if encryption_ready?
<% parsed_fields.select { |f| f[:secret] }.each do |field| -%>
    encrypts :<%= field[:name] %>, deterministic: true
<% end -%>
  end

  validates :name, presence: true
<% parsed_fields.select { |f| f[:secret] }.each do |field| -%>
  validates :<%= field[:name] %>, presence: true, on: :create
<% end -%>

  belongs_to :family
  has_one_attached :logo, dependent: :purge_later

  has_many :<%= file_name %>_accounts, dependent: :destroy
  has_many :accounts, through: :<%= file_name %>_accounts

  scope :active, -> { where(scheduled_for_deletion: false) }
  scope :ordered, -> { order(created_at: :desc) }
  scope :needs_update, -> { where(status: :requires_update) }

  def syncer
    <%= class_name %>Item::Syncer.new(self)
  end

  def destroy_later
    update!(scheduled_for_deletion: true)
    DestroyJob.perform_later(self)
  end

<% if investment_provider? -%>
  # Override syncing? to include background activities fetch
  def syncing?
    super || <%= file_name %>_accounts.where(activities_fetch_pending: true).exists?
  end
<% end -%>

  # Import data from provider API
  def import_latest_<%= file_name %>_data(sync: nil)
    provider = <%= file_name %>_provider
    unless provider
      Rails.logger.error "<%= class_name %>Item #{id} - Cannot import: provider is not configured"
      raise StandardError, I18n.t("<%= file_name %>_items.errors.provider_not_configured")
    end

    <%= class_name %>Item::Importer.new(self, <%= file_name %>_provider: provider, sync: sync).import
  rescue => e
    Rails.logger.error "<%= class_name %>Item #{id} - Failed to import data: #{e.message}"
    raise
  end

  # Process linked accounts after data import
  def process_accounts
    return [] if <%= file_name %>_accounts.empty?

    results = []
    linked_<%= file_name %>_accounts.includes(account_provider: :account).each do |<%= file_name %>_account|
      begin
        result = <%= class_name %>Account::Processor.new(<%= file_name %>_account).process
        results << { <%= file_name %>_account_id: <%= file_name %>_account.id, success: true, result: result }
      rescue => e
        Rails.logger.error "<%= class_name %>Item #{id} - Failed to process account #{<%= file_name %>_account.id}: #{e.message}"
        results << { <%= file_name %>_account_id: <%= file_name %>_account.id, success: false, error: e.message }
      end
    end

    results
  end

  # Schedule sync jobs for all linked accounts
  def schedule_account_syncs(parent_sync: nil, window_start_date: nil, window_end_date: nil)
    return [] if accounts.empty?

    results = []
    accounts.visible.each do |account|
      begin
        account.sync_later(
          parent_sync: parent_sync,
          window_start_date: window_start_date,
          window_end_date: window_end_date
        )
        results << { account_id: account.id, success: true }
      rescue => e
        Rails.logger.error "<%= class_name %>Item #{id} - Failed to schedule sync for account #{account.id}: #{e.message}"
        results << { account_id: account.id, success: false, error: e.message }
      end
    end

    results
  end

  def upsert_<%= file_name %>_snapshot!(accounts_snapshot)
    assign_attributes(
      raw_payload: accounts_snapshot
    )

    save!
  end

  def has_completed_initial_setup?
    accounts.any?
  end

  # Linked accounts (have AccountProvider association)
  def linked_<%= file_name %>_accounts
    <%= file_name %>_accounts.joins(:account_provider)
  end

  # Unlinked accounts (no AccountProvider association)
  def unlinked_<%= file_name %>_accounts
    <%= file_name %>_accounts.left_joins(:account_provider).where(account_providers: { id: nil })
  end

  def sync_status_summary
    total_accounts = total_accounts_count
    linked_count = linked_accounts_count
    unlinked_count = unlinked_accounts_count

    if total_accounts == 0
      I18n.t("<%= file_name %>_items.sync_status.no_accounts")
    elsif unlinked_count == 0
      I18n.t("<%= file_name %>_items.sync_status.synced", count: linked_count)
    else
      I18n.t("<%= file_name %>_items.sync_status.synced_with_setup", linked: linked_count, unlinked: unlinked_count)
    end
  end

  def linked_accounts_count
    <%= file_name %>_accounts.joins(:account_provider).count
  end

  def unlinked_accounts_count
    <%= file_name %>_accounts.left_joins(:account_provider).where(account_providers: { id: nil }).count
  end

  def total_accounts_count
    <%= file_name %>_accounts.count
  end

  def institution_display_name
    institution_name.presence || institution_domain.presence || name
  end

  def connected_institutions
    <%= file_name %>_accounts.includes(:account)
                  .where.not(institution_metadata: nil)
                  .map { |acc| acc.institution_metadata }
                  .uniq { |inst| inst["name"] || inst["institution_name"] }
  end

  def institution_summary
    institutions = connected_institutions
    case institutions.count
    when 0
      I18n.t("<%= file_name %>_items.institution_summary.none")
    else
      I18n.t("<%= file_name %>_items.institution_summary.count", count: institutions.count)
    end
  end

  def credentials_configured?
<% if parsed_fields.select { |f| f[:secret] }.any? -%>
    <%= parsed_fields.select { |f| f[:secret] }.map { |f| "#{f[:name]}.present?" }.join(" && ") %>
<% else -%>
    true
<% end -%>
  end
<% parsed_fields.select { |f| f[:default] }.each do |field| %>
  def effective_<%= field[:name] %>
    <%= field[:name] %>.presence || "<%= field[:default] %>"
  end
<% end -%>
end
