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

require 'rails_helper'

RSpec.describe 'Ticket::Article API > Time Accounting', :aggregate_failures, type: :request do
  let(:ticket) { create(:ticket) }
  let(:agent)  { create(:agent, groups: Group.all) }

  describe 'GET /api/v1/ticket_articles' do
    let(:article)        { create(:ticket_article, ticket: ticket) }
    let(:accounted_time) { create(:ticket_time_accounting, ticket: ticket, ticket_article: article, time_unit: 42) }

    before do
      article && accounted_time

      authenticated_as(agent)
      get "/api/v1/ticket_articles/#{article.id}?expand=true"
    end

    context 'when no time was accounted' do
      let(:accounted_time) { nil }

      it 'returns nil' do
        expect(response).to have_http_status(:ok)
        expect(json_response['time_unit']).to be_nil
      end
    end

    context 'when time was accounted' do
      it 'returns the accounted time' do
        expect(response).to have_http_status(:ok)
        expect(json_response['time_unit']).to eq(accounted_time.time_unit.to_s)
      end
    end
  end

  describe 'POST /api/v1/ticket_articles' do
    let(:params) do
      {
        ticket_id: ticket.id,
        body:      'Some example body.',
        time_unit: 42,
      }
    end

    let(:time_accounting_enabled) { true }

    before do
      Setting.set('time_accounting', time_accounting_enabled)

      ticket

      authenticated_as(agent)
      post '/api/v1/ticket_articles', params: params, as: :json
    end

    context 'when time accounting is enabled' do
      it 'creates the article together with its accounted time' do
        expect(response).to have_http_status(:created)
        expect(ticket.reload.articles.count).to eq(1)
        expect(Ticket::TimeAccounting.last)
          .to have_attributes(ticket_article_id: json_response['id'], time_unit: 42)
      end
    end

    context 'when time accounting is disabled' do
      let(:time_accounting_enabled) { false }

      # Article and accounted time are stored in one transaction, so a denied accounting
      #   must not leave the article behind. Unlike 'PUT /api/v1/tickets/:id', this endpoint
      #   has no surrounding transaction of its own to roll the article back.
      it 'creates neither the article nor the accounted time' do
        expect(response).to have_http_status(:forbidden)
        expect(ticket.reload.articles).to be_empty
        expect(Ticket::TimeAccounting.count).to be_zero
      end
    end
  end

  describe 'PUT /api/v1/tickets/:id' do
    let(:params) do
      {
        article: article
      }
    end

    let(:article) do
      {
        body:                   'Some example body.',
        time_unit:              time_unit,
        accounted_time_type_id: accounted_time_type_id
      }
    end

    let(:time_unit)                { 42 }
    let(:accounted_time_type_id)   { nil }
    let(:time_accounting_enabled)  { true }

    before do
      Setting.set('time_accounting', time_accounting_enabled)

      ticket

      authenticated_as(agent)
      put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
    end

    context 'when time accounting disabled' do
      let(:time_accounting_enabled) { false }

      it 'does not create ticket article' do
        expect(response).to have_http_status(:forbidden)
        expect(ticket.reload.articles).to be_empty
        expect(Ticket::TimeAccounting.count).to be_zero
      end
    end

    context 'without accounted time type' do
      it 'created accounted time entry for article' do
        expect(response).to have_http_status(:ok)
        expect(Ticket::TimeAccounting.last.time_unit).to eq(42)
      end
    end

    context 'with accounted time type' do
      let(:accounted_time_type)    { create(:ticket_time_accounting_type) }
      let(:accounted_time_type_id) { accounted_time_type.id }

      it 'created accounted time entry for article' do
        expect(response).to have_http_status(:ok)
        expect(Ticket::TimeAccounting.last.time_unit).to eq(42)
        expect(Ticket::TimeAccounting.last.type.id).to eq(accounted_time_type_id)
      end

      context 'with type name' do
        let(:article) do
          {
            body:                'Some example body.',
            time_unit:           time_unit,
            accounted_time_type: accounted_time_type.name
          }
        end

        it 'created accounted time entry for article' do
          expect(response).to have_http_status(:ok)
          expect(Ticket::TimeAccounting.last.time_unit).to eq(42)
          expect(Ticket::TimeAccounting.last.type.id).to eq(accounted_time_type_id)
        end
      end
    end
  end
end
