# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'Mobile > Ticket > Information > Accounted Time', app: :mobile, authenticated_as: :authenticate, type: :system do
  let(:group)                { create(:group) }
  let(:ticket)               { create(:ticket, group: group) }
  let(:article)              { create(:ticket_article, ticket: ticket) }
  let(:time_unit)            { Faker::Number.unique.decimal(l_digits: 1, r_digits: 1) }
  let(:time_accounting)      { create(:'ticket/time_accounting', ticket: ticket, ticket_article: article, time_unit: time_unit) }
  let(:time_accounting_unit) { '' }
  let(:display_unit)         { nil }

  let(:accounted_time_element) do
    find('section', text: 'Total accounted time')
  end

  def authenticate
    Setting.set('time_accounting', true)
    Setting.set('time_accounting_unit', time_accounting_unit) if time_accounting_unit.present?
    Setting.set('time_accounting_unit_custom', display_unit) if time_accounting_unit == 'custom'

    time_accounting

    user
  end

  before do
    visit "/tickets/#{ticket.id}/information"
  end

  shared_examples 'showing accounted time' do |time_accounting_unit = '', display_unit = nil|
    let(:time_accounting_unit) { time_accounting_unit }
    let(:display_unit)         { display_unit }

    it 'shows accounted time', if: !display_unit do
      expect(accounted_time_element).to have_text(format('%.2f', time_unit))
    end

    it "shows accounted time in #{display_unit}", if: display_unit do
      expect(accounted_time_element).to have_text("#{format('%.2f', time_unit)} #{display_unit}")
    end
  end

  context 'with agent user' do
    context 'with full permissions' do
      let(:user) { create(:agent, groups: [group]) }

      it_behaves_like 'showing accounted time'

      context 'with a pre-defined unit' do
        it_behaves_like 'showing accounted time', 'minute', 'minute(s)'
      end

      context 'with a custom unit' do
        it_behaves_like 'showing accounted time', 'custom', 'person day(s)'
      end
    end

    context 'with read permissions' do
      let(:user) { create(:agent, groups: [group], group_names_access_map: { group.name => 'read' }) }

      it_behaves_like 'showing accounted time'
    end
  end

  context 'with customer user' do
    let(:user)   { create(:customer) }
    let(:ticket) { create(:ticket, customer: user) }

    it 'does not show accounted time' do
      expect(page).to have_text(ticket.title)
      expect(page).to have_no_text('Total accounted time')
    end
  end
end
